Skip to content

Commit

Permalink
Traktor: Simplified the group component since it's no longer required…
Browse files Browse the repository at this point in the history
… to defer adding or removing children while updating.
  • Loading branch information
apistol78 committed Jan 30, 2024
1 parent dc5259d commit 15783fb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 72 deletions.
70 changes: 4 additions & 66 deletions code/World/Entity/GroupComponent.cpp
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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);
}

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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;
}

}
7 changes: 1 addition & 6 deletions code/World/Entity/GroupComponent.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/*
* 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/.
*/
#pragma once

#include <functional>
#include <string>
#include "Core/RefArray.h"
#include "World/IEntityComponent.h"
Expand Down Expand Up @@ -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;
};

}

0 comments on commit 15783fb

Please sign in to comment.