Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
entity.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include <compare>
6#include <cstddef>
7#include <cstdint>
8#include <format>
9#include <functional>
10#include <string>
11
12namespace aegis2d::ecs {
13
23class Entity {
24public:
26 using Index = std::uint32_t;
28 using Generation = std::uint8_t;
30 using Id = std::uint32_t;
31
33 static constexpr unsigned index_bits = 24;
35 static constexpr Index max_index = (Index{1} << index_bits) - 2;
36
38 constexpr Entity() noexcept = default;
39
43 constexpr Entity(Index index, Generation generation) noexcept
44 : id_{(Id{generation} << index_bits) | (index & index_mask)} {}
45
49 [[nodiscard]] static constexpr Entity from_id(Id id) noexcept {
50 Entity entity;
51 entity.id_ = id;
52 return entity;
53 }
54
57 [[nodiscard]] static constexpr Entity null() noexcept { return Entity{}; }
58
61 [[nodiscard]] constexpr Index index() const noexcept { return id_ & index_mask; }
62
65 [[nodiscard]] constexpr Generation generation() const noexcept {
66 return static_cast<Generation>(id_ >> index_bits);
67 }
68
71 [[nodiscard]] constexpr Id id() const noexcept { return id_; }
72
75 [[nodiscard]] constexpr bool is_null() const noexcept { return index() == null_index; }
76
78 friend constexpr bool operator==(Entity, Entity) noexcept = default;
80 friend constexpr std::strong_ordering operator<=>(Entity, Entity) noexcept = default;
81
82private:
83 static constexpr Id index_mask = (Id{1} << index_bits) - 1;
84 static constexpr Index null_index = index_mask;
85
86 Id id_ = ~Id{0};
87};
88
95[[nodiscard]] inline std::string to_string(Entity entity) {
96 if (entity.is_null()) {
97 return "Entity(null)";
98 }
99 return std::format("Entity({}, {})", entity.index(), entity.generation());
100}
101
102} // namespace aegis2d::ecs
103
104// Hashes an entity handle by its packed value, for unordered containers.
106template <>
107struct std::hash<aegis2d::ecs::Entity> {
108 std::size_t operator()(aegis2d::ecs::Entity entity) const noexcept {
109 return std::hash<aegis2d::ecs::Entity::Id>{}(entity.id());
110 }
111};
Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation.
Definition entity.hpp:23
constexpr Generation generation() const noexcept
Generation of the index when this handle was created.
Definition entity.hpp:65
constexpr Index index() const noexcept
Position of the entity in the component pools.
Definition entity.hpp:61
constexpr bool is_null() const noexcept
Tells whether the handle designates no entity.
Definition entity.hpp:75
constexpr Entity() noexcept=default
Creates a null handle.
static constexpr Entity from_id(Id id) noexcept
Rebuilds a handle from its packed value, e.g. received from the network.
Definition entity.hpp:49
static constexpr Index max_index
Highest valid index: 16 777 214 (the last value is reserved for null).
Definition entity.hpp:35
std::uint32_t Id
Packed value: generation in the 8 high bits, index in the 24 low bits.
Definition entity.hpp:30
std::uint32_t Index
Position of the entity in the component pools.
Definition entity.hpp:26
friend constexpr std::strong_ordering operator<=>(Entity, Entity) noexcept=default
Orders handles by packed value (generation, then index).
friend constexpr bool operator==(Entity, Entity) noexcept=default
Handles are equal when both index and generation are.
std::uint8_t Generation
Reuse counter of an index.
Definition entity.hpp:28
static constexpr unsigned index_bits
Number of bits of the index.
Definition entity.hpp:33
static constexpr Entity null() noexcept
The null handle, equal to a default-constructed one.
Definition entity.hpp:57
constexpr Id id() const noexcept
Packed value, unique per index and generation.
Definition entity.hpp:71
Entity-Component-System used by client and server.
Definition animation.hpp:13
std::string to_string(Entity entity)
Text form of a handle, for logs: Entity(3, 1) (index, generation) or Entity(null).
Definition entity.hpp:95
Root namespace of the engine.
Definition audio.hpp:17