Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
entity_allocator.hpp
Go to the documentation of this file.
1
3#pragma once
5#include <aegis2d/ecs/export.hpp>
6
7#include <cstddef>
8#include <deque>
9#include <vector>
10
11namespace aegis2d::ecs {
12
25class AEGIS2D_ECS_EXPORT EntityAllocator {
26public:
31 [[nodiscard]] Entity create();
32
37 bool destroy(Entity entity);
38
43 [[nodiscard]] bool alive(Entity entity) const noexcept;
44
48 void clear();
49
52 void reserve(std::size_t slots);
53
56 [[nodiscard]] std::size_t size() const noexcept;
57
60 [[nodiscard]] std::size_t slot_count() const noexcept;
61
64 [[nodiscard]] std::size_t free_count() const noexcept;
65
66private:
68 struct Slot {
70 Entity::Generation generation = 0;
72 bool alive = false;
73 };
74
75 std::vector<Slot> slots_;
76 std::deque<Entity::Index> free_;
77};
78
79} // namespace aegis2d::ecs
Hands out entity handles and recycles the indices of destroyed entities.
Definition entity_allocator.hpp:25
bool alive(Entity entity) const noexcept
Tells whether a handle designates a living entity.
bool destroy(Entity entity)
Destroys an entity: its index gets a new generation and goes to the free list.
void reserve(std::size_t slots)
Prepares room for a number of indices without reallocating.
Entity create()
Creates an entity.
std::size_t size() const noexcept
Number of living entities.
void clear()
Destroys every living entity, as destroy() would.
Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation.
Definition entity.hpp:23
std::uint8_t Generation
Reuse counter of an index.
Definition entity.hpp:28
Generational entity handle: an index into the component pools plus a generation.
Entity-Component-System used by client and server.
Definition animation.hpp:13
Definition endpoint.hpp:85