game-engine/libs/componentarray.cpp
2024-10-09 11:26:03 -04:00

47 lines
1.7 KiB
C++

#include <cassert>
#include "componentarray.hpp"
template <typename T>
void ComponentArray<T>::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 <typename T> void ComponentArray<T>::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 <typename T> T &ComponentArray<T>::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 <typename T> void ComponentArray<T>::EntityDestroyed(Entity entity) {
if (mEntityToIndexMap.find(entity) != mEntityToIndexMap.end()) {
// Remove the entity's component if it existed
RemoveData(entity);
}
};