diff --git a/Volt/Volt/src/Volt/Scene/Scene.cpp b/Volt/Volt/src/Volt/Scene/Scene.cpp index 7592f89f2..4939e159f 100644 --- a/Volt/Volt/src/Volt/Scene/Scene.cpp +++ b/Volt/Volt/src/Volt/Scene/Scene.cpp @@ -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) @@ -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()) - { - auto& relComp = entity.GetComponent(); - if (relComp.parent != entt::null) - { - Entity parentEnt = { relComp.parent, this }; - - if (parentEnt.HasComponent()) - { - auto& parentRelComp = parentEnt.GetComponent(); - 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(relComp.children.size()) - 1; i >= 0; --i) - { - Entity childEnt{ relComp.children.at(i), this }; - RemoveEntity(childEnt); - } - } - - if (entity.HasComponent()) - { - for (const auto& scriptId : entity.GetComponent().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) @@ -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()) + { + auto& relComp = entity.GetComponent(); + if (relComp.parent != entt::null) + { + Entity parentEnt = { relComp.parent, this }; + + if (parentEnt.HasComponent() && !removingParent) + { + auto& parentRelComp = parentEnt.GetComponent(); + + 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(relComp.children.size()) - 1; i >= 0; --i) + { + Entity childEnt{ relComp.children.at(i), this }; + RemoveEntityInternal(childEnt, true); + + relComp = entity.GetComponent(); + } + } + + if (entity.HasComponent()) + { + for (const auto& scriptId : entity.GetComponent().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); diff --git a/Volt/Volt/src/Volt/Scene/Scene.h b/Volt/Volt/src/Volt/Scene/Scene.h index 9652fd8eb..6a8d70c92 100644 --- a/Volt/Volt/src/Volt/Scene/Scene.h +++ b/Volt/Volt/src/Volt/Scene/Scene.h @@ -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); @@ -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; @@ -191,9 +192,6 @@ namespace Volt std::string m_name = "New Scene"; entt::registry m_registry; - std::map m_entityTimesToDestroyRemoved; - std::map m_entityTimesToDestroy; - std::vector m_sceneLayers; mutable std::unordered_map m_cachedEntityTransforms;