From 15783fb87e48eca7029edfca5df2753f0980f73d Mon Sep 17 00:00:00 2001 From: Anders Pistol Date: Tue, 30 Jan 2024 22:15:52 +0100 Subject: [PATCH] Traktor: Simplified the group component since it's no longer required to defer adding or removing children while updating. --- code/World/Entity/GroupComponent.cpp | 70 ++-------------------------- code/World/Entity/GroupComponent.h | 7 +-- 2 files changed, 5 insertions(+), 72 deletions(-) diff --git a/code/World/Entity/GroupComponent.cpp b/code/World/Entity/GroupComponent.cpp index 4b77ac4cbc..e51020d455 100644 --- a/code/World/Entity/GroupComponent.cpp +++ b/code/World/Entity/GroupComponent.cpp @@ -1,12 +1,11 @@ /* * TRAKTOR - * Copyright (c) 2022 Anders Pistol. + * Copyright (c) 2022-2024 Anders Pistol. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "Core/Misc/Save.h" #include "World/Entity.h" #include "World/Entity/GroupComponent.h" @@ -17,13 +16,6 @@ T_IMPLEMENT_RTTI_CLASS(L"traktor.world.GroupComponent", GroupComponent, IEntityC void GroupComponent::destroy() { - T_ASSERT(m_deferred[0].empty()); - T_ASSERT(m_deferred[1].empty()); - for (auto entity : m_entities) - { - if (entity) - entity->destroy(); - } m_entities.resize(0); } @@ -36,28 +28,6 @@ void GroupComponent::setOwner(Entity* owner) void GroupComponent::update(const UpdateParams& update) { - // Update child entities; set flag to indicate we're - //// updating 'em. - //{ - // T_ANONYMOUS_VAR(Save< bool >)(m_update, true); - // for (auto entity : m_entities) - // entity->update(update); - //} - - // Add deferred entities. - if (!m_deferred[1].empty()) - { - m_entities.insert(m_entities.end(), m_deferred[0].begin(), m_deferred[0].end()); - m_deferred[0].resize(0); - } - - // Remove deferred entities. - if (!m_deferred[1].empty()) - { - for (auto entity : m_deferred[1]) - removeEntity(entity); - m_deferred[1].resize(0); - } } void GroupComponent::setTransform(const Transform& transform) @@ -75,7 +45,7 @@ void GroupComponent::setTransform(const Transform& transform) Aabb3 GroupComponent::getBoundingBox() const { - Transform invTransform = m_transform.inverse(); + const Transform invTransform = m_transform.inverse(); Aabb3 boundingBox; for (auto entity : m_entities) @@ -95,30 +65,13 @@ Aabb3 GroupComponent::getBoundingBox() const void GroupComponent::addEntity(Entity* entity) { T_ASSERT_M (entity, L"Null entity"); - if (m_update) - { - // Add as deferred add; cannot add while in update loop. - m_deferred[0].push_back(entity); - } - else - m_entities.push_back(entity); + m_entities.push_back(entity); } void GroupComponent::removeEntity(Entity* entity) { T_ASSERT_M (entity, L"Null entity"); - if (m_update) - { - // Add as deferred remove; we cannot remove while update - // is iterating entity array. - m_deferred[1].push_back(entity); - } - else - { - auto i = std::find(m_entities.begin(), m_entities.end(), entity); - if (i != m_entities.end()) - m_entities.erase(i); - } + m_entities.remove(entity); } void GroupComponent::removeAllEntities() @@ -156,19 +109,4 @@ RefArray< Entity > GroupComponent::getEntities(const std::wstring& name) const return entities; } -bool GroupComponent::traverse(const std::function< bool(Entity*) >& visitor) -{ - for (auto entity : m_entities) - { - if (!visitor(entity)) - return false; - if (auto childGroup = entity->getComponent< GroupComponent >()) - { - if (!childGroup->traverse(visitor)) - return false; - } - } - return true; -} - } diff --git a/code/World/Entity/GroupComponent.h b/code/World/Entity/GroupComponent.h index 8168aabf9c..de5ad8b62d 100644 --- a/code/World/Entity/GroupComponent.h +++ b/code/World/Entity/GroupComponent.h @@ -1,6 +1,6 @@ /* * TRAKTOR - * Copyright (c) 2022 Anders Pistol. + * Copyright (c) 2022-2024 Anders Pistol. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -8,7 +8,6 @@ */ #pragma once -#include #include #include "Core/RefArray.h" #include "World/IEntityComponent.h" @@ -56,14 +55,10 @@ class T_DLLCLASS GroupComponent : public IEntityComponent RefArray< Entity > getEntities(const std::wstring& name) const; - bool traverse(const std::function< bool(Entity*) >& visitor); - private: Entity* m_owner = nullptr; Transform m_transform; RefArray< Entity > m_entities; - RefArray< Entity > m_deferred[2]; - bool m_update = false; }; }