Delayed removal of entities.
This commit is contained in:
+24
-14
@@ -36,6 +36,8 @@ void World::Update(double dt)
|
|||||||
system->Update(dt);
|
system->Update(dt);
|
||||||
RecursiveUpdate(system, dt, 0);
|
RecursiveUpdate(system, dt, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ProcessEntityRemovals();
|
||||||
}
|
}
|
||||||
|
|
||||||
//std::vector<EntityID> GetEntityChildren(EntityID entity);
|
//std::vector<EntityID> GetEntityChildren(EntityID entity);
|
||||||
@@ -60,21 +62,29 @@ bool World::ValidEntity(EntityID entity)
|
|||||||
|
|
||||||
void World::RemoveEntity(EntityID entity)
|
void World::RemoveEntity(EntityID entity)
|
||||||
{
|
{
|
||||||
m_EntityParents.erase(entity);
|
m_EntitiesToRemove.push_back(entity);
|
||||||
// Remove components
|
}
|
||||||
for (auto pair : m_EntityComponents[entity]) {
|
|
||||||
auto type = pair.first;
|
|
||||||
auto component = pair.second;
|
|
||||||
// Trigger events
|
|
||||||
for (auto pair : m_Systems) {
|
|
||||||
auto system = pair.second;
|
|
||||||
system->OnComponentRemoved(type, component.get());
|
|
||||||
}
|
|
||||||
m_ComponentsOfType[type].remove(component);
|
|
||||||
}
|
|
||||||
m_EntityComponents.erase(entity);
|
|
||||||
|
|
||||||
RecycleEntityID(entity);
|
void World::ProcessEntityRemovals()
|
||||||
|
{
|
||||||
|
for (auto entity : m_EntitiesToRemove) {
|
||||||
|
m_EntityParents.erase(entity);
|
||||||
|
// Remove components
|
||||||
|
for (auto pair : m_EntityComponents[entity]) {
|
||||||
|
auto type = pair.first;
|
||||||
|
auto component = pair.second;
|
||||||
|
// Trigger events
|
||||||
|
for (auto pair : m_Systems) {
|
||||||
|
auto system = pair.second;
|
||||||
|
system->OnComponentRemoved(type, component.get());
|
||||||
|
}
|
||||||
|
m_ComponentsOfType[type].remove(component);
|
||||||
|
}
|
||||||
|
m_EntityComponents.erase(entity);
|
||||||
|
|
||||||
|
RecycleEntityID(entity);
|
||||||
|
}
|
||||||
|
m_EntitiesToRemove.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
EntityID World::CreateEntity(EntityID parent /*= 0*/)
|
EntityID World::CreateEntity(EntityID parent /*= 0*/)
|
||||||
|
|||||||
@@ -85,9 +85,13 @@ protected:
|
|||||||
std::unordered_map<std::string, std::list<std::shared_ptr<Component>>> m_ComponentsOfType;
|
std::unordered_map<std::string, std::list<std::shared_ptr<Component>>> m_ComponentsOfType;
|
||||||
std::unordered_map<EntityID, std::map<std::string, std::shared_ptr<Component>>> m_EntityComponents;
|
std::unordered_map<EntityID, std::map<std::string, std::shared_ptr<Component>>> m_EntityComponents;
|
||||||
|
|
||||||
|
std::list<EntityID> m_EntitiesToRemove;
|
||||||
|
void ProcessEntityRemovals();
|
||||||
|
|
||||||
EntityID GenerateEntityID();
|
EntityID GenerateEntityID();
|
||||||
|
|
||||||
void RecycleEntityID(EntityID id);
|
void RecycleEntityID(EntityID id);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
|
|||||||
Reference in New Issue
Block a user