|
Aegis2D
Modular C++26 2D game engine for networked games
|
Hands out entity handles and recycles the indices of destroyed entities. More...
#include <aegis2d/ecs/entity_allocator.hpp>
Public Member Functions | |
| Entity | create () |
| Creates an entity. | |
| bool | destroy (Entity entity) |
| Destroys an entity: its index gets a new generation and goes to the free list. | |
| bool | alive (Entity entity) const noexcept |
| Tells whether a handle designates a living entity. | |
| void | clear () |
| Destroys every living entity, as destroy() would. | |
| void | reserve (std::size_t slots) |
| Prepares room for a number of indices without reallocating. | |
| std::size_t | size () const noexcept |
| Number of living entities. | |
| std::size_t | slot_count () const noexcept |
| Number of indices ever opened, living or free. | |
| std::size_t | free_count () const noexcept |
| Number of indices waiting on the free list. | |
Hands out entity handles and recycles the indices of destroyed entities.
Each index has a slot that stores its current generation and whether an entity lives there. create() reuses a free index when there is one, otherwise it opens a new slot; destroy() increments the generation of the index and puts it on the free list, so every handle created before is detected as stale by alive().
The free list is first in, first out: an index waits behind all the other free ones before it is reused. A game that spawns and kills bullets every tick thus spreads the reuses over many indices, and the 8-bit generation takes much longer to wrap back to a value an old handle could still hold.
Not thread-safe: the registry that owns the allocator serializes the calls.
|
noexcept |
Tells whether a handle designates a living entity.
| entity | Handle to check. |
false for a null handle, an index never created, a destroyed entity, or a handle whose generation is not the current one of its index. | void aegis2d::ecs::EntityAllocator::clear | ( | ) |
Destroys every living entity, as destroy() would.
Generations are kept and incremented, not reset: handles created before the call stay stale instead of matching the next entities.
| Entity aegis2d::ecs::EntityAllocator::create | ( | ) |
Creates an entity.
| std::length_error | If all Entity::max_index + 1 indices are alive. |
| bool aegis2d::ecs::EntityAllocator::destroy | ( | Entity | entity | ) |
Destroys an entity: its index gets a new generation and goes to the free list.
| entity | Handle to destroy. |
true if the entity was alive; false for a null, stale or unknown handle, which is left untouched (destroying twice is harmless).
|
noexcept |
Number of indices waiting on the free list.
| void aegis2d::ecs::EntityAllocator::reserve | ( | std::size_t | slots | ) |
Prepares room for a number of indices without reallocating.
| slots | Number of indices to reserve. |
|
noexcept |
Number of living entities.
|
noexcept |
Number of indices ever opened, living or free.