8#ifndef URANUS_REGISTRY_HPP
9#define URANUS_REGISTRY_HPP
12#include "SparseArray.hpp"
14#include "uranus/Exceptions.hpp"
20#include <unordered_map>
34 template<
class Component>
43 template<
class Component>
52 template<
class Component>
61 template<
class Component>
62 typename SparseArray<Component>::ConstReferenceType
getComponent(
const Entity &e)
const;
70 template<
class Component>
71 typename SparseArray<Component>::ReferenceType
getComponent(
const size_t &e);
79 template<
class Component>
80 typename SparseArray<Component>::ConstReferenceType
getComponent(
const size_t &e)
const;
87 template<
class Component>
95 template<
class Component>
137 template<
typename Component>
138 typename SparseArray<Component>::ReferenceType
addComponent(
const Entity &to, Component &&c);
148 template<
typename Component,
typename... Params>
156 template<
typename Component>
164 template<
typename Component>
186 std::unordered_map<std::type_index, std::function<void(
size_t const &)>>
_destroyArrays;
191 template<
class Component>
194 auto const &typeIndex = std::type_index(
typeid(Component));
202 template<
class Component>
205 auto const &typeIndex = std::type_index(
typeid(Component));
213 template<
class Component>
216 auto const &typeIndex = std::type_index(
typeid(Component));
221 template<
class Component>
224 auto const &typeIndex = std::type_index(
typeid(Component));
229 template<
class Component>
232 auto const &typeIndex = std::type_index(
typeid(Component));
234 return std::any_cast<SparseArray<Component>
const &>(
_componentsArrays.at(typeIndex))[e];
237 template<
class Component>
240 auto const &typeIndex = std::type_index(
typeid(Component));
242 return std::any_cast<SparseArray<Component>
const &>(
_componentsArrays.at(typeIndex))[e];
245 template<
class Component>
248 auto const &typeIndex = std::type_index(
typeid(Component));
253 template<
class Component>
256 auto const &typeIndex = std::type_index(
typeid(Component));
258 return std::any_cast<SparseArray<Component>
const &>(
_componentsArrays.at(typeIndex));
302 template<
typename Component>
305 auto &components = getComponents<Component>();
306 return components.insertAt(to, std::forward<Component>(c));
309 template<
typename Component,
typename... Params>
312 auto &components = getComponents<Component>();
313 return components.emplaceAt(to, std::forward<Params>(p)...);
316 template<
typename Component>
319 auto &components = getComponents<Component>();
320 components.erase(from);
323 template<
typename Component>
326 auto &components = getComponents<Component>();
327 components.erase(from);
This file is used to include some core files of the engine.
Entity class, used to identify entities.
Definition: Entity.hpp:21
size_t _id
Id of the entity.
Definition: Entity.hpp:48
Registry class, used to store and manage entities and their components.
Definition: Registry.hpp:26
size_t getEntityCounter() const
Get the counter used to generate new entity ids.
Definition: Registry.hpp:335
Entity spawnEntity()
Spawn a new entity.
Definition: Registry.hpp:261
SparseArray< Component > & registerComponent(std::function< void(const size_t &)> &)
Register a component type and it's delete function.
Definition: Registry.hpp:203
std::unordered_map< std::type_index, std::function< void(size_t const &)> > _destroyArrays
Map containing all the components delete functions.
Definition: Registry.hpp:186
SparseArray< Component >::ReferenceType emplaceComponent(const Entity &to, Params &&...p)
Emplace a component to an entity.
Definition: Registry.hpp:310
SparseArray< Component > const & getComponents() const
Get the const SparseArray of a component type.
void killAllEntities()
Destroy all entities.
Definition: Registry.hpp:294
SparseArray< Component > & getComponents()
Get the SparseArray of a component type.
Definition: Registry.hpp:246
void killEntity(const Entity &e)
Destroy an entity.
Definition: Registry.hpp:277
size_t _entityCounter
Counter used to generate new entity ids.
Definition: Registry.hpp:187
Entity entityFromIndex(std::size_t idx) const
Create an entity from an index (only use this if you know what you're doing) (this will not create a ...
Definition: Registry.hpp:270
std::unordered_map< std::type_index, std::any > _componentsArrays
Get the maximum index of an entity.
Definition: Registry.hpp:185
std::list< size_t > _freeIds
Queue containing the ids of the destroyed entities.
Definition: Registry.hpp:188
int entitiesAliveCount() const
Get the number of entities alive.
Definition: Registry.hpp:330
SparseArray< Component >::ReferenceType addComponent(const Entity &to, Component &&c)
Add a component to an entity.
Definition: Registry.hpp:303
SparseArray< Component >::ReferenceType getComponent(const Entity &e)
Get a component from an entity.
Definition: Registry.hpp:214
void removeComponent(const Entity &from)
Remove a component from an entity.
Definition: Registry.hpp:317
SparseArray class, used to store components in a sparse way.
Definition: SparseArray.hpp:25
Thrown when an entity has an invalid index.
Definition: Exceptions.hpp:17
Namespace containing all the ECS related classes.
Definition: Components.hpp:21