From ef492f3b5794f24951dc0b56b1d9c076ee08362e Mon Sep 17 00:00:00 2001 From: Bluub Date: Tue, 1 Jun 2021 09:55:49 +0200 Subject: [PATCH] adding a unit test to test with a movable entity --- .../Component/Movable/MovableComponent.hpp | 4 +- tests/CollisionTest.cpp | 43 ++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/sources/Component/Movable/MovableComponent.hpp b/sources/Component/Movable/MovableComponent.hpp index ba04f804..b9a997c9 100644 --- a/sources/Component/Movable/MovableComponent.hpp +++ b/sources/Component/Movable/MovableComponent.hpp @@ -15,9 +15,10 @@ namespace BBM private: //! @brief The acceleration of this entity. Vector3f _acceleration; + public: //! @brief The velocity of the entity. Vector3f _velocity; - public: + //! @brief Add an instant force to this entity. //! @param force The force to add to this entity's acceleration. The force is added instantly and in one go. void addForce(Vector3f force); @@ -35,6 +36,5 @@ namespace BBM MovableComponent &operator=(const MovableComponent &) = delete; friend class MovableSystem; - friend class CollisionSystem; }; } // namespace WAL \ No newline at end of file diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 6a9331df..7d296203 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -5,6 +5,8 @@ #include #include "Entity/Entity.hpp" #include "Component/Position/PositionComponent.hpp" +#include "Component/Movable/MovableComponent.hpp" +#include "System/Movable/MovableSystem.hpp" #include "System/Collision/CollisionSystem.hpp" #include "Wal.hpp" @@ -15,7 +17,7 @@ using namespace WAL; using namespace BBM; -TEST_CASE("Collsion test", "[Component][System]") +TEST_CASE("Collision test", "[Component][System]") { Wal wal; CollisionSystem collision(wal); @@ -55,4 +57,41 @@ TEST_CASE("Collsion test", "[Component][System]") REQUIRE(player.getComponent().position.x == 1.0); REQUIRE(player.getComponent().position.y == 1); REQUIRE(player.getComponent().position.z == 1); -} \ No newline at end of file +} + + +TEST_CASE("Collsion test with movable", "[Component][System]") +{ + Wal wal; + CollisionSystem collision(wal); + MovableSystem movable; + wal.scene = std::shared_ptr(new Scene); + wal.scene->addEntity("player") + .addComponent() + .addComponent([](Entity &actual, const Entity &) { + try { + auto &mov = actual.getComponent(); + mov._velocity = {0, 0, 0}; + } catch (std::exception &e) {}; + }, 5.0) + .addComponent(); + + wal.scene->addEntity("block") + .addComponent(0, 0, 0) + .addComponent(1); + Entity &entity = wal.scene->getEntities()[0]; + REQUIRE(entity.getComponent().position == Vector3f()); + + entity.getComponent().bound.x = 5; + entity.getComponent().bound.y = 5; + entity.getComponent().bound.z = 5; + + entity.getComponent().addForce({1, 1, 1}); + collision.onUpdate(entity, std::chrono::nanoseconds(1)); + collision.onFixedUpdate(entity); + movable.onUpdate(entity, std::chrono::nanoseconds(1)); + movable.onFixedUpdate(entity); + REQUIRE(entity.getComponent().position.x == 0.0); + REQUIRE(entity.getComponent().position.y == 0.0); + REQUIRE(entity.getComponent().position.z == 0.0); +} \ No newline at end of file