From e5d3823d0c1fae208e79a47e084ed338c4e9feee Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 21 May 2021 16:51:18 +0200 Subject: [PATCH] Reworking dependencies --- lib/wal/sources/Component/Component.cpp | 5 ----- lib/wal/sources/Component/Component.hpp | 5 ----- lib/wal/sources/Entity/Entity.cpp | 8 ++++++++ lib/wal/sources/Entity/Entity.hpp | 4 ++++ lib/wal/sources/Scene/Scene.cpp | 5 ----- lib/wal/sources/Scene/Scene.hpp | 4 ---- .../sources/System/Movable/MovableSystem.cpp | 10 ++++++---- .../sources/System/Movable/MovableSystem.hpp | 4 +--- lib/wal/sources/System/System.cpp | 10 ++++++++++ lib/wal/sources/System/System.hpp | 9 ++++++--- lib/wal/sources/Wal.cpp | 18 ++++++++++++------ lib/wal/sources/Wal.hpp | 6 ++++++ 12 files changed, 53 insertions(+), 35 deletions(-) diff --git a/lib/wal/sources/Component/Component.cpp b/lib/wal/sources/Component/Component.cpp index f891352b..fb4e1279 100644 --- a/lib/wal/sources/Component/Component.cpp +++ b/lib/wal/sources/Component/Component.cpp @@ -20,11 +20,6 @@ namespace WAL this->_disabled = disabled; } - const std::vector &Component::getDependencies() const - { - return this->_dependencies; - } - void Component::onStart() { //TODO handle events here diff --git a/lib/wal/sources/Component/Component.hpp b/lib/wal/sources/Component/Component.hpp index 410a026c..b2441383 100644 --- a/lib/wal/sources/Component/Component.hpp +++ b/lib/wal/sources/Component/Component.hpp @@ -22,8 +22,6 @@ namespace WAL protected: //! @brief The entity that own this component Entity &_entity; - //! @brief The list of dependencies of this component. - std::vector _dependencies; //! @brief A component can't be instantiated, it should be derived. explicit Component(Entity &entity); @@ -44,9 +42,6 @@ namespace WAL //! @brief Disable this component. void setDisable(bool disabled); - //! @brief Get the dependencies of this component. - const std::vector &getDependencies() const; - //! @brief The entity or this component has just been enabled. virtual void onStart(); diff --git a/lib/wal/sources/Entity/Entity.cpp b/lib/wal/sources/Entity/Entity.cpp index e5ca8b29..9a59447b 100644 --- a/lib/wal/sources/Entity/Entity.cpp +++ b/lib/wal/sources/Entity/Entity.cpp @@ -59,4 +59,12 @@ namespace WAL }); return existing != this->_components.end(); } + + bool Entity::hasComponent(const std::type_index &type) const + { + auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) { + return std::type_index(typeid(*cmp)) == type; + }); + return existing != this->_components.end(); + } } \ No newline at end of file diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 7a902ecd..5f4b21ca 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -67,6 +67,10 @@ namespace WAL //! @param type The type of the component bool hasComponent(const std::type_info &type) const; + //! @brief Check if this entity has a component. + //! @param type The type of the component + bool hasComponent(const std::type_index &type) const; + //! @brief Add a component to this entity. The component is constructed in place. //! @throw DuplicateError is thrown if a component with the same type already exist. //! @return This entity is returned diff --git a/lib/wal/sources/Scene/Scene.cpp b/lib/wal/sources/Scene/Scene.cpp index 5c0ef26e..b0a3b3a7 100644 --- a/lib/wal/sources/Scene/Scene.cpp +++ b/lib/wal/sources/Scene/Scene.cpp @@ -10,10 +10,5 @@ namespace WAL { return this->_entities; } - - void Scene::removeAll(std::function &predicate) - { -// this->_entities.erase(std::remove_if(this->_entities.begin(), this->_entities.end(), predicate), this->_entities.end()); - } } diff --git a/lib/wal/sources/Scene/Scene.hpp b/lib/wal/sources/Scene/Scene.hpp index 23ee5f63..00329172 100644 --- a/lib/wal/sources/Scene/Scene.hpp +++ b/lib/wal/sources/Scene/Scene.hpp @@ -30,10 +30,6 @@ namespace WAL return *this; } - //! @brief Remove every entity of this scene that matches the given predicate - //! @param predicate The predicate used to filer entities - void removeAll(std::function &predicate); - //! @brief A default constructor Scene() = default; //! @brief A scene is copy constructable diff --git a/lib/wal/sources/System/Movable/MovableSystem.cpp b/lib/wal/sources/System/Movable/MovableSystem.cpp index 600f7c8e..7bd56fb0 100644 --- a/lib/wal/sources/System/Movable/MovableSystem.cpp +++ b/lib/wal/sources/System/Movable/MovableSystem.cpp @@ -9,10 +9,12 @@ namespace WAL { - const std::type_info &MovableSystem::getComponent() const - { - return typeid(MovableComponent); - } + MovableSystem::MovableSystem() + : System({ + typeid(MovableComponent), + typeid(PositionComponent) + }) + {} void MovableSystem::onFixedUpdate(Entity &entity) { diff --git a/lib/wal/sources/System/Movable/MovableSystem.hpp b/lib/wal/sources/System/Movable/MovableSystem.hpp index 400c7830..f7ec3c3c 100644 --- a/lib/wal/sources/System/Movable/MovableSystem.hpp +++ b/lib/wal/sources/System/Movable/MovableSystem.hpp @@ -13,13 +13,11 @@ namespace WAL class MovableSystem : public System { public: - //! @inherit - const std::type_info &getComponent() const override; //! @inherit void onFixedUpdate(Entity &entity) override; //! @brief A default constructor - MovableSystem() = default; + MovableSystem(); //! @brief A movable system is copy constructable MovableSystem(const MovableSystem &) = default; //! @brief A default destructor diff --git a/lib/wal/sources/System/System.cpp b/lib/wal/sources/System/System.cpp index 82f7fe11..56814735 100644 --- a/lib/wal/sources/System/System.cpp +++ b/lib/wal/sources/System/System.cpp @@ -4,8 +4,13 @@ #include "System.hpp" +#include + namespace WAL { + System::System(std::vector dependencies) + : _dependencies(std::move(dependencies)) + {} void System::onUpdate(Entity &entity, std::chrono::nanoseconds dtime) {} @@ -15,4 +20,9 @@ namespace WAL void System::onSelfUpdate() {} + + const std::vector &System::getDependencies() const + { + return this->_dependencies; + } } \ No newline at end of file diff --git a/lib/wal/sources/System/System.hpp b/lib/wal/sources/System/System.hpp index 3813e216..5ab9b8a5 100644 --- a/lib/wal/sources/System/System.hpp +++ b/lib/wal/sources/System/System.hpp @@ -12,6 +12,9 @@ namespace WAL //! @brief A base system of WAL class System { + private: + //! @brief The list of dependencies of this system + std::vector _dependencies = {}; public: //! @brief A virtual, default, destructor virtual ~System() = default; @@ -19,8 +22,8 @@ namespace WAL System(System &&) = default; //! @brief Get the name of the component corresponding to this system. - virtual const std::type_info &getComponent() const = 0; - + const std::vector &getDependencies() const; + //! @brief Update the corresponding component of the given entity //! @param entity The entity to update. //! @param dtime The delta time. @@ -35,7 +38,7 @@ namespace WAL virtual void onSelfUpdate(); protected: //! @brief A system can't be instantiated, it should be derived. - System() = default; + explicit System(std::vector dependencies); //! @brief A system can't be instantiated, it should be derived. System(const System &) = default; //! @brief A system can't be instantiated, it should be derived. diff --git a/lib/wal/sources/Wal.cpp b/lib/wal/sources/Wal.cpp index e1598dd7..45d2caff 100644 --- a/lib/wal/sources/Wal.cpp +++ b/lib/wal/sources/Wal.cpp @@ -3,6 +3,7 @@ // #include +#include #include "Wal.hpp" using namespace std::chrono_literals; @@ -36,10 +37,8 @@ namespace WAL for (auto &system : this->_systems) { for (auto &entity : entities) { - const auto &cmp = system->getComponent(); - if (!entity.hasComponent(cmp)) + if (!Wal::_hasDependencies(entity, *system)) continue; - // TODO handle dependencies. system->onUpdate(entity, dtime); } system->onSelfUpdate(); @@ -52,12 +51,19 @@ namespace WAL for (auto &system : this->_systems) { for (auto &entity : entities) { - auto &cmp = system->getComponent(); - if (!entity.hasComponent(cmp)) + if (!Wal::_hasDependencies(entity, *system)) continue; - // TODO handle dependencies. system->onFixedUpdate(entity); } } } + + bool Wal::_hasDependencies(const Entity &entity, const System &system) + { + // TODO use an hashmap to cache results. + const auto &dependency = system.getDependencies(); + return std::ranges::all_of(dependency.begin(), dependency.end(), [&entity](const auto &dependency) { + return entity.hasComponent(dependency); + }); + } } \ No newline at end of file diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index 8b21a4c8..f1b61f6f 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -31,6 +31,12 @@ namespace WAL //! @brief Call the onFixedUpdate of every system with every component void _fixedUpdate(); + + //! @brief Check if an entity met a system's dependencies. + //! @param entity The entity to check + //! @param system The system that will list dependencies + //! @return True if all dependencies are met, false otherwise. + static bool _hasDependencies(const Entity &entity, const System &system); public: //! @brief The time between each fixed update. static std::chrono::nanoseconds timestep;