diff --git a/SDK/include/NDK/BaseSystem.hpp b/SDK/include/NDK/BaseSystem.hpp index a323f2952..99dcbf768 100644 --- a/SDK/include/NDK/BaseSystem.hpp +++ b/SDK/include/NDK/BaseSystem.hpp @@ -9,6 +9,7 @@ #include #include +#include #include namespace Ndk @@ -33,7 +34,7 @@ namespace Ndk bool Filters(const Entity* entity) const; - inline const std::vector& GetEntities() const; + inline const EntityList& GetEntities() const; inline SystemIndex GetIndex() const; inline int GetUpdateOrder() const; inline float GetUpdateRate() const; @@ -84,12 +85,11 @@ namespace Ndk static inline bool Initialize(); static inline void Uninitialize(); - std::vector m_entities; - Nz::Bitset m_entityBits; Nz::Bitset<> m_excludedComponents; mutable Nz::Bitset<> m_filterResult; Nz::Bitset<> m_requiredAnyComponents; Nz::Bitset<> m_requiredComponents; + EntityList m_entities; SystemIndex m_systemIndex; World* m_world; bool m_updateEnabled; diff --git a/SDK/include/NDK/BaseSystem.inl b/SDK/include/NDK/BaseSystem.inl index e949d254b..5969dd8e3 100644 --- a/SDK/include/NDK/BaseSystem.inl +++ b/SDK/include/NDK/BaseSystem.inl @@ -56,7 +56,7 @@ namespace Ndk * \return A constant reference to the list of entities */ - inline const std::vector& BaseSystem::GetEntities() const + inline const EntityList& BaseSystem::GetEntities() const { return m_entities; } @@ -121,10 +121,7 @@ namespace Ndk inline bool BaseSystem::HasEntity(const Entity* entity) const { - if (!entity) - return false; - - return m_entityBits.UnboundedTest(entity->GetId()); + return m_entities.Has(entity); } /*! @@ -288,9 +285,7 @@ namespace Ndk { NazaraAssert(entity, "Invalid entity"); - m_entities.emplace_back(entity); - m_entityBits.UnboundedSet(entity->GetId(), true); - + m_entities.Insert(entity); entity->RegisterSystem(m_systemIndex); OnEntityAdded(entity); @@ -308,14 +303,7 @@ namespace Ndk { NazaraAssert(entity, "Invalid entity"); - auto it = std::find(m_entities.begin(), m_entities.end(), *entity); - NazaraAssert(it != m_entities.end(), "Entity is not part of this system"); - - // To avoid moving a lot of handles, we swap and pop - std::swap(*it, m_entities.back()); - m_entities.pop_back(); // We get it out of the vector - - m_entityBits.Reset(entity->GetId()); + m_entities.Remove(entity); entity->UnregisterSystem(m_systemIndex); OnEntityRemoved(entity); // And we alert our callback @@ -360,7 +348,7 @@ namespace Ndk } /*! - * \brief Uninitializes the BaseSystem + * \brief Uninitialize the BaseSystem */ inline void BaseSystem::Uninitialize() diff --git a/SDK/include/NDK/EntityList.hpp b/SDK/include/NDK/EntityList.hpp index 22a632e65..76097f87b 100644 --- a/SDK/include/NDK/EntityList.hpp +++ b/SDK/include/NDK/EntityList.hpp @@ -21,6 +21,8 @@ namespace Ndk using size_type = std::size_t; inline EntityList(); + inline EntityList(const EntityList& entityList); + inline EntityList(EntityList&& entityList) noexcept; ~EntityList() = default; inline void Clear(); @@ -33,11 +35,14 @@ namespace Ndk inline void Remove(Entity* entity); // STL API - inline iterator begin(); + inline iterator begin() const; inline bool empty() const; - inline iterator end(); + inline iterator end() const; inline size_type size() const; + inline EntityList& operator=(const EntityList& entityList); + inline EntityList& operator=(EntityList&& entityList) noexcept; + private: inline std::size_t FindNext(std::size_t currentId) const; inline World* GetWorld() const; @@ -46,7 +51,7 @@ namespace Ndk World* m_world; }; - class EntityList::iterator : public std::iterator + class NDK_API EntityList::iterator : public std::iterator { friend EntityList; diff --git a/SDK/include/NDK/EntityList.inl b/SDK/include/NDK/EntityList.inl index 6d9447a7f..8d639b762 100644 --- a/SDK/include/NDK/EntityList.inl +++ b/SDK/include/NDK/EntityList.inl @@ -16,13 +16,32 @@ namespace Ndk */ /*! - * \brief Clears the set from every entities + * \brief Construct a new entity list */ inline EntityList::EntityList() : m_world(nullptr) { } + /*! + * \brief Construct a new entity list by copying another one + */ + inline EntityList::EntityList(const EntityList& entityList) : + m_entityBits(entityList.m_entityBits), + m_world(entityList.m_world) + { + } + + /*! + * \brief Construct a new entity list by moving a list into this one + */ + inline EntityList::EntityList(EntityList&& entityList) noexcept : + m_entityBits(std::move(entityList.m_entityBits)), + m_world(entityList.m_world) + { + } + + /*! * \brief Clears the set from every entities * @@ -93,7 +112,7 @@ namespace Ndk } // STL Interface - inline EntityList::iterator EntityList::begin() + inline EntityList::iterator EntityList::begin() const { return EntityList::iterator(this, m_entityBits.FindFirst()); } @@ -103,7 +122,7 @@ namespace Ndk return m_entityBits.TestAny(); } - inline EntityList::iterator EntityList::end() + inline EntityList::iterator EntityList::end() const { return EntityList::iterator(this, m_entityBits.npos); } @@ -113,6 +132,22 @@ namespace Ndk return m_entityBits.Count(); } + inline EntityList& EntityList::operator=(const EntityList& entityList) + { + m_entityBits = entityList.m_entityBits; + m_world = entityList.m_world; + + return *this; + } + + inline EntityList& EntityList::operator=(EntityList && entityList) noexcept + { + m_entityBits = std::move(entityList.m_entityBits); + m_world = entityList.m_world; + + return *this; + } + inline std::size_t EntityList::FindNext(std::size_t currentId) const { return m_entityBits.FindNext(currentId);