From ab187a9fa91cc9c9b8063f6222ffe6d97dbfc5a8 Mon Sep 17 00:00:00 2001 From: Benjamin Palko Date: Thu, 10 Oct 2024 14:00:33 -0400 Subject: [PATCH] merge component array into manager --- libs/ecs/componentarray.cpp | 47 ----------------------------- libs/ecs/componentarray.hpp | 37 ----------------------- libs/ecs/componentmanager.cpp | 56 ++++++++++++++++++++++++++++++++++- libs/ecs/componentmanager.hpp | 33 +++++++++++++++++++-- 4 files changed, 86 insertions(+), 87 deletions(-) delete mode 100644 libs/ecs/componentarray.cpp delete mode 100644 libs/ecs/componentarray.hpp diff --git a/libs/ecs/componentarray.cpp b/libs/ecs/componentarray.cpp deleted file mode 100644 index 3b3b45b..0000000 --- a/libs/ecs/componentarray.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include -#include "componentarray.hpp" - -template -void ComponentArray::InsertData(Entity entity, T component) { - assert(mEntityToIndexMap.find(entity) == mEntityToIndexMap.end() && - "Component added to same entity more than once."); - - // Put new entry at end and update the maps - size_t newIndex = mSize; - mEntityToIndexMap[entity] = newIndex; - mIndexToEntityMap[newIndex] = entity; - mComponentArray[newIndex] = component; - ++mSize; -}; -template void ComponentArray::RemoveData(Entity entity) { - assert(mEntityToIndexMap.find(entity) != mEntityToIndexMap.end() && - "Removing non-existent component."); - - // Copy element at end into deleted element's place to maintain density - size_t indexOfRemovedEntity = mEntityToIndexMap[entity]; - size_t indexOfLastElement = mSize - 1; - mComponentArray[indexOfRemovedEntity] = mComponentArray[indexOfLastElement]; - - // Update map to point to moved spot - Entity entityOfLastElement = mIndexToEntityMap[indexOfLastElement]; - mEntityToIndexMap[entityOfLastElement] = indexOfRemovedEntity; - mIndexToEntityMap[indexOfRemovedEntity] = entityOfLastElement; - - mEntityToIndexMap.erase(entity); - mIndexToEntityMap.erase(indexOfLastElement); - - --mSize; -}; -template T &ComponentArray::GetData(Entity entity) { - assert(mEntityToIndexMap.find(entity) != mEntityToIndexMap.end() && - "Retrieving non-existent component."); - - // Return a reference to the entity's component - return mComponentArray[mEntityToIndexMap[entity]]; -}; -template void ComponentArray::EntityDestroyed(Entity entity) { - if (mEntityToIndexMap.find(entity) != mEntityToIndexMap.end()) { - // Remove the entity's component if it existed - RemoveData(entity); - } -}; diff --git a/libs/ecs/componentarray.hpp b/libs/ecs/componentarray.hpp deleted file mode 100644 index f8b10c4..0000000 --- a/libs/ecs/componentarray.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef COMPONENTARRAY_HPP -#define COMPONENTARRAY_HPP - -#include -#include -#include "entity.hpp" - -class IComponentArray { -public: - virtual ~IComponentArray() = default; - virtual void EntityDestroyed(Entity entity) = 0; -}; - -template class ComponentArray : public IComponentArray { - // The packed array of components (of generic type T), - // set to a specified maximum amount, matching the maximum number - // of entities allowed to exist simultaneously, so that each entity - // has a unique spot. - std::array mComponentArray; - - // Map from an entity ID to an array index. - std::unordered_map mEntityToIndexMap; - - // Map from an array index to an entity ID. - std::unordered_map mIndexToEntityMap; - - // Total size of valid entries in the array. - size_t mSize; - -public: - void InsertData(Entity entity, T component); - void RemoveData(Entity entity); - T &GetData(Entity entity); - void EntityDestroyed(Entity entity) override; -}; - -#endif // COMPONENTARRAY_HPP diff --git a/libs/ecs/componentmanager.cpp b/libs/ecs/componentmanager.cpp index 24584db..352c01f 100644 --- a/libs/ecs/componentmanager.cpp +++ b/libs/ecs/componentmanager.cpp @@ -1,7 +1,61 @@ #include "componentmanager.hpp" -#include "componentarray.hpp" + +#include #include +/** + * ComponentArray + **/ + +template +void ComponentArray::InsertData(Entity entity, T component) { + assert(mEntityToIndexMap.find(entity) == mEntityToIndexMap.end() && + "Component added to same entity more than once."); + + // Put new entry at end and update the maps + size_t newIndex = mSize; + mEntityToIndexMap[entity] = newIndex; + mIndexToEntityMap[newIndex] = entity; + mComponentArray[newIndex] = component; + ++mSize; +}; +template void ComponentArray::RemoveData(Entity entity) { + assert(mEntityToIndexMap.find(entity) != mEntityToIndexMap.end() && + "Removing non-existent component."); + + // Copy element at end into deleted element's place to maintain density + size_t indexOfRemovedEntity = mEntityToIndexMap[entity]; + size_t indexOfLastElement = mSize - 1; + mComponentArray[indexOfRemovedEntity] = mComponentArray[indexOfLastElement]; + + // Update map to point to moved spot + Entity entityOfLastElement = mIndexToEntityMap[indexOfLastElement]; + mEntityToIndexMap[entityOfLastElement] = indexOfRemovedEntity; + mIndexToEntityMap[indexOfRemovedEntity] = entityOfLastElement; + + mEntityToIndexMap.erase(entity); + mIndexToEntityMap.erase(indexOfLastElement); + + --mSize; +}; +template T &ComponentArray::GetData(Entity entity) { + assert(mEntityToIndexMap.find(entity) != mEntityToIndexMap.end() && + "Retrieving non-existent component."); + + // Return a reference to the entity's component + return mComponentArray[mEntityToIndexMap[entity]]; +}; +template void ComponentArray::EntityDestroyed(Entity entity) { + if (mEntityToIndexMap.find(entity) != mEntityToIndexMap.end()) { + // Remove the entity's component if it existed + RemoveData(entity); + } +}; + +/** + * ComponentManager + **/ + // Private functions template std::shared_ptr> ComponentManager::GetComponentArray() { diff --git a/libs/ecs/componentmanager.hpp b/libs/ecs/componentmanager.hpp index 16ab368..a61a95f 100644 --- a/libs/ecs/componentmanager.hpp +++ b/libs/ecs/componentmanager.hpp @@ -1,11 +1,40 @@ #ifndef COMPONENTMANAGER_HPP #define COMPONENTMANAGER_HPP +#include #include #include -#include +#include "entity.hpp" #include "component.hpp" -#include "componentarray.hpp" + +class IComponentArray { +public: + virtual ~IComponentArray() = default; + virtual void EntityDestroyed(Entity entity) = 0; +}; + +template class ComponentArray : public IComponentArray { + // The packed array of components (of generic type T), + // set to a specified maximum amount, matching the maximum number + // of entities allowed to exist simultaneously, so that each entity + // has a unique spot. + std::array mComponentArray; + + // Map from an entity ID to an array index. + std::unordered_map mEntityToIndexMap; + + // Map from an array index to an entity ID. + std::unordered_map mIndexToEntityMap; + + // Total size of valid entries in the array. + size_t mSize; + +public: + void InsertData(Entity entity, T component); + void RemoveData(Entity entity); + T &GetData(Entity entity); + void EntityDestroyed(Entity entity) override; +}; class ComponentManager { // Map from type string pointer to a component type