Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
EntityManager.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <stack>
4#include <array>
5#include <algorithm>
6#include "Entity.hpp"
7#include "EntityHash.hpp"
8
14public:
16 static constexpr std::size_t MAX_ENTITIES = 10000;
17
22 for (Entity::IdType i = 0; i < MAX_ENTITIES; ++i)
23 freeIds.push(i);
24 }
25
31 if (freeIds.empty()) return Entity(Entity::INVALID_ENTITY);
32 Entity::IdType id = freeIds.top();
33 freeIds.pop();
34 alive.push_back(id);
35 masks[id].reset();
36 return Entity(id);
37 }
38
43 void destroy(Entity e) {
44 if (e.getId() == Entity::INVALID_ENTITY) return;
45 masks[e.getId()].reset();
46 freeIds.push(e.getId());
47 alive.erase(std::remove(alive.begin(), alive.end(), e.getId()), alive.end());
48 }
49
56 return masks[e.getId()];
57 }
58
63 const std::vector<Entity::IdType>& getAlive() const {
64 return alive;
65 }
66
67private:
69 std::vector<Entity::IdType> alive;
71 std::stack<Entity::IdType> freeIds;
73 std::array<ComponentMask, MAX_ENTITIES> masks;
74};
std::bitset< MAX_COMPONENTS > ComponentMask
Bitmask representing component registration status.
Definition Entity.hpp:55
Lightweight identifier representing a game object in the Entity Component System.
Definition Entity.hpp:11
static constexpr IdType INVALID_ENTITY
Constant representing a null or invalid entity.
Definition Entity.hpp:16
IdType getId() const
Retrieve the raw identifier of the entity.
Definition Entity.hpp:32
std::uint32_t IdType
Numeric type of the entity identifier.
Definition Entity.hpp:14
static constexpr std::size_t MAX_ENTITIES
Maximum allowed active entities.
Definition EntityManager.hpp:16
Entity create()
Creates a new Entity, populating it from the free ID pool.
Definition EntityManager.hpp:30
ComponentMask & getMask(Entity e)
Retrieves the component mask of an entity.
Definition EntityManager.hpp:55
EntityManager()
Construct a new Entity Manager object and initializes the free identifier pool.
Definition EntityManager.hpp:21
void destroy(Entity e)
Destroys an entity and recycles its identifier.
Definition EntityManager.hpp:43
std::stack< Entity::IdType > freeIds
Stack of available identifiers.
Definition EntityManager.hpp:71
const std::vector< Entity::IdType > & getAlive() const
Retrieves list of currently active entity identifiers.
Definition EntityManager.hpp:63
std::vector< Entity::IdType > alive
Tracking vector of active entity IDs.
Definition EntityManager.hpp:69
std::array< ComponentMask, MAX_ENTITIES > masks
Array of component masks indexable by entity ID.
Definition EntityManager.hpp:73