From 5ee56897e28dea05853e463e061d7f21cb9771a3 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Wed, 13 Nov 2024 18:46:31 +0100 Subject: updated test --- src/test/CollisionTest.cpp | 100 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/test/CollisionTest.cpp (limited to 'src/test/CollisionTest.cpp') diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp new file mode 100644 index 0000000..1dfe808 --- /dev/null +++ b/src/test/CollisionTest.cpp @@ -0,0 +1,100 @@ +#include "api/BoxCollider.h" +#include "api/CircleCollider.h" +#include "api/Vector2.h" +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace std::chrono_literals; +using namespace crepe; + +class CollisionTest : public ::testing::Test { +protected: + GameObject * game_object1; + GameObject * game_object2; + PhysicsSystem physics_system; + void SetUp() override { + ComponentManager & mgr = ComponentManager::get_instance(); + std::vector> transforms + = mgr.get_components_by_id(0); + if (transforms.empty()) { + double width,height,radius = 10; + // ob 1 + game_object1 = new GameObject(0, "", "", Vector2{0, 0}, 0, 0); + game_object1->add_component(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 1, + .body_type = Rigidbody::BodyType::DYNAMIC, + .max_linear_velocity = Vector2{10, 10}, + .max_angular_velocity = 10, + .constraints = {0, 0}, + .use_gravity = true, + .bounce = false, + }); + + game_object1->add_component(Vector2{0,0},width,height); + game_object1->add_component(Vector2{0,0},radius); + + //ob 2 + game_object2 = new GameObject(0, "", "", Vector2{0, 0}, 0, 0); + game_object2->add_component(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 1, + .body_type = Rigidbody::BodyType::DYNAMIC, + .max_linear_velocity = Vector2{10, 10}, + .max_angular_velocity = 10, + .constraints = {0, 0}, + .use_gravity = true, + .bounce = false, + }); + game_object2->add_component(Vector2{0,0},width,height); + game_object2->add_component(Vector2{0,0},radius); + + } + for (int i = 0; i < 2; i ++) { + transforms = mgr.get_components_by_id(i); + Transform & transform = transforms.front().get(); + transform.position.x = 0.0; + transform.position.y = 0.0; + transform.rotation = 0.0; + std::vector> rigidbodies + = mgr.get_components_by_id(i); + Rigidbody & rigidbody = rigidbodies.front().get(); + rigidbody.data.angular_velocity = 0; + rigidbody.data.linear_velocity.x = 0; + rigidbody.data.linear_velocity.y = 0; + std::vector> boxcolliders + = mgr.get_components_by_id(i); + BoxCollider & box_collider = boxcolliders.front().get(); + box_collider.offset = {0,0}; + box_collider.width = 10; + box_collider.height = 10; + std::vector> circlecolliders + = mgr.get_components_by_id(i); + CircleCollider & circle_collider = circlecolliders.front().get(); + circle_collider.offset = {0,0}; + circle_collider.radius = 10; + } + + } +}; + +TEST_F(CollisionTest, box_box_collision) { + Config::get_instance().physics.gravity = 1; + ComponentManager & mgr = ComponentManager::get_instance(); + std::vector> transforms + = mgr.get_components_by_id(0); + const Transform & transform = transforms.front().get(); + ASSERT_FALSE(transforms.empty()); + EXPECT_EQ(transform.position.y, 0); + physics_system.update(); + EXPECT_EQ(transform.position.y, 1); + physics_system.update(); + EXPECT_EQ(transform.position.y, 3); +} + -- cgit v1.2.3 From 15c4cf103c4da7808b7173581ead22ab7190632d Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 14 Nov 2024 18:49:17 +0100 Subject: updated test --- src/crepe/system/CollisionSystem.cpp | 3 +- src/test/CMakeLists.txt | 1 + src/test/CollisionTest.cpp | 107 ++++++++++++++--------------------- src/test/PhysicsTest.cpp | 1 + 4 files changed, 46 insertions(+), 66 deletions(-) (limited to 'src/test/CollisionTest.cpp') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index a6cf891..0d056a5 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -23,6 +23,7 @@ void CollisionSystem::update() { std::vector> boxcolliders = mgr.get_components_by_type(); std::vector> circlecolliders = mgr.get_components_by_type(); std::vector> collided = check_collisions(boxcolliders,circlecolliders); + std::cout << "DEBUG INFO" << std::endl; for (const auto& collision : collided) { std::cout << "Object " << collision.first << " collided with Object " << collision.second << std::endl; } @@ -206,4 +207,4 @@ Vector2 CollisionSystem::current_position(const Collider& collider, const Transf // Final positions considering scaling and rotation return(transform.position + Vector2(rotated_total_offset_x1, rotated_total_offset_y1)); -} \ No newline at end of file +} diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 0e4eaed..8618ae6 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -2,5 +2,6 @@ target_sources(test_main PUBLIC dummy.cpp # audio.cpp PhysicsTest.cpp + CollisionTest.cpp ) diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index 1dfe808..1c4c325 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include using namespace std; @@ -17,70 +17,42 @@ class CollisionTest : public ::testing::Test { protected: GameObject * game_object1; GameObject * game_object2; - PhysicsSystem physics_system; + CollisionSystem collision_system; void SetUp() override { ComponentManager & mgr = ComponentManager::get_instance(); + mgr.delete_all_components(); std::vector> transforms = mgr.get_components_by_id(0); - if (transforms.empty()) { - double width,height,radius = 10; - // ob 1 - game_object1 = new GameObject(0, "", "", Vector2{0, 0}, 0, 0); - game_object1->add_component(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, - .max_linear_velocity = Vector2{10, 10}, - .max_angular_velocity = 10, - .constraints = {0, 0}, - .use_gravity = true, - .bounce = false, - }); - - game_object1->add_component(Vector2{0,0},width,height); - game_object1->add_component(Vector2{0,0},radius); - //ob 2 - game_object2 = new GameObject(0, "", "", Vector2{0, 0}, 0, 0); - game_object2->add_component(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, - .max_linear_velocity = Vector2{10, 10}, - .max_angular_velocity = 10, - .constraints = {0, 0}, - .use_gravity = true, - .bounce = false, - }); - game_object2->add_component(Vector2{0,0},width,height); - game_object2->add_component(Vector2{0,0},radius); - - } - for (int i = 0; i < 2; i ++) { - transforms = mgr.get_components_by_id(i); - Transform & transform = transforms.front().get(); - transform.position.x = 0.0; - transform.position.y = 0.0; - transform.rotation = 0.0; - std::vector> rigidbodies - = mgr.get_components_by_id(i); - Rigidbody & rigidbody = rigidbodies.front().get(); - rigidbody.data.angular_velocity = 0; - rigidbody.data.linear_velocity.x = 0; - rigidbody.data.linear_velocity.y = 0; - std::vector> boxcolliders - = mgr.get_components_by_id(i); - BoxCollider & box_collider = boxcolliders.front().get(); - box_collider.offset = {0,0}; - box_collider.width = 10; - box_collider.height = 10; - std::vector> circlecolliders - = mgr.get_components_by_id(i); - CircleCollider & circle_collider = circlecolliders.front().get(); - circle_collider.offset = {0,0}; - circle_collider.radius = 10; - } + // ob 1 + game_object1 = new GameObject(0, "", "", Vector2{0, 0}, 0, 0); + game_object1->add_component(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 1, + .body_type = Rigidbody::BodyType::DYNAMIC, + .max_linear_velocity = Vector2{10, 10}, + .max_angular_velocity = 10, + .constraints = {0, 0, 0}, + .use_gravity = false, + .bounce = false, + }); + + game_object1->add_component(Vector2{0,0},10,10); + + //ob 2 + game_object2 = new GameObject(1, "", "", Vector2{50, 50}, 0, 0); + game_object2->add_component(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 1, + .body_type = Rigidbody::BodyType::DYNAMIC, + .max_linear_velocity = Vector2{10, 10}, + .max_angular_velocity = 10, + .constraints = {0, 0, 0}, + .use_gravity = false, + .bounce = false, + }); + game_object2->add_component(Vector2{0,0},10,10); } }; @@ -89,12 +61,17 @@ TEST_F(CollisionTest, box_box_collision) { ComponentManager & mgr = ComponentManager::get_instance(); std::vector> transforms = mgr.get_components_by_id(0); - const Transform & transform = transforms.front().get(); + Transform & transform = transforms.front().get(); ASSERT_FALSE(transforms.empty()); - EXPECT_EQ(transform.position.y, 0); - physics_system.update(); - EXPECT_EQ(transform.position.y, 1); - physics_system.update(); - EXPECT_EQ(transform.position.y, 3); + transform.position = {39,50}; + collision_system.update(); + transform.position = {40,50}; + collision_system.update(); + transform.position = {41,50}; + collision_system.update(); + transform.position = {44,50}; + collision_system.update(); + transform.position = {50,50}; + collision_system.update(); } diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp index 5385962..3332f74 100644 --- a/src/test/PhysicsTest.cpp +++ b/src/test/PhysicsTest.cpp @@ -16,6 +16,7 @@ protected: PhysicsSystem physics_system; void SetUp() override { ComponentManager & mgr = ComponentManager::get_instance(); + mgr.delete_all_components(); std::vector> transforms = mgr.get_components_by_id(0); if (transforms.empty()) { -- cgit v1.2.3 From b748691ea7aab8bd12ed76e81f9e9b4539e7d2f7 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 14 Nov 2024 19:07:07 +0100 Subject: fixed bugs in collision system --- src/crepe/system/CollisionSystem.cpp | 6 +++--- src/test/CollisionTest.cpp | 14 ++++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) (limited to 'src/test/CollisionTest.cpp') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 0d056a5..a3c59e3 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -70,7 +70,7 @@ std::vector> CollisionSystem::check_collisions(const std::ve for (size_t j = 0; j < circlecolliders.size(); ++j) { // Skip self collision - int game_object_id_2 = boxcolliders[j].get().game_object_id; + int game_object_id_2 = circlecolliders[j].get().game_object_id; if (game_object_id_1 == game_object_id_2) continue; // Fetch components for the second collider (circle) @@ -87,13 +87,13 @@ std::vector> CollisionSystem::check_collisions(const std::ve for (size_t i = 0; i < circlecolliders.size(); ++i) { // Fetch components for the first circle collider - int game_object_id_1 = boxcolliders[i].get().game_object_id; + int game_object_id_1 = circlecolliders[i].get().game_object_id; Transform & transform1 = mgr.get_components_by_id(circlecolliders[i].get().game_object_id).front().get(); Rigidbody & rigidbody1 = mgr.get_components_by_id(circlecolliders[i].get().game_object_id).front().get(); for (size_t j = i + 1; j < circlecolliders.size(); ++j) { // Skip self collision - int game_object_id_2 = boxcolliders[j].get().game_object_id; + int game_object_id_2 = circlecolliders[j].get().game_object_id; if (game_object_id_1 == game_object_id_2) continue; // Fetch components for the second circle collider diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index 1c4c325..3e43479 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -52,7 +52,7 @@ protected: .use_gravity = false, .bounce = false, }); - game_object2->add_component(Vector2{0,0},10,10); + game_object2->add_component(Vector2{0,0},5); } }; @@ -67,11 +67,17 @@ TEST_F(CollisionTest, box_box_collision) { collision_system.update(); transform.position = {40,50}; collision_system.update(); - transform.position = {41,50}; + transform.position = {50,39}; collision_system.update(); - transform.position = {44,50}; + transform.position = {50,40}; collision_system.update(); - transform.position = {50,50}; + transform.position = {50,60}; + collision_system.update(); + transform.position = {50,61}; + collision_system.update(); + transform.position = {60,50}; + collision_system.update(); + transform.position = {61,50}; collision_system.update(); } -- cgit v1.2.3 From 690471c7c4536c074a4dca5aab7cc618d47bfb5f Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Wed, 20 Nov 2024 10:43:32 +0100 Subject: merge with maser --- src/crepe/api/LoopManager.cpp | 6 +- src/crepe/api/LoopManager.h | 5 +- src/crepe/api/Script.h | 9 ++- src/crepe/system/CollisionSystem.cpp | 6 +- src/crepe/system/CollisionSystem.h | 2 - src/example/CMakeLists.txt | 2 +- src/example/collision.cpp | 123 ----------------------------------- src/example/game.cpp | 89 +++++++++++++++++++++++++ src/example/particles.cpp | 43 ------------ src/example/physics.cpp | 24 ------- src/example/rendering.cpp | 4 +- src/test/CMakeLists.txt | 2 - src/test/CollisionTest.cpp | 121 ++++++++++++++++------------------ 13 files changed, 165 insertions(+), 271 deletions(-) delete mode 100644 src/example/collision.cpp create mode 100644 src/example/game.cpp delete mode 100644 src/example/particles.cpp delete mode 100644 src/example/physics.cpp (limited to 'src/test/CollisionTest.cpp') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index a4bc101..586919d 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -35,10 +35,8 @@ void LoopManager::start() { void LoopManager::set_running(bool running) { this->game_running = running; } void LoopManager::fixed_update() { - PhysicsSystem phys; - phys.update(); - CollisionSystem col; - col.update(); + this->get_system().update(); + this->get_system().update(); } void LoopManager::loop() { diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index f6904be..37f13ac 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -4,6 +4,7 @@ #include "../ComponentManager.h" #include "../system/System.h" +#include "api/SceneManager.h" namespace crepe { @@ -71,7 +72,9 @@ private: private: //! Component manager instance ComponentManager component_manager{}; - +public: + //! Scene manager instance + SceneManager scene_manager{component_manager}; private: /** * \brief Collection of System instances diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 2b70379..939d142 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -63,7 +63,14 @@ protected: */ template std::vector> get_components() const; - + + /** + * \brief Gets game object id this script is attached to + * + * \returns game object id + */ + game_object_id_t get_game_object_id() const {return this->game_object_id;}; + protected: // NOTE: Script must have a constructor without arguments so the game programmer doesn't need // to manually add `using Script::Script` to their concrete script class. diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index df0ee41..3e73b44 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -18,11 +18,9 @@ using namespace crepe; -CollisionSystem::CollisionSystem() {} - void CollisionSystem::update() { // Get collider components and keep them seperate - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector> boxcolliders = mgr.get_components_by_type(); std::vector> circlecolliders = mgr.get_components_by_type(); @@ -185,7 +183,7 @@ void CollisionSystem::static_collision_handler(CollisionInfo& info){ } std::vector> CollisionSystem::check_collisions(const std::vector>& boxcolliders, const std::vector>& circlecolliders) { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector> collisions_ret; // TODO: diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index 0518b34..d94f6bc 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -70,8 +70,6 @@ public: public: - CollisionSystem(); - //! Updates the collision system by checking for collisions between colliders and handling them. void update() override; diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index d2ea926..2facc4d 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -28,7 +28,7 @@ add_example(proxy) add_example(db) add_example(ecs) add_example(scene_manager) -add_example(collision) +add_example(game) add_example(events) add_example(particles) add_example(gameloop) diff --git a/src/example/collision.cpp b/src/example/collision.cpp deleted file mode 100644 index f51380e..0000000 --- a/src/example/collision.cpp +++ /dev/null @@ -1,123 +0,0 @@ -#include "api/BoxCollider.h" -#include "system/CollisionSystem.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -using namespace crepe; -using namespace std; - -class MyScript : public Script { - static bool oncollision(const CollisionEvent& test) { - std::cout << "test collision: " << test.info.first.collider.game_object_id << std::endl; - return true; - } - void init() { - EventManager::get_instance().subscribe(oncollision, this->parent->game_object_id); - } - void update() { - // Retrieve component from the same GameObject this script is on - - } - - -}; - -int main(int argc, char * argv[]) { - //setup - LoopManager gameloop; - Color color(0, 0, 0, 0); - - double screen_size_width = 640; - double screen_size_height = 480; - double world_collider = 1000; - //define playable world - GameObject World(0, "Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); - World.add_component(Rigidbody::Data{ - .mass = 0, - .gravity_scale = 0, - .body_type = Rigidbody::BodyType::STATIC, - .constraints = {0, 0, 0}, - .use_gravity = false, - .bounce = false, - .offset = {0,0} - }); - World.add_component(Vector2{0, 0-(screen_size_height/2+world_collider/2)}, world_collider, world_collider);; // Top - World.add_component(Vector2{0, screen_size_height/2+world_collider/2}, world_collider, world_collider); // Bottom - World.add_component(Vector2{0-(screen_size_width/2+world_collider/2), 0}, world_collider, world_collider); // Left - World.add_component(Vector2{screen_size_width/2+world_collider/2, 0}, world_collider, world_collider); // right - - - GameObject game_object1(1, "Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); - game_object1.add_component(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(Vector2{0, 0}, 20, 20); - game_object1.add_component().set_script(); - game_object1.add_component( - make_shared("/home/jaro/crepe/asset/texture/green_square.png"), color, - FlipSettings{true, true}); - game_object1.add_component(Color::get_white()); - - - // GameObject game_object2(2, "Name", "Tag", Vector2{20, 470}, 0, 1); - // game_object2.add_component(Rigidbody::Data{ - // .mass = 1, - // .gravity_scale = 1, - // .body_type = Rigidbody::BodyType::DYNAMIC, - // .linear_velocity = {0,0}, - // .constraints = {0, 0, 0}, - // .use_gravity = false, - // .bounce = false, - // .offset = {0,0}, - // }); - // game_object2.add_component(Vector2{0, 0}, 0, 0); - // game_object2.add_component().set_script(); - // game_object2.add_component( - // make_shared("/home/jaro/crepe/asset/texture/red_square.png"), color, - // FlipSettings{true, true}); - - - crepe::ScriptSystem sys; - // Update all scripts. This should result in MyScript::update being called - sys.update(); - - gameloop.start(); - // auto & render = crepe::RenderSystem::get_instance(); - // auto start = std::chrono::steady_clock::now(); - // while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { - // render.update(); - // } - - - return 0; -} diff --git a/src/example/game.cpp b/src/example/game.cpp new file mode 100644 index 0000000..a9f6103 --- /dev/null +++ b/src/example/game.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace crepe; + +using namespace std; + +class MyScript : public Script { + static bool oncollision(const CollisionEvent& test) { + std::cout << "test collision: " << test.info.first.collider.game_object_id << std::endl; + return true; + } + void init() { + EventManager::get_instance().subscribe(oncollision, this->get_game_object_id()); + } + void update() { + // Retrieve component from the same GameObject this script is on + + } +}; + +class ConcreteScene1 : public Scene { +public: + using Scene::Scene; + + void load_scene() { + ComponentManager & mgr = this->component_manager; + Color color(0, 0, 0, 0); + + double screen_size_width = 640; + double screen_size_height = 480; + double world_collider = 1000; + //define playable world + GameObject world = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); + world.add_component(Rigidbody::Data{ + .mass = 0, + .gravity_scale = 0, + .body_type = Rigidbody::BodyType::STATIC, + .constraints = {0, 0, 0}, + .use_gravity = false, + .bounce = false, + .offset = {0,0} + }); + world.add_component(Vector2{0, 0-(screen_size_height/2+world_collider/2)}, world_collider, world_collider);; // Top + world.add_component(Vector2{0, screen_size_height/2+world_collider/2}, world_collider, world_collider); // Bottom + world.add_component(Vector2{0-(screen_size_width/2+world_collider/2), 0}, world_collider, world_collider); // Left + world.add_component(Vector2{screen_size_width/2+world_collider/2, 0}, world_collider, world_collider); // right + + + GameObject game_object1 = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); + game_object1.add_component(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(Vector2{0, 0}, 20, 20); + game_object1.add_component().set_script(); + game_object1.add_component( + make_shared("/home/jaro/crepe/asset/texture/green_square.png"), color, + FlipSettings{true, true}); + game_object1.add_component(Color::get_white()); + } +}; + +int main(int argc, char * argv[]) { + + LoopManager gameloop; + gameloop.scene_manager.add_scene("scene1"); + gameloop.scene_manager.load_next_scene(); + gameloop.start(); + return 0; +} diff --git a/src/example/particles.cpp b/src/example/particles.cpp deleted file mode 100644 index 3d5f676..0000000 --- a/src/example/particles.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -int main(int argc, char * argv[]) { - ComponentManager mgr{}; - GameObject game_object = mgr.new_object("", "", Vector2{0, 0}, 0, 0); - Color color(0, 0, 0, 0); - Sprite test_sprite = game_object.add_component( - make_shared("../asset/texture/img.png"), color, FlipSettings{true, true}); - game_object.add_component(ParticleEmitter::Data{ - .position = {0, 0}, - .max_particles = 100, - .emission_rate = 0, - .min_speed = 0, - .max_speed = 0, - .min_angle = 0, - .max_angle = 0, - .begin_lifespan = 0, - .end_lifespan = 0, - .force_over_time = Vector2{0, 0}, - .boundary{ - .width = 0, - .height = 0, - .offset = Vector2{0, 0}, - .reset_on_exit = false, - }, - .sprite = test_sprite, - }); - - return 0; -} diff --git a/src/example/physics.cpp b/src/example/physics.cpp deleted file mode 100644 index ad663a0..0000000 --- a/src/example/physics.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -int main(int argc, char * argv[]) { - ComponentManager mgr{}; - - GameObject game_object = mgr.new_object("Name", "Tag", Vector2{0, 0}, 0, 0); - game_object.add_component(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, - .constraints = {0, 0, 0}, - .use_gravity = true, - .bounce = false, - }); - return 0; -} diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index c9e62f1..14ecaa9 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -30,13 +30,13 @@ int main() { // Normal adding components { Color color(0, 0, 0, 0); - obj.add_component(make_shared("../asset/texture/img.png"), color, + obj.add_component(make_shared("/home/jaro/crepe/asset/texture/green_square.png"), color, FlipSettings{false, false}); obj.add_component(Color::get_red()); } { Color color(0, 0, 0, 0); - obj1.add_component(make_shared("../asset/texture/second.png"), color, + obj1.add_component(make_shared("/home/jaro/crepe/asset/texture/green_square.png"), color, FlipSettings{true, true}); } diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index a41d097..f830165 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,6 +1,4 @@ target_sources(test_main PUBLIC - dummy.cpp - # audio.cpp CollisionTest.cpp main.cpp PhysicsTest.cpp diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index 3e43479..83564e1 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -1,83 +1,76 @@ -#include "api/BoxCollider.h" -#include "api/CircleCollider.h" -#include "api/Vector2.h" #include #include #include #include #include -#include +#include #include using namespace std; using namespace std::chrono_literals; using namespace crepe; -class CollisionTest : public ::testing::Test { -protected: - GameObject * game_object1; - GameObject * game_object2; - CollisionSystem collision_system; - void SetUp() override { - ComponentManager & mgr = ComponentManager::get_instance(); - mgr.delete_all_components(); - std::vector> transforms - = mgr.get_components_by_id(0); - - // ob 1 - game_object1 = new GameObject(0, "", "", Vector2{0, 0}, 0, 0); - game_object1->add_component(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, - .max_linear_velocity = Vector2{10, 10}, - .max_angular_velocity = 10, - .constraints = {0, 0, 0}, - .use_gravity = false, - .bounce = false, - }); - - game_object1->add_component(Vector2{0,0},10,10); +class MyScript : public Script { + static bool oncollision(const CollisionEvent& test) { + std::cout << "test collision: " << test.info.first.collider.game_object_id << std::endl; + return true; + } + void init() { + EventManager::get_instance().subscribe(oncollision, this->get_game_object_id()); + } + void update() { + // Retrieve component from the same GameObject this script is on + } +}; + +class PhysicsTest : public ::testing::Test { +public: + ComponentManager component_manager; + PhysicsSystem system{component_manager}; - //ob 2 - game_object2 = new GameObject(1, "", "", Vector2{50, 50}, 0, 0); - game_object2->add_component(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, - .max_linear_velocity = Vector2{10, 10}, - .max_angular_velocity = 10, - .constraints = {0, 0, 0}, - .use_gravity = false, - .bounce = false, - }); - game_object2->add_component(Vector2{0,0},5); + void SetUp() override { + ComponentManager & mgr = this->component_manager; + vector> transforms + = mgr.get_components_by_id(0); + if (transforms.empty()) { + auto entity = mgr.new_object("", "", Vector2{0, 0}, 0, 0); + entity.add_component(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 1, + .body_type = Rigidbody::BodyType::DYNAMIC, + .max_linear_velocity = Vector2{10, 10}, + .max_angular_velocity = 10, + .constraints = {0, 0}, + .use_gravity = true, + .bounce = false, + }); + } + transforms = mgr.get_components_by_id(0); + Transform & transform = transforms.front().get(); + transform.position.x = 0.0; + transform.position.y = 0.0; + transform.rotation = 0.0; + vector> rigidbodies + = mgr.get_components_by_id(0); + Rigidbody & rigidbody = rigidbodies.front().get(); + rigidbody.data.angular_velocity = 0; + rigidbody.data.linear_velocity.x = 0; + rigidbody.data.linear_velocity.y = 0; } }; -TEST_F(CollisionTest, box_box_collision) { +TEST_F(PhysicsTest, gravity) { Config::get_instance().physics.gravity = 1; - ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> transforms - = mgr.get_components_by_id(0); - Transform & transform = transforms.front().get(); + ComponentManager & mgr = this->component_manager; + vector> transforms = mgr.get_components_by_id(0); + const Transform & transform = transforms.front().get(); ASSERT_FALSE(transforms.empty()); - transform.position = {39,50}; - collision_system.update(); - transform.position = {40,50}; - collision_system.update(); - transform.position = {50,39}; - collision_system.update(); - transform.position = {50,40}; - collision_system.update(); - transform.position = {50,60}; - collision_system.update(); - transform.position = {50,61}; - collision_system.update(); - transform.position = {60,50}; - collision_system.update(); - transform.position = {61,50}; - collision_system.update(); -} + EXPECT_EQ(transform.position.y, 0); + + system.update(); + EXPECT_EQ(transform.position.y, 1); + system.update(); + EXPECT_EQ(transform.position.y, 3); +} -- cgit v1.2.3 From 5b7f4b3461a7108c8c320ffaa239b11b6ef5933c Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Wed, 20 Nov 2024 10:54:36 +0100 Subject: empty test with static collison data from script --- src/test/CollisionTest.cpp | 82 ++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 54 deletions(-) (limited to 'src/test/CollisionTest.cpp') diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index 83564e1..3c2ee6e 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -1,76 +1,50 @@ -#include +#include + #include #include #include #include +#include +#include +#include + +#include #include -#include + using namespace std; using namespace std::chrono_literals; using namespace crepe; + class MyScript : public Script { - static bool oncollision(const CollisionEvent& test) { - std::cout << "test collision: " << test.info.first.collider.game_object_id << std::endl; - return true; - } - void init() { - EventManager::get_instance().subscribe(oncollision, this->get_game_object_id()); - } - void update() { - // Retrieve component from the same GameObject this script is on - - } + public: + static crepe::CollisionSystem::CollisionInfo last_collision_info; + private: + static bool oncollision(const CollisionEvent& test) { + std::cout << "test collision: " << test.info.first.collider.game_object_id << std::endl; + return true; + } + void init() { + EventManager::get_instance().subscribe(oncollision, this->get_game_object_id()); + } + void update() { + // Retrieve component from the same GameObject this script is on + + } }; -class PhysicsTest : public ::testing::Test { +class CollisionTest : public ::testing::Test { public: ComponentManager component_manager; PhysicsSystem system{component_manager}; - + void SetUp() override { ComponentManager & mgr = this->component_manager; - vector> transforms - = mgr.get_components_by_id(0); - if (transforms.empty()) { - auto entity = mgr.new_object("", "", Vector2{0, 0}, 0, 0); - entity.add_component(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, - .max_linear_velocity = Vector2{10, 10}, - .max_angular_velocity = 10, - .constraints = {0, 0}, - .use_gravity = true, - .bounce = false, - }); - } - transforms = mgr.get_components_by_id(0); - Transform & transform = transforms.front().get(); - transform.position.x = 0.0; - transform.position.y = 0.0; - transform.rotation = 0.0; - vector> rigidbodies - = mgr.get_components_by_id(0); - Rigidbody & rigidbody = rigidbodies.front().get(); - rigidbody.data.angular_velocity = 0; - rigidbody.data.linear_velocity.x = 0; - rigidbody.data.linear_velocity.y = 0; } }; -TEST_F(PhysicsTest, gravity) { - Config::get_instance().physics.gravity = 1; - ComponentManager & mgr = this->component_manager; - vector> transforms = mgr.get_components_by_id(0); - const Transform & transform = transforms.front().get(); - ASSERT_FALSE(transforms.empty()); - EXPECT_EQ(transform.position.y, 0); - - system.update(); - EXPECT_EQ(transform.position.y, 1); - - system.update(); - EXPECT_EQ(transform.position.y, 3); +TEST_F(CollisionTest, collision) { +//read static data +const CollisionEvent& test = MyScript::last_collision_info; } -- cgit v1.2.3 From 02cdbd367d701d8d858806f45bfe2f15b392bb59 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Wed, 20 Nov 2024 11:37:49 +0100 Subject: made test function and fixed static value --- src/example/game.cpp | 5 ++-- src/test/CollisionTest.cpp | 60 +++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 9 deletions(-) (limited to 'src/test/CollisionTest.cpp') diff --git a/src/example/game.cpp b/src/example/game.cpp index a9f6103..b6a0c31 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -26,7 +26,6 @@ class MyScript : public Script { } void update() { // Retrieve component from the same GameObject this script is on - } }; @@ -35,8 +34,8 @@ public: using Scene::Scene; void load_scene() { - ComponentManager & mgr = this->component_manager; - Color color(0, 0, 0, 0); + ComponentManager & mgr = this->component_manager; + Color color(0, 0, 0, 0); double screen_size_width = 640; double screen_size_height = 480; diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index 3c2ee6e..5da26cc 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -9,7 +10,8 @@ #include #include -#include +#include +#include using namespace std; @@ -19,10 +21,10 @@ using namespace crepe; class MyScript : public Script { public: - static crepe::CollisionSystem::CollisionInfo last_collision_info; + static const crepe::CollisionSystem::CollisionInfo* last_collision_info; private: static bool oncollision(const CollisionEvent& test) { - std::cout << "test collision: " << test.info.first.collider.game_object_id << std::endl; + last_collision_info = &test.info; return true; } void init() { @@ -37,14 +39,60 @@ class MyScript : public Script { class CollisionTest : public ::testing::Test { public: ComponentManager component_manager; - PhysicsSystem system{component_manager}; + CollisionSystem system{component_manager}; void SetUp() override { + MyScript::last_collision_info = nullptr; ComponentManager & mgr = this->component_manager; + if(mgr.get_components_by_id(0).empty()) + { + create_test_world(); + create_test_components(); + } + reset_test_components(); + } + + void create_test_world() { + double screen_size_width = 640; + double screen_size_height = 480; + double world_collider = 1000; + ComponentManager & mgr = this->component_manager; + GameObject world = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); + world.add_component(Rigidbody::Data{ + .mass = 0, + .gravity_scale = 0, + .body_type = Rigidbody::BodyType::STATIC, + .constraints = {0, 0, 0}, + .use_gravity = false, + .bounce = false, + .offset = {0,0} + }); + world.add_component(Vector2{0, 0-(screen_size_height/2+world_collider/2)}, world_collider, world_collider);; // Top + world.add_component(Vector2{0, screen_size_height/2+world_collider/2}, world_collider, world_collider); // Bottom + world.add_component(Vector2{0-(screen_size_width/2+world_collider/2), 0}, world_collider, world_collider); // Left + world.add_component(Vector2{screen_size_width/2+world_collider/2, 0}, world_collider, world_collider); // right + } + + void create_test_components() + { + + } + + void reset_test_components() + { + } }; TEST_F(CollisionTest, collision) { -//read static data -const CollisionEvent& test = MyScript::last_collision_info; + // change object data before calling update + + // call collision system update + system.update(); + // should not be nullptr after update + ASSERT_NE(MyScript::last_collision_info, nullptr); + + // check if values are correct + EXPECT_EQ(MyScript::last_collision_info->first.collider.game_object_id, 1); + EXPECT_EQ(MyScript::last_collision_info->second.collider.game_object_id, 2); } -- cgit v1.2.3 From 37a38c8c9b1b78d214d8e26d45d5a85dc7bd89bd Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Wed, 20 Nov 2024 15:14:44 +0100 Subject: changed test --- src/crepe/system/CollisionSystem.cpp | 2 +- src/example/CMakeLists.txt | 2 - src/test/CollisionTest.cpp | 157 ++++++++++++++++++++++++++++------- 3 files changed, 130 insertions(+), 31 deletions(-) (limited to 'src/test/CollisionTest.cpp') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 3e73b44..2132b0d 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -205,7 +205,7 @@ std::vector(game_object_id_1).front().get(); if(!rigidbody1.active) continue; - // Check CircleCollider vs CircleCollider + // Check BoxCollider vs BoxCollider for (size_t j = i + 1; j < boxcolliders.size(); ++j) { if(!boxcolliders[j].get().active) continue; // Skip self collision diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 2facc4d..c5cb63f 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -22,7 +22,6 @@ add_example(script) add_example(log) add_example(rendering) add_example(asset_manager) -add_example(physics) add_example(savemgr) add_example(proxy) add_example(db) @@ -30,6 +29,5 @@ add_example(ecs) add_example(scene_manager) add_example(game) add_example(events) -add_example(particles) add_example(gameloop) diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index 5da26cc..7aadfa6 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -1,4 +1,5 @@ -#include +#include "system/ScriptSystem.h" +#include "types.h" #include #include @@ -11,38 +12,52 @@ #include #include -#include - +#include "iostream" using namespace std; using namespace std::chrono_literals; using namespace crepe; +//scripts for object 1 collision test +class UnitTestBoxBoxCollision1 : public Script { + static bool oncollision(const CollisionEvent& test) { + std::cout << "collision event 1" << std::endl; + return true; + } + void init() { + EventManager::get_instance().subscribe(oncollision, this->get_game_object_id()); + } + void update() { + // Retrieve component from the same GameObject this script is on + + } +}; -class MyScript : public Script { - public: - static const crepe::CollisionSystem::CollisionInfo* last_collision_info; - private: - static bool oncollision(const CollisionEvent& test) { - last_collision_info = &test.info; - return true; - } - void init() { - EventManager::get_instance().subscribe(oncollision, this->get_game_object_id()); - } - void update() { - // Retrieve component from the same GameObject this script is on - - } +//scripts for object 1 collision test +class UnitTestBoxBoxCollision2 : public Script { + static bool oncollision(const CollisionEvent& test) { + std::cout << "collision event 1" << std::endl; + return true; + } + void init() { + EventManager::get_instance().subscribe(oncollision, this->get_game_object_id()); + } + void update() { + // Retrieve component from the same GameObject this script is on + + } }; class CollisionTest : public ::testing::Test { public: ComponentManager component_manager; CollisionSystem system{component_manager}; + ScriptSystem sr{component_manager}; + const double screen_size_width = 640; + const double screen_size_height = 480; + const double world_collider = 1000; void SetUp() override { - MyScript::last_collision_info = nullptr; ComponentManager & mgr = this->component_manager; if(mgr.get_components_by_id(0).empty()) { @@ -50,12 +65,11 @@ public: create_test_components(); } reset_test_components(); + sr.update(); } void create_test_world() { - double screen_size_width = 640; - double screen_size_height = 480; - double world_collider = 1000; + ComponentManager & mgr = this->component_manager; GameObject world = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); world.add_component(Rigidbody::Data{ @@ -75,24 +89,111 @@ public: void create_test_components() { + ComponentManager & mgr = this->component_manager; + GameObject game_object1 = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); + game_object1.add_component(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(Vector2{0, 0}, 20, 20); + game_object1.add_component().set_script(); + + GameObject game_object2 = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2-100}, 0, 1); + game_object2.add_component(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(Vector2{0, 0}, 20, 20); + game_object2.add_component().set_script(); } void reset_test_components() { + ComponentManager & mgr = this->component_manager; + //game object 1 + { + game_object_id_t id = 1; + Transform & tf = mgr.get_components_by_id(id).front().get(); + tf.position = Vector2{screen_size_width/2, screen_size_height/2}; + tf.rotation = 0; + tf.scale = 1; + tf.active = 1; + Rigidbody & rg = mgr.get_components_by_id(id).front().get(); + rg.data.angular_damping = 0; + rg.data.angular_velocity = 0; + rg.data.max_angular_velocity = 100; + rg.data.linear_velocity = {0,0}; + rg.data.linear_damping = {0,0}; + rg.data.max_linear_velocity = {100,100}; + rg.data.bounce = false; + rg.data.elastisity = 0; + rg.data.offset = {0,0}; + rg.data.constraints = {0,0,0}; + } + { + game_object_id_t id = 2; + Transform & tf = mgr.get_components_by_id(id).front().get(); + tf.position = Vector2{screen_size_width/2, screen_size_height/2-100}; + tf.rotation = 0; + tf.scale = 1; + tf.active = 1; + Rigidbody & rg = mgr.get_components_by_id(id).front().get(); + rg.data.angular_damping = 0; + rg.data.angular_velocity = 0; + rg.data.max_angular_velocity = 100; + rg.data.linear_velocity = {0,0}; + rg.data.linear_damping = {0,0}; + rg.data.max_linear_velocity = {100,100}; + rg.data.bounce = false; + rg.data.elastisity = 0; + rg.data.offset = {0,0}; + rg.data.constraints = {0,0,0}; + } } }; -TEST_F(CollisionTest, collision) { +TEST_F(CollisionTest, collision_example) { // change object data before calling update // call collision system update system.update(); - // should not be nullptr after update - ASSERT_NE(MyScript::last_collision_info, nullptr); + // 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 +} - // check if values are correct - EXPECT_EQ(MyScript::last_collision_info->first.collider.game_object_id, 1); - EXPECT_EQ(MyScript::last_collision_info->second.collider.game_object_id, 2); +TEST_F(CollisionTest, collision_box_box_dynamic) { + // change object data before calling update + ComponentManager & mgr = this->component_manager; + Transform & test = mgr.get_components_by_id(2).front().get(); + test.position = {screen_size_width/2,screen_size_height/2}; + // call collision system update + system.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 } -- cgit v1.2.3 From 91bb45fd6430cc353d2a57f2496c3e9945e98557 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Wed, 20 Nov 2024 16:13:46 +0100 Subject: updated test --- src/test/CollisionTest.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/test/CollisionTest.cpp') diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index 7aadfa6..78765b4 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -22,6 +22,8 @@ using namespace crepe; class UnitTestBoxBoxCollision1 : public Script { static bool oncollision(const CollisionEvent& test) { std::cout << "collision event 1" << std::endl; + std::cout << "collision event 1 x" << test.info.move_back_value.x << std::endl; + std::cout << "collision event 1 y" << test.info.move_back_value.y << std::endl; return true; } void init() { -- cgit v1.2.3 From f0effabc7555c08bec9a4284baad8c4e1c61c110 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 18:09:06 +0100 Subject: clean up CollisionTest --- src/crepe/api/BehaviorScript.h | 7 +- src/crepe/api/BehaviorScript.hpp | 6 +- src/test/CollisionTest.cpp | 212 ++++++++++++++------------------------- src/test/main.cpp | 2 +- 4 files changed, 83 insertions(+), 144 deletions(-) (limited to 'src/test/CollisionTest.cpp') diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 9d85d4c..d556fe5 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -39,11 +39,14 @@ public: * \brief Set the concrete script of this component * * \tparam T Concrete script type (derived from \c crepe::Script) + * \tparam Args Arguments for concrete script constructor + * + * \param args Arguments for concrete script constructor (forwarded using perfect forwarding) * * \returns Reference to BehaviorScript component (`*this`) */ - template - BehaviorScript & set_script(); + template + BehaviorScript & set_script(Args &&... args); protected: //! Script instance diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index d80321d..6bd123d 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -9,11 +9,11 @@ namespace crepe { -template -BehaviorScript & BehaviorScript::set_script() { +template +BehaviorScript & BehaviorScript::set_script(Args &&... args) { dbg_trace(); static_assert(std::is_base_of::value); - Script * s = new T(); + Script * s = new T(std::forward(args)...); s->game_object_id = this->game_object_id; s->component_manager_ref = &this->component_manager; this->script = std::unique_ptr