16 #include "SparseArray.hpp"
35 explicit Registry(std::size_t nbEntities) : _nbEntities(nbEntities) {};
48 template <
class Component>
55 _erasers.emplace_back([](
Registry ®istry,
Entity const &entity) {
59 throw std::invalid_argument(
"Component already registered");
61 return std::any_cast<Sparse_array<Component> &>(it->second);
70 template <
class Component>
73 return std::any_cast<Sparse_array<Component> &>(_componentsArrays.at(std::type_index(
typeid(
Component))));
82 template <
class Component>
85 return std::any_cast<Sparse_array<Component> &>(_componentsArrays.at(std::type_index(
typeid(
Component))));
97 return Entity(_nbEntities - 1);
107 template <
class ...Components>
111 (
add_component(e, std::forward<Components>(components)), ...);
123 if (idx >= _nbEntities) {
124 throw std::out_of_range(
"At this index, entity doesn't exist !");
136 for (
auto &&it : _erasers)
148 template <
typename Component>
151 if (_componentsArrays.find(std::type_index(
typeid(
Component))) == _componentsArrays.end())
152 throw std::invalid_argument(
"Component not registered !");
155 if (e > sparseArray.
size()) {
156 sparseArray.
extend((
size_t) e - sparseArray.
size());
170 template <
typename Component,
typename ...Params>
174 return sparseArray.emplaceAt(to, p...);
183 template <
typename Component>
188 if (sparseArray.
size() > from) {
189 sparseArray.
erase(from);
200 template <
class ...Component,
typename Function>
202 _listOfSystems.push_back([&f](
Registry ®istry) ->
void {
214 template <
class ...Component,
typename Function>
216 _listOfSystems.push_back([&f](
Registry ®istry) ->
void {
225 for (
auto &
function : _listOfSystems)
230 std::vector<std::function<void (
Registry &,
Entity const &)>> _erasers;
232 std::map<std::type_index, std::any> _componentsArrays;
234 std::size_t _nbEntities = 0;
236 std::vector<Entity> _killedEntities;
238 std::vector<std::function<void(
Registry &)>> _listOfSystems;
Registry(std::size_t nbEntities)
Construct a new Registry object.
Definition: Registry.hpp:35
Class that handle Element of type Component like a vector which can be holed.
Definition: SparseArray.hpp:22
Sparse_array< Component > & get_components()
Get the components object.
Definition: Registry.hpp:71
size_type size() const
Overloads the operator size()
Definition: SparseArray.hpp:188
const Sparse_array< Component > & get_components() const
Get the components object.
Definition: Registry.hpp:83
Namespace for all components.
Definition: CConnection.hpp:14
Sparse_array< Component >::reference_type add_component(Entity const &e, Component &&c)
A method to add a component to a known sparse array.
Definition: Registry.hpp:149
void kill_entity(Entity const &e)
A method to kill an entity.
Definition: Registry.hpp:134
void extend(size_t sizeToExtend)
Overload the operator extend()
Definition: SparseArray.hpp:198
Sparse_array< Component > & register_component()
A method to register a component to the registry, it inserts it inside the various arrays contained i...
Definition: Registry.hpp:49
Class that handle ECS.
Definition: Registry.hpp:22
void erase(size_type pos)
Overloads the operator erase()
Definition: SparseArray.hpp:274
virtual ~Registry()=default
Destroy the Registry object.
Sparse_array< Component >::reference_type emplace_component(Entity const &to, Params &&...p)
A method to add a component at a position int the sparse array, a sparse array means that some cells ...
Definition: Registry.hpp:171
Registry()=default
Construct a new Registry object.
void remove_component(Entity const &from)
A method to remove a component from an entity.
Definition: Registry.hpp:184
Entity spawn_entity_with(Components &&...components)
A method that create an entity and add all components you want to it.
Definition: Registry.hpp:108
Class that handle entity in a sparse array like an index.
Definition: Entity.hpp:16
void add_system(Function &&f)
A method to add a system to the registry, adds the element to the list of systems of the registry.
Definition: Registry.hpp:201
Entity entity_from_index(std::size_t idx) const
A method to get an entity with its index.
Definition: Registry.hpp:121
Entity spawn_entity()
A method to spawn an entity, it adds it to the creators array.
Definition: Registry.hpp:93
reference_type insert_at(size_type pos, Component const &component)
Overloads the operator insert_at()
Definition: SparseArray.hpp:211
void run_systems()
A method that is used to run all the systems of the registry.
Definition: Registry.hpp:224
void add_system(Function const &f)
A method to add a system to the registry, adds the element to the list of systems of the registry.
Definition: Registry.hpp:215