Sdk/World: Use a Bitset to store free entity id

This commit is contained in:
Jérôme Leclercq 2018-02-08 15:15:56 +01:00
parent 555817e3ee
commit 6161b1a751
4 changed files with 12 additions and 8 deletions

View File

@ -95,6 +95,7 @@ Nazara Development Kit:
- Add ConstraintComponent2D class - Add ConstraintComponent2D class
- Fix CollisionComponent3D initialization (teleportation to their real coordinates) which could sometimes mess up the physics scene. - Fix CollisionComponent3D initialization (teleportation to their real coordinates) which could sometimes mess up the physics scene.
- ⚠️ Renamed World::Update() to World::Refresh() for more clarity and to differentiate it from World::Update(elapsedTime) - ⚠️ Renamed World::Update() to World::Refresh() for more clarity and to differentiate it from World::Update(elapsedTime)
- World entity ids are now reused from lowest to highest (they were previously reused in reverse order of death)
# 0.4: # 0.4:

View File

@ -96,9 +96,9 @@ namespace Ndk
std::vector<EntityBlock> m_entities; std::vector<EntityBlock> m_entities;
std::vector<EntityBlock*> m_entityBlocks; std::vector<EntityBlock*> m_entityBlocks;
std::vector<std::unique_ptr<EntityBlock>> m_waitingEntities; std::vector<std::unique_ptr<EntityBlock>> m_waitingEntities;
std::vector<EntityId> m_freeIdList;
EntityList m_aliveEntities; EntityList m_aliveEntities;
Nz::Bitset<Nz::UInt64> m_dirtyEntities; Nz::Bitset<Nz::UInt64> m_dirtyEntities;
Nz::Bitset<Nz::UInt64> m_freeEntityIds;
Nz::Bitset<Nz::UInt64> m_killedEntities; Nz::Bitset<Nz::UInt64> m_killedEntities;
bool m_orderedSystemsUpdated; bool m_orderedSystemsUpdated;
}; };

View File

@ -288,7 +288,7 @@ namespace Ndk
m_aliveEntities = std::move(world.m_aliveEntities); m_aliveEntities = std::move(world.m_aliveEntities);
m_dirtyEntities = std::move(world.m_dirtyEntities); m_dirtyEntities = std::move(world.m_dirtyEntities);
m_entityBlocks = std::move(world.m_entityBlocks); m_entityBlocks = std::move(world.m_entityBlocks);
m_freeIdList = std::move(world.m_freeIdList); m_freeEntityIds = std::move(world.m_freeEntityIds);
m_killedEntities = std::move(world.m_killedEntities); m_killedEntities = std::move(world.m_killedEntities);
m_orderedSystems = std::move(world.m_orderedSystems); m_orderedSystems = std::move(world.m_orderedSystems);
m_orderedSystemsUpdated = world.m_orderedSystemsUpdated; m_orderedSystemsUpdated = world.m_orderedSystemsUpdated;

View File

@ -61,11 +61,14 @@ namespace Ndk
{ {
EntityId id; EntityId id;
EntityBlock* entBlock; EntityBlock* entBlock;
if (!m_freeIdList.empty())
std::size_t freeEntityId = m_freeEntityIds.FindFirst();
if (freeEntityId != m_freeEntityIds.npos)
{ {
// We get an identifier // We get an identifier
id = m_freeIdList.back(); m_freeEntityIds.Reset(freeEntityId); //< Remove id from free entity id
m_freeIdList.pop_back();
id = static_cast<EntityId>(freeEntityId);
entBlock = &m_entities[id]; entBlock = &m_entities[id];
entBlock->handle.Reset(&entBlock->entity); //< Reset handle (as it was reset when entity got destroyed) entBlock->handle.Reset(&entBlock->entity); //< Reset handle (as it was reset when entity got destroyed)
@ -81,7 +84,7 @@ namespace Ndk
{ {
NazaraAssert(m_waitingEntities.empty(), "There should be no waiting entities if space is available in main container"); NazaraAssert(m_waitingEntities.empty(), "There should be no waiting entities if space is available in main container");
m_entities.push_back(Entity(this, id)); //< We can't use emplace_back due to the scope m_entities.emplace_back(Entity(this, id)); //< We can't make our vector create the entity due to the scope
entBlock = &m_entities.back(); entBlock = &m_entities.back();
} }
else else
@ -124,11 +127,11 @@ namespace Ndk
m_entityBlocks.clear(); m_entityBlocks.clear();
m_entities.clear(); m_entities.clear();
m_freeIdList.clear();
m_waitingEntities.clear(); m_waitingEntities.clear();
m_aliveEntities.Clear(); m_aliveEntities.Clear();
m_dirtyEntities.Clear(); m_dirtyEntities.Clear();
m_freeEntityIds.Clear();
m_killedEntities.Clear(); m_killedEntities.Clear();
} }
@ -207,7 +210,7 @@ namespace Ndk
entity->Destroy(); entity->Destroy();
// Send back the identifier of the entity to the free queue // Send back the identifier of the entity to the free queue
m_freeIdList.push_back(entity->GetId()); m_freeEntityIds.UnboundedSet(entity->GetId());
} }
m_killedEntities.Reset(); m_killedEntities.Reset();