Sdk/BaseSystem: Reuse EntityList

This commit is contained in:
Lynix 2017-04-21 01:02:46 +02:00
parent 53b92106ff
commit 203509f141
4 changed files with 54 additions and 26 deletions

View File

@ -9,6 +9,7 @@
#include <Nazara/Core/Bitset.hpp>
#include <NDK/Entity.hpp>
#include <NDK/EntityList.hpp>
#include <vector>
namespace Ndk
@ -33,7 +34,7 @@ namespace Ndk
bool Filters(const Entity* entity) const;
inline const std::vector<EntityHandle>& 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<EntityHandle> m_entities;
Nz::Bitset<Nz::UInt64> 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;

View File

@ -56,7 +56,7 @@ namespace Ndk
* \return A constant reference to the list of entities
*/
inline const std::vector<EntityHandle>& 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()

View File

@ -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<std::forward_iterator_tag, const EntityHandle>
class NDK_API EntityList::iterator : public std::iterator<std::forward_iterator_tag, const EntityHandle>
{
friend EntityList;

View File

@ -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);