hydrangea/include/EntityManager.hpp

72 lines
1.7 KiB
C++

#include "Types.hpp"
#include <array>
#include <cassert>
#include <queue>
#ifndef WINDOW_H
#define WINDOW_H
class EntityManager
{
private:
// Queue of unused entity IDs
std::queue<Entity> mAvailableEntities{};
// Array of signatures where the index corresponds to the entity ID
std::array<Signature, MAX_ENTITIES> mSignatures{};
// Total living entities - used to keep limits on how many exist
uint32_t mLivingEntityCount{};
public:
EntityManager()
{
// Initialize the queue with all possible entity IDs
for (Entity entity = 0; entity < MAX_ENTITIES; ++entity)
{
mAvailableEntities.push(entity);
}
}
Entity CreateEntity()
{
assert(mLivingEntityCount < MAX_ENTITIES && "Too many entities in existence.");
// Take an ID from the front of the queue
Entity id = mAvailableEntities.front();
mAvailableEntities.pop();
++mLivingEntityCount;
return id;
}
void DestroyEntity(Entity entity)
{
assert(entity < MAX_ENTITIES && "Entity out of range.");
// Invalidate the destroyed entity's signature
mSignatures[entity].reset();
// Put the destroyed ID at the back of the queue
mAvailableEntities.push(entity);
--mLivingEntityCount;
}
void SetSignature(Entity entity, Signature signature)
{
assert(entity < MAX_ENTITIES && "Entity out of range.");
// Put this entity's signature into the array
mSignatures[entity] = signature;
}
Signature GetSignature(Entity entity)
{
assert(entity < MAX_ENTITIES && "Entity out of range.");
// Get this entity's signature from the array
return mSignatures[entity];
}
};
#endif