Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
registry.hpp
Go to the documentation of this file.
1
3#pragma once
6#include <aegis2d/ecs/export.hpp>
9
10#include <cstddef>
11#include <functional>
12#include <memory>
13#include <optional>
14#include <stdexcept>
15#include <typeindex>
16#include <typeinfo>
17#include <unordered_map>
18#include <utility>
19
20namespace aegis2d::ecs {
21
34class AEGIS2D_ECS_EXPORT Registry {
35public:
36 Registry() = default;
37 Registry(const Registry&) = delete;
38 Registry& operator=(const Registry&) = delete;
39 Registry(Registry&&) noexcept = default;
40 Registry& operator=(Registry&&) noexcept = default;
41 ~Registry() = default;
42
46 [[nodiscard]] Entity create();
47
52 bool destroy(Entity entity);
53
57 [[nodiscard]] bool alive(Entity entity) const noexcept;
58
61 [[nodiscard]] std::size_t size() const noexcept;
62
66 void clear();
67
75 template <typename Component, typename... Args>
76 Component& emplace(Entity entity, Args&&... args) {
77 check_alive(entity);
78 return pool<Component>().emplace(entity, std::forward<Args>(args)...);
79 }
80
87 template <typename Component, typename... Args>
88 Component& emplace_or_replace(Entity entity, Args&&... args) {
89 check_alive(entity);
90 SparseSet<Component>& components = pool<Component>();
91 if (auto current = components.try_get(entity)) {
92 current->get() = Component(std::forward<Args>(args)...);
93 return current->get();
94 }
95 return components.emplace(entity, std::forward<Args>(args)...);
96 }
97
102 template <typename Component>
103 bool remove(Entity entity) {
104 auto components = find_pool<Component>();
105 return components && components->get().remove(entity);
106 }
107
112 template <typename... Components>
113 requires(sizeof...(Components) > 0)
114 [[nodiscard]] bool has(Entity entity) const {
115 return (has_one<Components>(entity) && ...);
116 }
117
123 template <typename Component>
124 [[nodiscard]] Component& get(Entity entity) {
125 auto components = find_pool<Component>();
126 if (!components) {
127 throw std::out_of_range{"Registry: the entity has no such component"};
128 }
129 return components->get().get(entity);
130 }
131
133 template <typename Component>
134 [[nodiscard]] const Component& get(Entity entity) const {
135 auto components = find_pool<Component>();
136 if (!components) {
137 throw std::out_of_range{"Registry: the entity has no such component"};
138 }
139 return components->get().get(entity);
140 }
141
146 template <typename Component>
147 [[nodiscard]] std::optional<std::reference_wrapper<Component>> try_get(Entity entity) {
148 auto components = find_pool<Component>();
149 if (!components) {
150 return std::nullopt;
151 }
152 return components->get().try_get(entity);
153 }
154
156 template <typename Component>
157 [[nodiscard]] std::optional<std::reference_wrapper<const Component>>
158 try_get(Entity entity) const {
159 auto components = find_pool<Component>();
160 if (!components) {
161 return std::nullopt;
162 }
163 return components->get().try_get(entity);
164 }
165
169 template <typename Component>
170 [[nodiscard]] std::size_t count() const {
171 auto components = find_pool<Component>();
172 return components ? components->get().size() : 0;
173 }
174
180 template <typename Component>
181 [[nodiscard]] SparseSet<Component>& pool() {
182 if (auto components = find_pool<Component>()) {
183 return components->get();
184 }
185 auto created = std::make_unique<SparseSet<Component>>();
186 SparseSet<Component>& components = *created;
187 add_pool(typeid(Component), std::move(created));
188 return components;
189 }
190
194 template <typename Component>
195 [[nodiscard]] std::optional<std::reference_wrapper<SparseSet<Component>>> find_pool() {
196 auto base = find_base(typeid(Component));
197 if (!base) {
198 return std::nullopt;
199 }
200 return std::ref(dynamic_cast<SparseSet<Component>&>(base->get()));
201 }
202
204 template <typename Component>
205 [[nodiscard]] std::optional<std::reference_wrapper<const SparseSet<Component>>>
206 find_pool() const {
207 auto base = find_base(typeid(Component));
208 if (!base) {
209 return std::nullopt;
210 }
211 return std::cref(dynamic_cast<const SparseSet<Component>&>(base->get()));
212 }
213
221 template <typename... Components>
222 [[nodiscard]] View<Components...> view() {
223 return View<Components...>{pool<Components>()...};
224 }
225
226private:
227 template <typename Component>
228 [[nodiscard]] bool has_one(Entity entity) const {
229 auto components = find_pool<Component>();
230 return components && components->get().contains(entity);
231 }
232
233 void check_alive(Entity entity) const;
234 [[nodiscard]] std::optional<std::reference_wrapper<SparseSetBase>>
235 find_base(std::type_index type) noexcept;
236 [[nodiscard]] std::optional<std::reference_wrapper<const SparseSetBase>>
237 find_base(std::type_index type) const noexcept;
238 void add_pool(std::type_index type, std::unique_ptr<SparseSetBase> pool);
239
240 EntityAllocator entities_;
241 std::unordered_map<std::type_index, std::unique_ptr<SparseSetBase>> pools_;
242};
243
244} // namespace aegis2d::ecs
Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation.
Definition entity.hpp:23
Entry point of the ECS: creates entities and stores their components.
Definition registry.hpp:34
Component & get(Entity entity)
Component of an entity.
Definition registry.hpp:124
std::optional< std::reference_wrapper< SparseSet< Component > > > find_pool()
Pool of a component type, if it was ever used.
Definition registry.hpp:195
Registry & operator=(const Registry &)=delete
Registry(Registry &&) noexcept=default
std::optional< std::reference_wrapper< Component > > try_get(Entity entity)
Component of an entity, if it has one.
Definition registry.hpp:147
Registry(const Registry &)=delete
bool has(Entity entity) const
Tells whether an entity has every listed component.
Definition registry.hpp:114
const Component & get(Entity entity) const
Component of an entity.
Definition registry.hpp:134
std::size_t count() const
Number of entities that have a component.
Definition registry.hpp:170
Component & emplace_or_replace(Entity entity, Args &&... args)
Gives an entity a component, or replaces the one it has.
Definition registry.hpp:88
std::optional< std::reference_wrapper< const SparseSet< Component > > > find_pool() const
Pool of a component type, if it was ever used.
Definition registry.hpp:206
std::optional< std::reference_wrapper< const Component > > try_get(Entity entity) const
Component of an entity, if it has one.
Definition registry.hpp:158
bool remove(Entity entity)
Removes a component from an entity.
Definition registry.hpp:103
SparseSet< Component > & pool()
Pool of a component type, created empty on first use.
Definition registry.hpp:181
View< Components... > view()
View on the entities that own all the listed components.
Definition registry.hpp:222
Pool of one component type, stored in a sparse set.
Definition sparse_set.hpp:117
std::optional< std::reference_wrapper< Component > > try_get(Entity entity)
Component of an entity, if it has one.
Definition sparse_set.hpp:153
Component & emplace(Entity entity, Args &&... args)
Gives an entity a component built in place.
Definition sparse_set.hpp:127
Visits every entity that owns all of Components, with those components.
Definition view.hpp:51
Generational entity handle: an index into the component pools plus a generation.
Creates and destroys entities, recycling their indices with a new generation.
Entity-Component-System used by client and server.
Definition animation.hpp:13
Definition endpoint.hpp:85
Sparse-set component pool: components packed in a dense array, found in O(1).
View: iterates the entities that own all the requested components.