diff options
Diffstat (limited to 'src/test/CollisionTest.cpp')
-rw-r--r-- | src/test/CollisionTest.cpp | 121 |
1 files changed, 57 insertions, 64 deletions
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 <crepe/ComponentManager.h> #include <crepe/api/Config.h> #include <crepe/api/GameObject.h> #include <crepe/api/Rigidbody.h> #include <crepe/api/Transform.h> -#include <crepe/system/CollisionSystem.h> +#include <crepe/system/PhysicsSystem.h> #include <gtest/gtest.h> 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<std::reference_wrapper<Transform>> transforms - = mgr.get_components_by_id<Transform>(0); - - // ob 1 - game_object1 = new GameObject(0, "", "", Vector2{0, 0}, 0, 0); - game_object1->add_component<Rigidbody>(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<BoxCollider>(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<CollisionEvent>(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>(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<CircleCollider>(Vector2{0,0},5); + void SetUp() override { + ComponentManager & mgr = this->component_manager; + vector<reference_wrapper<Transform>> transforms + = mgr.get_components_by_id<Transform>(0); + if (transforms.empty()) { + auto entity = mgr.new_object("", "", Vector2{0, 0}, 0, 0); + entity.add_component<Rigidbody>(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<Transform>(0); + Transform & transform = transforms.front().get(); + transform.position.x = 0.0; + transform.position.y = 0.0; + transform.rotation = 0.0; + vector<reference_wrapper<Rigidbody>> rigidbodies + = mgr.get_components_by_id<Rigidbody>(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<std::reference_wrapper<Transform>> transforms - = mgr.get_components_by_id<Transform>(0); - Transform & transform = transforms.front().get(); + ComponentManager & mgr = this->component_manager; + vector<reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(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); +} |