ecs
Loading...
Searching...
No Matches
Registry.hpp
Go to the documentation of this file.
1
8#ifndef REGISTRY_HPP
9#define REGISTRY_HPP
10
11//local
12#include "SparseArray.hpp"
13
14//global
15#include <any>
16#include <functional>
17#include <typeindex>
18#include <unordered_map>
19#include <vector>
20#include <iostream>
21#include <vector>
22#include <unordered_map>
23#include <typeindex>
24#include <type_traits>
25#include <memory>
26#include "System.hpp"
27#include "Concepts.hpp"
28
29namespace ecs {
30
31 class Entity;
32 class System;
33
44 class Registry {
45
46 public:
47
49
56
63 Entity entityFromIndex(std::size_t idx);
64
70 void killEntity(Entity const &e);
71
73
80 template <typename Component>
82
88 template <class ... Components>
89 void registerComponents();
90
96 template <IsTuple Tuple>
98
105 template <class Component>
107
114 template <class Component>
116
125 template <typename... Component, typename = std::enable_if_t<(sizeof...(Component) != 0)>>
126 void addComponent(Entity const &to, Component &&...c);
127
137 template <typename Component, typename ... Params>
138 void emplaceComponent(Entity const &to, Params &&... p);
139
146 template <typename... Component, typename = std::enable_if<(sizeof...(Component) != 0)>>
147 void removeComponent(Entity const &from);
148
150
156 template<SystemImplementation System>
157 void addSystem(System& s);
158
165 template<SystemImplementation System>
166 void addSystem(System&& s);
167
175 template <SystemImplementation... System, typename = std::enable_if<(sizeof...(System) != 0)>>
176 void addSystem(System&& ... s);
177
185 template <SystemImplementation System, typename ... Params>
186 void emplaceSystem(Params &&... params);
187
193 template <SystemImplementation ... System, typename = std::enable_if<(sizeof...(System) != 0)>>
194 void removeSystem();
195
201 template <SystemImplementation ... System, typename = std::enable_if<(sizeof...(System) != 0)>>
202 void callSystem();
203
204 private:
205
206 size_t _entitiesCount = 0;
207 std::vector<Entity> killedEntities;
208
209 std::unordered_map<std::type_index, std::any> componentsArrays;
210 std::unordered_map<std::type_index, std::any> systemsArrays;
211
212 std::vector<std::function<void(Registry &, Entity const &)>> componentsRemoves;
213
214 std::unordered_map<std::type_index, std::shared_ptr<ISystem>> systems;
215 };
216
217}
218
219#endif // REGISTRY_HPP
Component is a template define in Registry. (not a class)
Abstract base class representing a system in the ECS architecture.
Entity class from ECS.
Definition Entity.hpp:26
define the Registry class
Definition Registry.hpp:44
void addComponent(Entity const &to, Component &&...c)
Definition Registry_impl.hpp:50
Entity createEntity()
handling entities (define in Registry.cpp)
Definition Registry.cpp:14
void emplaceSystem(Params &&... params)
add a system to the registry
void callSystem()
call a system
Definition Registry_impl.hpp:93
void removeSystem()
remove a system from the registry
Definition Registry_impl.hpp:88
void registerComponents()
register a list of components
Definition Registry_impl.hpp:28
SparseArray< Component > & registerComponent()
handling components (define in Registry_impl.hpp)
Definition Registry_impl.hpp:21
void killEntity(Entity const &e)
Kill the given entity.
Definition Registry.cpp:29
SparseArray< Component > & getComponents()
get components of a given type
Definition Registry_impl.hpp:40
Entity entityFromIndex(std::size_t idx)
Create a Entity object from an index.
Definition Registry.cpp:25
void addSystem(System &s)
handling systems
Definition Registry_impl.hpp:68
void registerComponentsByExtraction()
register every components by extraction from a tuple
Definition Registry_impl.hpp:33
void emplaceComponent(Entity const &to, Params &&... p)
emplace a component to the given entity
Definition Registry_impl.hpp:55
void removeComponent(Entity const &from)
remove a component from the given entity
Definition Registry_impl.hpp:61
SparseArray class.
Definition SparseArray.hpp:45
Concept that ensures a type derives from System.
Definition System.hpp:55
Entity Component System.