6#include <aegis2d/ecs/export.hpp>
17#include <unordered_map>
57 [[nodiscard]]
bool alive(
Entity entity) const noexcept;
61 [[nodiscard]]
std::
size_t size() const noexcept;
75 template <typename Component, typename... Args>
76 Component& emplace(
Entity entity, Args&&... args) {
78 return pool<Component>().emplace(entity, std::forward<Args>(args)...);
87 template <
typename Component,
typename... Args>
91 if (
auto current = components.
try_get(entity)) {
92 current->get() = Component(std::forward<Args>(args)...);
93 return current->get();
95 return components.
emplace(entity, std::forward<Args>(args)...);
102 template <
typename Component>
104 auto components = find_pool<Component>();
105 return components && components->get().remove(entity);
112 template <
typename... Components>
113 requires(
sizeof...(Components) > 0)
115 return (has_one<Components>(entity) && ...);
123 template <
typename Component>
125 auto components = find_pool<Component>();
127 throw std::out_of_range{
"Registry: the entity has no such component"};
129 return components->get().get(entity);
133 template <
typename Component>
134 [[nodiscard]]
const Component&
get(
Entity entity)
const {
135 auto components = find_pool<Component>();
137 throw std::out_of_range{
"Registry: the entity has no such component"};
139 return components->get().get(entity);
146 template <
typename Component>
147 [[nodiscard]] std::optional<std::reference_wrapper<Component>>
try_get(
Entity entity) {
148 auto components = find_pool<Component>();
152 return components->get().try_get(entity);
156 template <
typename Component>
157 [[nodiscard]] std::optional<std::reference_wrapper<const Component>>
159 auto components = find_pool<Component>();
163 return components->get().try_get(entity);
169 template <
typename Component>
170 [[nodiscard]] std::size_t
count()
const {
171 auto components = find_pool<Component>();
172 return components ? components->get().size() : 0;
180 template <
typename Component>
182 if (
auto components = find_pool<Component>()) {
183 return components->get();
185 auto created = std::make_unique<SparseSet<Component>>();
187 add_pool(
typeid(Component), std::move(created));
194 template <
typename Component>
195 [[nodiscard]] std::optional<std::reference_wrapper<SparseSet<Component>>>
find_pool() {
196 auto base = find_base(
typeid(Component));
204 template <
typename Component>
205 [[nodiscard]] std::optional<std::reference_wrapper<const SparseSet<Component>>>
207 auto base = find_base(
typeid(Component));
221 template <
typename... Components>
223 return View<Components...>{pool<Components>()...};
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);
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);
240 EntityAllocator entities_;
241 std::unordered_map<std::type_index, std::unique_ptr<SparseSetBase>> pools_;
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.