diff --git a/CMakeLists.txt b/CMakeLists.txt index 70d6a98e..fd96ef8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,34 +35,34 @@ set(SOURCES sources/Component/Keyboard/KeyboardComponent.hpp sources/Component/Health/HealthComponent.cpp sources/Component/Health/HealthComponent.hpp -# sources/System/Movable/MovableSystem.hpp -# sources/System/Movable/MovableSystem.cpp -# sources/System/Controllable/ControllableSystem.cpp -# sources/System/Controllable/ControllableSystem.hpp -# sources/System/Gamepad/GamepadSystem.cpp -# sources/System/Gamepad/GamepadSystem.hpp -# sources/System/Health/HealthSystem.cpp -# sources/System/Health/HealthSystem.hpp -# sources/System/Keyboard/KeyboardSystem.cpp -# sources/System/Keyboard/KeyboardSystem.hpp -# sources/System/Movable/MovableSystem.cpp -# sources/System/Movable/MovableSystem.hpp + sources/System/Movable/MovableSystem.hpp + sources/System/Movable/MovableSystem.cpp + sources/System/Controllable/ControllableSystem.cpp + sources/System/Controllable/ControllableSystem.hpp + sources/System/Gamepad/GamepadSystem.cpp + sources/System/Gamepad/GamepadSystem.hpp + sources/System/Health/HealthSystem.cpp + sources/System/Health/HealthSystem.hpp + sources/System/Keyboard/KeyboardSystem.cpp + sources/System/Keyboard/KeyboardSystem.hpp + sources/System/Movable/MovableSystem.cpp + sources/System/Movable/MovableSystem.hpp sources/Models/Vector3.hpp sources/Component/GridCentered/GridCenteredComponent.cpp sources/Component/GridCentered/GridCenteredComponent.hpp -# sources/System/GridCentered/GridCenteredSystem.cpp -# sources/System/GridCentered/GridCenteredSystem.hpp + sources/System/GridCentered/GridCenteredSystem.cpp + sources/System/GridCentered/GridCenteredSystem.hpp sources/Models/Vector2.hpp sources/Component/Renderer/Drawable2DComponent.hpp sources/Component/Renderer/Drawable3DComponent.hpp -# sources/System/Renderer/RenderSystem.hpp -# sources/System/Renderer/RenderSystem.cpp + sources/System/Renderer/RenderSystem.hpp + sources/System/Renderer/RenderSystem.cpp sources/Component/Renderer/CameraComponent.cpp sources/Component/Renderer/CameraComponent.hpp sources/Component/Animation/AnimationsComponent.cpp sources/Component/Animation/AnimationsComponent.hpp -# sources/System/Animation/AnimationsSystem.cpp -# sources/System/Animation/AnimationsSystem.hpp + sources/System/Animation/AnimationsSystem.cpp + sources/System/Animation/AnimationsSystem.hpp sources/Component/Collision/CollisionComponent.cpp sources/Component/Collision/CollisionComponent.hpp sources/System/Collision/CollisionSystem.hpp diff --git a/lib/wal/sources/System/System.hpp b/lib/wal/sources/System/System.hpp index c21de9d2..58f49970 100644 --- a/lib/wal/sources/System/System.hpp +++ b/lib/wal/sources/System/System.hpp @@ -12,6 +12,7 @@ #include "Wal.hpp" #include "View/View.hpp" #include "ISystem.hpp" +#include namespace WAL { @@ -50,7 +51,7 @@ namespace WAL //! @param dtime The delta time since the last call to this method. void update(std::chrono::nanoseconds dtime) final { - for (auto entity : this->getView()) + for (auto &entity : this->getView()) this->onUpdate(entity, dtime); this->onSelfUpdate(); } @@ -59,7 +60,7 @@ namespace WAL //! @remark This should be used for Physics, AI and everything that could be imprecise due to float rounding. void fixedUpdate() final { - for (auto entity : this->getView()) + for (auto &entity : this->getView()) this->onFixedUpdate(entity); } protected: diff --git a/lib/wal/sources/View/View.hpp b/lib/wal/sources/View/View.hpp index 1c3ba2d9..cbee789e 100644 --- a/lib/wal/sources/View/View.hpp +++ b/lib/wal/sources/View/View.hpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include "Entity/Entity.hpp" namespace WAL @@ -43,6 +45,12 @@ namespace WAL { return std::get>(this->_value); } + + template + auto &get() + { + return std::get(this->_value); + } }; template @@ -50,18 +58,19 @@ namespace WAL { private: It _it; + std::optional> _entity; public: - ViewEntity operator*() + ViewEntity &operator*() { - ViewEntity entity(*this->_it); - return entity; + this->_entity.emplace(*this->_it); + return this->_entity.value(); } - ViewEntity operator->() + ViewEntity *operator->() { - ViewEntity entity(*this->_it); - return entity; + this->_entity.emplace(*this->_it); + return &this->_entity; } ViewIterator &operator++() @@ -88,7 +97,8 @@ namespace WAL } explicit ViewIterator(It current) - : _it(current) + : _it(current), + _entity(std::nullopt) {} }; @@ -129,7 +139,22 @@ namespace WAL iterator end() { - return iterator(this->_entities.begin()); + return iterator(this->_entities.end()); + } + + std::size_t size() const + { + return this->_entities.size(); + } + + ViewEntity front() + { + return *iterator(this->_entities.begin()); + } + + ViewEntity back() + { + return *iterator(--this->_entities.end()); } const std::vector &getTypes() const override @@ -137,14 +162,21 @@ namespace WAL return this->_types; } - void emplace_back(Entity &) override + void emplace_back(Entity &entity) override { - + auto tuple = std::make_tuple(entity.tryGetComponent()...); + if (std::apply([](const auto *...component) {return ((component == nullptr) || ...);}, tuple)) + return; + std::apply([&](auto *...component) { + this->_entities.emplace_back(entity, *component...); + }, tuple); } - void erase(const Entity &) override + void erase(const Entity &entity) override { - + this->_entities.erase(std::remove_if(this->_entities.begin(), this->_entities.end(), [&entity](const auto &ref){ + return &std::get<0>(ref).get() == &entity; + })); } //! @brief Construct a view from a list of entities. @@ -152,14 +184,8 @@ namespace WAL explicit View(std::list &scene) { this->_types = {typeid(Components)...}; - for (auto &entity : scene) { - auto tuple = std::make_tuple(entity.tryGetComponent()...); - if (std::apply([](const auto *...component) {return ((component == nullptr) || ...);}, tuple)) - continue; - std::apply([&](auto *...component) { - this->_entities.emplace_back(entity, *component...); - }, tuple); - } + for (auto &entity : scene) + this->emplace_back(entity); } //! @brief Copying a view is not possible since a view must be managed by a scene. @@ -169,4 +195,24 @@ namespace WAL //! @brief A view is not assignable. View &operator=(const View &) = delete; }; +} + +namespace std +{ + template + struct tuple_size<::WAL::ViewEntity> + : public std::integral_constant + {}; + + template + struct tuple_element<0, ::WAL::ViewEntity> + { + using type = WAL::Entity &; + }; + + template + struct tuple_element> + { + using type = typename std::tuple_element>::type; + }; } \ No newline at end of file diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index ff933fff..6dbce849 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -4,33 +4,28 @@ #include #include -//#include "System/Movable/MovableSystem.hpp" -//#include "System/Renderer/RenderSystem.hpp" +#include "System/Movable/MovableSystem.hpp" +#include "System/Renderer/RenderSystem.hpp" #include #include -#include -//#include #include -//#include -//#include +#include +#include #include #include #include #include #include -//#include -//#include "Models/Vector2.hpp" +#include #include "Component/Renderer/CameraComponent.hpp" -//#include "Component/Renderer/Drawable2DComponent.hpp" #include "Component/Renderer/Drawable3DComponent.hpp" #include "Runner.hpp" #include "Models/GameState.hpp" #include #include "Component/Animation/AnimationsComponent.hpp" -//#include "System/Animation/AnimationsSystem.hpp" +#include "System/Animation/AnimationsSystem.hpp" #include "Map/Map.hpp" -namespace RAY2D = RAY::Drawables::Drawables2D; namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM @@ -47,22 +42,22 @@ namespace BBM void addSystems(WAL::Wal &wal) { - wal.addSystem(); - // wal.addSystem() -// .addSystem() -// .addSystem() -// .addSystem() -// .addSystem(); + wal.addSystem() + .addSystem() + .addSystem() + .addSystem() + .addSystem(); } void enableRaylib(WAL::Wal &wal) { RAY::TraceLog::setLevel(LOG_WARNING); RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE); -// wal.addSystem(window); + wal.addSystem() + .addSystem(window); } - std::shared_ptr loadGameScene(WAL::Wal &wal) + std::shared_ptr loadGameScene() { auto scene = std::make_shared(); scene->addEntity("player") @@ -99,7 +94,7 @@ namespace BBM WAL::Wal wal; addSystems(wal); enableRaylib(wal); - wal.scene = loadGameScene(wal); + wal.scene = loadGameScene(); try { wal.run(updateState); diff --git a/sources/System/Animation/AnimationsSystem.cpp b/sources/System/Animation/AnimationsSystem.cpp index afc22e31..5aaa4597 100644 --- a/sources/System/Animation/AnimationsSystem.cpp +++ b/sources/System/Animation/AnimationsSystem.cpp @@ -15,10 +15,10 @@ namespace BBM : System(wal) {} - void AnimationsSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) + void AnimationsSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds) { - auto &model = entity.getComponent(); - auto &anim = entity.getComponent(); + auto &model = entity.get(); + auto &anim = entity.get(); if (anim.isDisabled()) return; diff --git a/sources/System/Animation/AnimationsSystem.hpp b/sources/System/Animation/AnimationsSystem.hpp index b17b1935..6ace28d8 100644 --- a/sources/System/Animation/AnimationsSystem.hpp +++ b/sources/System/Animation/AnimationsSystem.hpp @@ -14,7 +14,7 @@ namespace BBM { public: //! @inherit - void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds) override; + void onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds) override; //! @brief A default constructor explicit AnimationsSystem(WAL::Wal &wal); diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 79351729..4e992bda 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -28,8 +28,8 @@ namespace BBM auto &posA = entity.get(); auto &col = entity.get(); Vector3f position = posA.position; -// if (entity.hasComponent(typeid(MovableComponent))) -// position += entity.getComponent().getVelocity(); + if (auto *movable = entity->tryGetComponent()) + position += movable->getVelocity(); Vector3f minA = Vector3f::min(position, position + col.bound); Vector3f maxA = Vector3f::max(position, position + col.bound); for (auto other : this->getView()) { diff --git a/sources/System/Controllable/ControllableSystem.cpp b/sources/System/Controllable/ControllableSystem.cpp index b028a2c7..f14c46b3 100644 --- a/sources/System/Controllable/ControllableSystem.cpp +++ b/sources/System/Controllable/ControllableSystem.cpp @@ -14,10 +14,10 @@ namespace BBM : System(wal) {} - void ControllableSystem::onFixedUpdate(WAL::Entity &entity) + void ControllableSystem::onFixedUpdate(WAL::ViewEntity &entity) { - auto &controllable = entity.getComponent(); - auto &movable = entity.getComponent(); + auto &controllable = entity.get(); + auto &movable = entity.get(); Vector2f move = controllable.move.normalized() * ControllableSystem::speed; movable.addForce(Vector3f(move.x, controllable.jump, move.y)); diff --git a/sources/System/Controllable/ControllableSystem.hpp b/sources/System/Controllable/ControllableSystem.hpp index 042b17df..25a9e837 100644 --- a/sources/System/Controllable/ControllableSystem.hpp +++ b/sources/System/Controllable/ControllableSystem.hpp @@ -19,7 +19,7 @@ namespace BBM static constexpr const float speed = .25f; //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit ControllableSystem(WAL::Wal &wal); diff --git a/sources/System/Gamepad/GamepadSystem.cpp b/sources/System/Gamepad/GamepadSystem.cpp index 9392dc02..f6d67d99 100644 --- a/sources/System/Gamepad/GamepadSystem.cpp +++ b/sources/System/Gamepad/GamepadSystem.cpp @@ -17,10 +17,10 @@ namespace BBM : System(wal) {} - void GamepadSystem::onFixedUpdate(WAL::Entity &entity) + void GamepadSystem::onFixedUpdate(WAL::ViewEntity &entity) { - const auto &gamepadComponent = entity.getComponent(); - auto &controllable = entity.getComponent(); + const auto &gamepadComponent = entity.get(); + auto &controllable = entity.get(); Gamepad gamepad(gamepadComponent.getID()); const std::map keyPressedMap = { diff --git a/sources/System/Gamepad/GamepadSystem.hpp b/sources/System/Gamepad/GamepadSystem.hpp index 8daa5e06..61efdd5e 100644 --- a/sources/System/Gamepad/GamepadSystem.hpp +++ b/sources/System/Gamepad/GamepadSystem.hpp @@ -16,7 +16,7 @@ namespace BBM { public: //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit GamepadSystem(WAL::Wal &wal); diff --git a/sources/System/GridCentered/GridCenteredSystem.cpp b/sources/System/GridCentered/GridCenteredSystem.cpp index d6337c3a..3c86175b 100644 --- a/sources/System/GridCentered/GridCenteredSystem.cpp +++ b/sources/System/GridCentered/GridCenteredSystem.cpp @@ -12,10 +12,10 @@ namespace BBM : System(wal) {} - void GridCenteredSystem::onFixedUpdate(WAL::Entity &entity) + void GridCenteredSystem::onFixedUpdate(WAL::ViewEntity &entity) { - auto &grid = entity.getComponent(); - auto &movement = entity.getComponent(); + auto &grid = entity.get(); + auto &movement = entity.get(); // movement.addForce(grid.force * ) } } \ No newline at end of file diff --git a/sources/System/GridCentered/GridCenteredSystem.hpp b/sources/System/GridCentered/GridCenteredSystem.hpp index 3af49ea1..6ab97339 100644 --- a/sources/System/GridCentered/GridCenteredSystem.hpp +++ b/sources/System/GridCentered/GridCenteredSystem.hpp @@ -13,7 +13,7 @@ namespace BBM class GridCenteredSystem : public WAL::System { public: - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit GridCenteredSystem(WAL::Wal &wal); diff --git a/sources/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp index e9aaa155..29e0db1f 100644 --- a/sources/System/Health/HealthSystem.cpp +++ b/sources/System/Health/HealthSystem.cpp @@ -14,9 +14,9 @@ namespace BBM : System(wal) {} - void HealthSystem::onFixedUpdate(WAL::Entity &entity) + void HealthSystem::onFixedUpdate(WAL::ViewEntity &entity) { - auto &health = entity.getComponent(); + auto &health = entity.get(); if (health.getHealthPoint() == 0) health.onDeath(entity); diff --git a/sources/System/Health/HealthSystem.hpp b/sources/System/Health/HealthSystem.hpp index fe8bb1ab..f1cd2397 100644 --- a/sources/System/Health/HealthSystem.hpp +++ b/sources/System/Health/HealthSystem.hpp @@ -15,7 +15,7 @@ namespace BBM { public: //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit HealthSystem(WAL::Wal &wal); diff --git a/sources/System/Keyboard/KeyboardSystem.cpp b/sources/System/Keyboard/KeyboardSystem.cpp index 044373b4..ccc207ab 100644 --- a/sources/System/Keyboard/KeyboardSystem.cpp +++ b/sources/System/Keyboard/KeyboardSystem.cpp @@ -3,11 +3,9 @@ // Edited by Benjamin Henry on 2021-05-20. // -#include #include "KeyboardSystem.hpp" #include "Component/Keyboard/KeyboardComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" -#include "Entity/Entity.hpp" #include "Controllers/Keyboard.hpp" using Keyboard = RAY::Controller::Keyboard; @@ -18,10 +16,10 @@ namespace BBM : System(wal) {} - void KeyboardSystem::onFixedUpdate(WAL::Entity &entity) + void KeyboardSystem::onFixedUpdate(WAL::ViewEntity &entity) { - const auto &keyboard = entity.getComponent(); - auto &controllable = entity.getComponent(); + const auto &keyboard = entity.get(); + auto &controllable = entity.get(); const std::map keyPressedMap = { {keyboard.keyJump, controllable.jump}, diff --git a/sources/System/Keyboard/KeyboardSystem.hpp b/sources/System/Keyboard/KeyboardSystem.hpp index 56977386..cf7309e1 100644 --- a/sources/System/Keyboard/KeyboardSystem.hpp +++ b/sources/System/Keyboard/KeyboardSystem.hpp @@ -17,7 +17,7 @@ namespace BBM { public: //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit KeyboardSystem(WAL::Wal &wal); diff --git a/sources/System/Movable/MovableSystem.cpp b/sources/System/Movable/MovableSystem.cpp index c6d86457..eff5a79a 100644 --- a/sources/System/Movable/MovableSystem.cpp +++ b/sources/System/Movable/MovableSystem.cpp @@ -12,10 +12,10 @@ namespace BBM : System(wal) {} - void MovableSystem::onFixedUpdate(WAL::Entity &entity) + void MovableSystem::onFixedUpdate(WAL::ViewEntity &entity) { - auto &movable = entity.getComponent(); - auto &position = entity.getComponent(); + auto &movable = entity.get(); + auto &position = entity.get(); position.position += movable._velocity; movable._velocity = movable._acceleration; diff --git a/sources/System/Movable/MovableSystem.hpp b/sources/System/Movable/MovableSystem.hpp index 7032d97e..e109da13 100644 --- a/sources/System/Movable/MovableSystem.hpp +++ b/sources/System/Movable/MovableSystem.hpp @@ -17,7 +17,7 @@ namespace BBM { public: //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief A default constructor explicit MovableSystem(WAL::Wal &wal); diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index 16190445..1607183e 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -52,10 +52,10 @@ namespace BBM this->_window.endDrawing(); } - void RenderSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) + void RenderSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) { - const auto &pos = entity.getComponent(); - const auto &cam = entity.getComponent(); + const auto &pos = entity.get(); + const auto &cam = entity.get(); _camera.setPosition(pos.position); _camera.setTarget(cam.target); } diff --git a/sources/System/Renderer/RenderSystem.hpp b/sources/System/Renderer/RenderSystem.hpp index 0ba6c4d3..9c5c4c27 100644 --- a/sources/System/Renderer/RenderSystem.hpp +++ b/sources/System/Renderer/RenderSystem.hpp @@ -27,7 +27,7 @@ namespace BBM void onSelfUpdate() override; //! @inherit - void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override; + void onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) override; //! @brief ctor RenderSystem(WAL::Wal &wal, RAY::Window &window); diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 77188df1..ce153238 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -39,8 +39,8 @@ TEST_CASE("Collision test", "[Component][System]") entity.getComponent().bound.y = 5; entity.getComponent().bound.z = 5; - collision.onUpdate(entity, std::chrono::nanoseconds(1)); - collision.onFixedUpdate(entity); + collision.update(std::chrono::nanoseconds(1)); + collision.fixedUpdate(); REQUIRE(entity.getComponent().position.x == 0.0); REQUIRE(entity.getComponent().position.y == 0.0); REQUIRE(entity.getComponent().position.z == 0.0); @@ -49,9 +49,9 @@ TEST_CASE("Collision test", "[Component][System]") .addComponent(2,2,2) .addComponent(1); Entity &player = wal.scene->getEntities().front(); - collision.onUpdate(entity, std::chrono::nanoseconds(1)); + collision.update(std::chrono::nanoseconds(1)); REQUIRE(player.hasComponent(typeid(PositionComponent))); - collision.onFixedUpdate(player); + collision.fixedUpdate(); REQUIRE(wal.scene->getEntities().size() == 2); REQUIRE(player.hasComponent(typeid(PositionComponent))); REQUIRE(player.getComponent().position.x == 1.0); @@ -87,10 +87,10 @@ TEST_CASE("Collsion test with movable", "[Component][System]") 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); + collision.update(std::chrono::nanoseconds(1)); + collision.fixedUpdate(); + movable.update(std::chrono::nanoseconds(1)); + movable.fixedUpdate(); REQUIRE(entity.getComponent().position.x == 0.0); REQUIRE(entity.getComponent().position.y == 0.0); REQUIRE(entity.getComponent().position.z == 0.0); diff --git a/tests/MoveTests.cpp b/tests/MoveTests.cpp index ac035c86..b19a68fc 100644 --- a/tests/MoveTests.cpp +++ b/tests/MoveTests.cpp @@ -20,32 +20,32 @@ using namespace BBM; TEST_CASE("Move test", "[Component][System]") { Wal wal; - Scene scene; - scene.addEntity("player") + wal.scene = std::make_shared(); + wal.scene->addEntity("player") .addComponent() .addComponent() .addComponent(); - Entity &entity = *scene.getEntities().begin(); + Entity &entity = wal.scene->getEntities().front(); REQUIRE(entity.getComponent().position == Vector3f()); entity.getComponent().move = Vector2f(1, 1); ControllableSystem controllable(wal); - controllable.onUpdate(entity, std::chrono::nanoseconds(1)); - controllable.onFixedUpdate(entity); + controllable.update(std::chrono::nanoseconds(1)); + controllable.fixedUpdate(); REQUIRE(entity.getComponent()._acceleration.x > 0); REQUIRE(entity.getComponent()._acceleration.z > 0); MovableSystem movable(wal); - movable.onUpdate(entity, std::chrono::nanoseconds(1)); - movable.onFixedUpdate(entity); + movable.update(std::chrono::nanoseconds(1)); + movable.fixedUpdate(); REQUIRE(entity.getComponent()._velocity.x > 0); REQUIRE(entity.getComponent()._velocity.z > 0); REQUIRE(entity.getComponent()._acceleration.x == 0); REQUIRE(entity.getComponent()._acceleration.z == 0); - movable.onUpdate(entity, std::chrono::nanoseconds(1)); - movable.onFixedUpdate(entity); + movable.update(std::chrono::nanoseconds(1)); + movable.fixedUpdate(); REQUIRE(entity.getComponent().position.x > 0); REQUIRE(entity.getComponent().position.z > 0); diff --git a/tests/ViewTest.cpp b/tests/ViewTest.cpp index 194e04a6..569d42ad 100644 --- a/tests/ViewTest.cpp +++ b/tests/ViewTest.cpp @@ -22,7 +22,7 @@ TEST_CASE("View creation", "[View]") REQUIRE(scene.view().size() == 2); REQUIRE(scene.view().size() == 1); Entity &entity = *scene.getEntities().begin(); - Entity &firstView = *scene.view().entities.begin(); + Entity &firstView = scene.view().front(); REQUIRE(&entity == &firstView); } @@ -62,23 +62,65 @@ TEST_CASE("View cache switch", "[View]") .addComponent(); REQUIRE(&view == &scene.view()); - REQUIRE(view.entities.begin()->get().getName() == "player"); - REQUIRE(scene2.view().entities.begin()->get().getName() == "box"); + REQUIRE(view.front()->getName() == "player"); + REQUIRE(scene2.view().front()->getName() == "box"); } -//TEST_CASE("View iteration", "[View]") -//{ -// Scene scene; -// scene.addEntity("player") -// .addComponent() -// .addComponent(); -// scene.addEntity("Box") -// .addComponent(); -// int i = 0; -// for (auto &entity : scene.view()) { -// if (i == 0) -// REQUIRE(entity.getName() == "player"); -// else -// REQUIRE(entity.getName() == "Box"); -// } -//} \ No newline at end of file +TEST_CASE("View entity iteration", "[View]") +{ + Scene scene; + scene.addEntity("player") + .addComponent() + .addComponent(); + scene.addEntity("Box") + .addComponent(); + int i = 0; + for (Entity &entity : scene.view()) { + if (i == 0) + REQUIRE(entity.getName() == "player"); + else + REQUIRE(entity.getName() == "Box"); + i++; + } + REQUIRE(i == 2); +} + +TEST_CASE("ViewEntity<> iteration", "[View]") +{ + Scene scene; + scene.addEntity("player") + .addComponent(1, 1, 1) + .addComponent(); + scene.addEntity("Box") + .addComponent(1, 1, 1); + int i = 0; + for (auto entity : scene.view()) { + if (i == 0) + REQUIRE(entity->getName() == "player"); + else + REQUIRE(entity->getName() == "Box"); + REQUIRE(entity.get().position == Vector3f(1, 1, 1)); + i++; + } + REQUIRE(i == 2); +} + +TEST_CASE("View [entity, component] iteration", "[View]") +{ + Scene scene; + scene.addEntity("player") + .addComponent(1, 1, 1) + .addComponent(); + scene.addEntity("Box") + .addComponent(1, 1, 1); + int i = 0; + for (auto &[entity, position] : scene.view()) { + if (i == 0) + REQUIRE(entity.getName() == "player"); + else + REQUIRE(entity.getName() == "Box"); + REQUIRE(position.position == Vector3f(1, 1, 1)); + i++; + } + REQUIRE(i == 2); +} \ No newline at end of file