diff --git a/lib/wal/sources/Entity/Entity.cpp b/lib/wal/sources/Entity/Entity.cpp index 56627a16..17ebd6c2 100644 --- a/lib/wal/sources/Entity/Entity.cpp +++ b/lib/wal/sources/Entity/Entity.cpp @@ -46,25 +46,43 @@ namespace WAL Entity &Entity::addComponent(const Component &component) { - if (this->hasComponent(typeid(component))) + if (this->hasComponent(typeid(component), false)) throw DuplicateError("A component of the type \"" + std::string(typeid(component).name()) + "\" already exists."); this->_components.emplace_back(component.clone(*this)); return *this; } - bool Entity::hasComponent(const std::type_info &type) const + bool Entity::hasComponent(const std::type_info &type, bool skipDisabled) const { auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) { return typeid(*cmp) == type; }); - return existing != this->_components.end(); + if (existing == this->_components.end()) + return false; + if (skipDisabled) + return !(*existing)->isDisabled(); + return true; } - bool Entity::hasComponent(const std::type_index &type) const + bool Entity::hasComponent(const std::type_index &type, bool skipDisabled) 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(); + if (existing == this->_components.end()) + return false; + if (skipDisabled) + return !(*existing)->isDisabled(); + return true; + } + + bool Entity::shouldDelete() const + { + return this->_shouldDelete; + } + + void Entity::scheduleDeletion() + { + this->_shouldDelete = true; } } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 8e157c6e..090c9baf 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -23,6 +23,8 @@ namespace WAL std::string _name; //! @brief Is this entity enabled? bool _disabled = false; + //! @brief Has this entity been scheduled for deletion? + bool _shouldDelete; //! @brief The list of the components of this entity std::vector> _components = {}; @@ -36,10 +38,14 @@ namespace WAL //! @brief Used if the entity is disabled bool isDisable() const; - //! @brief Disable this entity. void setDisable(bool disabled); + //! @brief Has this entity been scheduled for deletion? + bool shouldDelete() const; + //! @brief Schedule this entity for deletion + void scheduleDeletion(); + //! @brief Get a component of a specific type //! @throw NotFoundError if the component could not be found template @@ -55,21 +61,24 @@ namespace WAL } //! @brief Check if this entity has a component. + //! @param skipDisabled True if you want to skip disabled components (consider them non present), false otherwise. //! @tparam T The type of the component template - bool hasComponent() const + bool hasComponent(bool skipDisabled = true) const { const std::type_info &type = typeid(T); - return this->hasComponent(type); + return this->hasComponent(type, skipDisabled); } //! @brief Check if this entity has a component. + //! @param skipDisabled True if you want to skip disabled components (consider them non present), false otherwise. //! @param type The type of the component - bool hasComponent(const std::type_info &type) const; + bool hasComponent(const std::type_info &type, bool skipDisabled = true) const; //! @brief Check if this entity has a component. + //! @param skipDisabled True if you want to skip disabled components (consider them non present), false otherwise. //! @param type The type of the component - bool hasComponent(const std::type_index &type) const; + bool hasComponent(const std::type_index &type, bool skipDisabled = true) 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. @@ -77,7 +86,7 @@ namespace WAL template Entity &addComponent(Types &&...params) { - if (this->hasComponent()) + if (this->hasComponent(false)) throw DuplicateError("A component of the type \"" + std::string(typeid(T).name()) + "\" already exists."); this->_components.push_back(std::make_unique(*this, std::forward(params)...)); return *this;