diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/test/CollisionTest.cpp | 139 | ||||
-rw-r--r-- | src/test/main.cpp | 2 |
3 files changed, 142 insertions, 2 deletions
diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index dc985a3..3eee466 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,11 +1,12 @@ target_sources(test_main PUBLIC + CollisionTest.cpp main.cpp PhysicsTest.cpp ScriptTest.cpp ParticleTest.cpp - EventTest.cpp ECSTest.cpp SceneManagerTest.cpp + EventTest.cpp ValueBrokerTest.cpp DBTest.cpp ) diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp new file mode 100644 index 0000000..a68db46 --- /dev/null +++ b/src/test/CollisionTest.cpp @@ -0,0 +1,139 @@ +#include <gtest/gtest.h> + +#define private public +#define protected public + +#include <crepe/ComponentManager.h> +#include <crepe/api/Event.h> +#include <crepe/api/EventManager.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Script.h> +#include <crepe/api/Transform.h> +#include <crepe/system/CollisionSystem.h> +#include <crepe/system/ScriptSystem.h> +#include <crepe/types.h> +#include <crepe/util/Log.h> + +using namespace std; +using namespace std::chrono_literals; +using namespace crepe; +using namespace testing; + +class CollisionHandler : public Script { +public: + int box_id; + EventManager & evmgr = EventManager::get_instance(); + function<void(const CollisionEvent& ev)> test_fn = [](const CollisionEvent & ev) { }; + + CollisionHandler(int box_id) { + this->box_id = box_id; + } + + bool on_collision(const CollisionEvent& ev) { + test_fn(ev); + return true; + } + + void init() { + Log::logf("Box {} script init()", box_id); + + // TODO: this should be built into script + evmgr.subscribe<CollisionEvent>([this](const CollisionEvent & ev) { + return this->on_collision(ev); + }, this->get_game_object_id()); + } +}; + +class CollisionTest : public Test { +public: + ComponentManager mgr; + CollisionSystem collision_sys{mgr}; + ScriptSystem script_sys{mgr}; + + GameObject world = mgr.new_object("world"); + GameObject game_object1 = mgr.new_object("object1", "", { 0, 0 }); + GameObject game_object2 = mgr.new_object("object2", "", { 0, 0 }); + + CollisionHandler * script_object1_ref = nullptr; + CollisionHandler * script_object2_ref = nullptr; + + void SetUp() override { + world.add_component<Rigidbody>(Rigidbody::Data{ + // TODO: remove unrelated properties: + .body_type = Rigidbody::BodyType::STATIC, + .bounce = false, + .offset = {0,0}, + }); + // Create a box with an inner size of 10x10 units + world.add_component<BoxCollider>(Vector2{5, 11}, 10, 2); // Top + world.add_component<BoxCollider>(Vector2{5, -1}, 10, 2); // Bottom + world.add_component<BoxCollider>(Vector2{-1, 5}, 2, 10); // Left + world.add_component<BoxCollider>(Vector2{11, 5}, 2, 10); // right + + game_object1.add_component<Rigidbody>(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 0.01, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = {1,0}, + .constraints = {0, 0, 0}, + .use_gravity = true, + .bounce = true, + .elastisity = 1, + .offset = {0,0}, + }); + game_object1.add_component<BoxCollider>(Vector2{0, 0}, 20, 20); + BehaviorScript & script_object1 = game_object1.add_component<BehaviorScript>().set_script<CollisionHandler>(1); + script_object1_ref = static_cast<CollisionHandler*>(script_object1.script.get()); + ASSERT_NE(script_object1_ref, nullptr); + + game_object2.add_component<Rigidbody>(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 0.01, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = {1,0}, + .constraints = {0, 0, 0}, + .use_gravity = true, + .bounce = true, + .elastisity = 1, + .offset = {0,0}, + }); + game_object2.add_component<BoxCollider>(Vector2{0, 0}, 20, 20); + BehaviorScript & script_object2 = game_object2.add_component<BehaviorScript>().set_script<CollisionHandler>(2); + script_object2_ref = static_cast<CollisionHandler*>(script_object2.script.get()); + ASSERT_NE(script_object2_ref, nullptr); + + // Ensure Script::init() is called on all BehaviorScript instances + script_sys.update(); + } +}; + +TEST_F(CollisionTest, collision_example) { + script_object1_ref->test_fn = [](const CollisionEvent & ev) { + Log::logf("event x={} y={}", ev.info.move_back_value.x, ev.info.move_back_value.y); + EXPECT_TRUE(true); + }; + collision_sys.update(); + + // should be nullptr after update with no collision + //ASSERT_EQ(MyScriptCollider1::last_collision_info_1, nullptr); + //ASSERT_EQ(MyScriptCollider2::last_collision_info_2, nullptr); + // check if values are correct (filled in data) + // EXPECT_EQ(MyScriptCollider1::last_collision_info->first.collider.game_object_id, 1); + // EXPECT_EQ(MyScriptCollider2::last_collision_info->second.collider.game_object_id, 2); + // check test data +} + +TEST_F(CollisionTest, collision_box_box_dynamic) { + script_object1_ref->test_fn = [](const CollisionEvent & ev) { + EXPECT_TRUE(false); + }; + collision_sys.update(); + // should be nullptr after update with no collision + // ASSERT_NE(MyScriptCollider1::last_collision_info_1, nullptr); + // ASSERT_NE(MyScriptCollider2::last_collision_info_2, nullptr); + // // check if values are correct (filled in data) + // EXPECT_EQ(MyScriptCollider1::last_collision_info_1->first.collider.game_object_id, 1); + // EXPECT_EQ(MyScriptCollider2::last_collision_info_2->second.collider.game_object_id, 2); + // check test data +} diff --git a/src/test/main.cpp b/src/test/main.cpp index 241015d..f792239 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -9,7 +9,7 @@ int main(int argc, char ** argv) { InitGoogleTest(&argc, argv); auto & cfg = Config::get_instance(); - cfg.log.level = Log::Level::ERROR; + cfg.log.level = Log::Level::INFO; return RUN_ALL_TESTS(); } |