ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
Loading...
Searching...
No Matches
EntityManager.hpp
Go to the documentation of this file.
1
6# pragma once
7
8#include <cstdint> // for uint32_t
9#include <vector> // for holding the Entity generations
10#include <queue> // for holding free indices
11
12#include "Imago/ECS/Entity.hpp"
13
14
15namespace Imago::ECS
16{
17
36 class EntityManager {
37 private:
38
39 std::vector<uint32_t> _generations;
40 std::queue<uint32_t> _freeIndices;
41
42 public:
43
44 // default constructor
45 EntityManager() = default;
46
52 Entity create();
53
63 void destroy(Entity);
64
74 bool is_valid(Entity) const;
75
79 uint32_t get_alive_count() const { return static_cast<uint32_t>(_generations.size() - _freeIndices.size()); };
80 };
81}
Defines the Entity alias and the free functions used to pack and unpack it.
uint32_t Entity
A packed identifier: 20 bits of index, 12 bits of generation.
Definition Entity.hpp:20
bool is_valid(Entity) const
Checks whether an Entity handle is still valid.
Definition EntityManager.cpp:67
void destroy(Entity)
Destroys an Entity, freeing its index for future reuse.
Definition EntityManager.cpp:43
uint32_t get_alive_count() const
Returns the number of currently-alive entities.
Definition EntityManager.hpp:79
Entity create()
Creates a new Entity, reusing a freed index when one is available.
Definition EntityManager.cpp:9