8#ifndef URANUS_REGISTRY_HPP
9#define URANUS_REGISTRY_HPP
12#include "SparseArray.hpp"
18#include <unordered_map>
20namespace uranus::ecs {
32 template<
class Component>
41 template<
class Component>
50 template<
class Component>
59 template<
class Component>
60 typename SparseArray<Component>::ConstReferenceType
getComponent(
const Entity &e)
const;
68 template<
class Component>
69 typename SparseArray<Component>::ReferenceType
getComponent(
const size_t &e);
77 template<
class Component>
78 typename SparseArray<Component>::ConstReferenceType
getComponent(
const size_t &e)
const;
85 template<
class Component>
93 template<
class Component>
133 template<
typename Component>
134 typename SparseArray<Component>::ReferenceType
addComponent(
const Entity &to, Component &&c);
144 template<
typename Component,
typename... Params>
152 template<
typename Component>
160 template<
typename Component>
182 std::unordered_map<std::type_index, std::function<void(
size_t const &)>>
_destroyArrays;
187 template<
class Component>
190 auto const &typeIndex = std::type_index(
typeid(Component));
198 template<
class Component>
201 auto const &typeIndex = std::type_index(
typeid(Component));
209 template<
class Component>
212 auto const &typeIndex = std::type_index(
typeid(Component));
217 template<
class Component>
220 auto const &typeIndex = std::type_index(
typeid(Component));
225 template<
class Component>
228 auto const &typeIndex = std::type_index(
typeid(Component));
230 return std::any_cast<SparseArray<Component>
const &>(
_componentsArrays.at(typeIndex))[e];
233 template<
class Component>
236 auto const &typeIndex = std::type_index(
typeid(Component));
238 return std::any_cast<SparseArray<Component>
const &>(
_componentsArrays.at(typeIndex))[e];
241 template<
class Component>
244 auto const &typeIndex = std::type_index(
typeid(Component));
249 template<
class Component>
252 auto const &typeIndex = std::type_index(
typeid(Component));
254 return std::any_cast<SparseArray<Component>
const &>(
_componentsArrays.at(typeIndex));
296 template<
typename Component>
299 auto &components = getComponents<Component>();
300 return components.insertAt(to, std::forward<Component>(c));
303 template<
typename Component,
typename... Params>
306 auto &components = getComponents<Component>();
307 return components.emplaceAt(to, std::forward<Params>(p)...);
310 template<
typename Component>
313 auto &components = getComponents<Component>();
314 components.erase(from);
317 template<
typename Component>
320 auto &components = getComponents<Component>();
321 components.erase(from);
Entity class, used to identify entities.
Definition: Entity.hpp:20
size_t _id
Id of the entity.
Definition: Entity.hpp:47
Registry class, used to store and manage entities and their components.
Definition: Registry.hpp:24
size_t getEntityCounter() const
Get the counter used to generate new entity ids.
Definition: Registry.hpp:329
Entity spawnEntity()
Spawn a new entity.
Definition: Registry.hpp:257
SparseArray< Component > & registerComponent(std::function< void(const size_t &)> &)
Register a component type and it's delete function.
Definition: Registry.hpp:199
std::unordered_map< std::type_index, std::function< void(size_t const &)> > _destroyArrays
Map containing all the components delete functions.
Definition: Registry.hpp:182
SparseArray< Component >::ReferenceType emplaceComponent(const Entity &to, Params &&...p)
Emplace a component to an entity.
Definition: Registry.hpp:304
SparseArray< Component > const & getComponents() const
Get the const SparseArray of a component type.
void killAllEntities()
Destroy all entities.
Definition: Registry.hpp:287
SparseArray< Component > & getComponents()
Get the SparseArray of a component type.
Definition: Registry.hpp:242
void killEntity(const Entity &e)
Destroy an entity.
Definition: Registry.hpp:271
size_t _entityCounter
Counter used to generate new entity ids.
Definition: Registry.hpp:183
Entity entityFromIndex(std::size_t idx) const
Create an entity from an index.
Definition: Registry.hpp:266
std::unordered_map< std::type_index, std::any > _componentsArrays
Get the maximum index of an entity.
Definition: Registry.hpp:181
std::list< size_t > _freeIds
Queue containing the ids of the destroyed entities.
Definition: Registry.hpp:184
int entitiesAliveCount() const
Get the number of entities alive.
Definition: Registry.hpp:324
SparseArray< Component >::ReferenceType addComponent(const Entity &to, Component &&c)
Add a component to an entity.
Definition: Registry.hpp:297
SparseArray< Component >::ReferenceType getComponent(const Entity &e)
Get a component from an entity.
Definition: Registry.hpp:210
void removeComponent(const Entity &from)
Remove a component from an entity.
Definition: Registry.hpp:311
SparseArray class, used to store components in a sparse way.
Definition: SparseArray.hpp:22