Skip to content

Commit

Permalink
Fixed removing entities
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkTreasure1 committed Sep 26, 2023
1 parent ff7daaa commit 4d02f1f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 93 deletions.
136 changes: 47 additions & 89 deletions Volt/Volt/src/Volt/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,40 +298,6 @@ namespace Volt
m_particleSystem.Update(m_registry, shared_from_this(), aDeltaTime);
m_audioSystem.Update(m_registry, shared_from_this(), aDeltaTime);
m_animationSystem.Update(m_registry, aDeltaTime);

for (auto& [ent, time] : m_entityTimesToDestroy)
{
time -= aDeltaTime;
if (time <= 0.f && !m_entityTimesToDestroyRemoved.at(ent))
{
RemoveEntity(Entity{ ent, this });
}
}

for (auto it = m_entityTimesToDestroyRemoved.begin(); it != m_entityTimesToDestroyRemoved.end();)
{
if (it->second == true)
{
m_entityTimesToDestroy.erase(it->first);
it = m_entityTimesToDestroyRemoved.erase(it);
}
else
{
++it;
}
}

for (auto it = m_entityTimesToDestroy.begin(); it != m_entityTimesToDestroy.end();)
{
if (it->second <= 0.f)
{
it = m_entityTimesToDestroy.erase(it);
}
else
{
++it;
}
}
}

void Scene::FixedUpdate(float aDeltaTime)
Expand Down Expand Up @@ -423,64 +389,10 @@ namespace Volt

void Scene::RemoveEntity(Entity entity)
{
if (!m_registry.valid(entity.GetID()))
{
return;
}

if (m_entityTimesToDestroyRemoved.contains(entity.GetID()))
{
m_entityTimesToDestroyRemoved.at(entity.GetID()) = true;
}

if (entity.HasComponent<RelationshipComponent>())
{
auto& relComp = entity.GetComponent<RelationshipComponent>();
if (relComp.parent != entt::null)
{
Entity parentEnt = { relComp.parent, this };

if (parentEnt.HasComponent<RelationshipComponent>())
{
auto& parentRelComp = parentEnt.GetComponent<RelationshipComponent>();
auto it = std::find(parentRelComp.children.begin(), parentRelComp.children.end(), entity.GetID());
if (it != parentRelComp.children.end())
{
parentRelComp.children.erase(it);
}
}
}

for (int32_t i = static_cast<int32_t>(relComp.children.size()) - 1; i >= 0; --i)
{
Entity childEnt{ relComp.children.at(i), this };
RemoveEntity(childEnt);
}
}

if (entity.HasComponent<MonoScriptComponent>())
{
for (const auto& scriptId : entity.GetComponent<MonoScriptComponent>().scriptIds)
{
MonoScriptEngine::OnDestroyInstance(scriptId);
}
}

m_registry.destroy(entity.GetID());
RemoveEntityInternal(entity, false);
SortScene();
}

void Scene::RemoveEntity(Entity entity, float aTimeToDestroy)
{
if (m_entityTimesToDestroy.find(entity.GetID()) != m_entityTimesToDestroy.end())
{
return;
}

m_entityTimesToDestroy.emplace(entity.GetID(), aTimeToDestroy);
m_entityTimesToDestroyRemoved.emplace(entity.GetID(), false);
}

void Scene::ParentEntity(Entity parent, Entity child)
{
if (!parent.IsValid() || !child.IsValid() || parent == child)
Expand Down Expand Up @@ -908,6 +820,52 @@ namespace Volt
InvalidateEntityTransform(entity.GetID());
}

void Scene::RemoveEntityInternal(Entity entity, bool removingParent)
{
if (!m_registry.valid(entity.GetID()))
{
return;
}

if (entity.HasComponent<RelationshipComponent>())
{
auto& relComp = entity.GetComponent<RelationshipComponent>();
if (relComp.parent != entt::null)
{
Entity parentEnt = { relComp.parent, this };

if (parentEnt.HasComponent<RelationshipComponent>() && !removingParent)
{
auto& parentRelComp = parentEnt.GetComponent<RelationshipComponent>();

auto it = std::find(parentRelComp.children.begin(), parentRelComp.children.end(), entity.GetID());
if (it != parentRelComp.children.end())
{
parentRelComp.children.erase(it);
}
}
}

for (int32_t i = static_cast<int32_t>(relComp.children.size()) - 1; i >= 0; --i)
{
Entity childEnt{ relComp.children.at(i), this };
RemoveEntityInternal(childEnt, true);

relComp = entity.GetComponent<RelationshipComponent>();
}
}

if (entity.HasComponent<MonoScriptComponent>())
{
for (const auto& scriptId : entity.GetComponent<MonoScriptComponent>().scriptIds)
{
MonoScriptEngine::OnDestroyInstance(scriptId);
}
}

m_registry.destroy(entity.GetID());
}

void Scene::AddLayer(const std::string& layerName, uint32_t layerId)
{
m_sceneLayers.emplace_back(layerId, layerName);
Expand Down
6 changes: 2 additions & 4 deletions Volt/Volt/src/Volt/Scene/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ namespace Volt

Entity CreateEntity(const std::string& tag = "");
void RemoveEntity(Entity entity);
void RemoveEntity(Entity entity, float aTimeToDestroy);

void ParentEntity(Entity parent, Entity child);
void UnparentEntity(Entity entity);
Expand Down Expand Up @@ -158,6 +157,8 @@ namespace Volt
void ConvertToWorldSpace(Entity entity);
void ConvertToLocalSpace(Entity entity);

void RemoveEntityInternal(Entity entity, bool removingParent);

void AddLayer(const std::string& layerName, uint32_t layerId);

const glm::mat4 GetWorldTransform(Entity entity) const;
Expand Down Expand Up @@ -191,9 +192,6 @@ namespace Volt
std::string m_name = "New Scene";
entt::registry m_registry;

std::map<entt::entity, bool> m_entityTimesToDestroyRemoved;
std::map<entt::entity, float> m_entityTimesToDestroy;

std::vector<SceneLayer> m_sceneLayers;

mutable std::unordered_map<entt::entity, glm::mat4> m_cachedEntityTransforms;
Expand Down

0 comments on commit 4d02f1f

Please sign in to comment.