ecs
Entity-Component-System
Loading...
Searching...
No Matches
Registry_impl.hpp
Go to the documentation of this file.
1
11#ifndef REGISTRY_IMPL_HPP
12#define REGISTRY_IMPL_HPP
13
14#include "Registry.hpp"
15#include "Entity.hpp"
16#include "SparseArray.hpp"
17#include "Concepts.hpp"
18
19#include "System.hpp"
20
21namespace ecs {
22
23 template <class Component>
25 componentsArrays[std::type_index(typeid(Component))] = std::make_any<SparseArray<Component>>();
26 componentsRemoves.push_back([] (Registry &r, Entity const &e) { r.getComponents<Component>().erase(e._idx); } );
27 return std::any_cast<SparseArray<Component> &>(componentsArrays[std::type_index(typeid(Component))]);
28 };
29
30 template <class ... Components>
34
35 template <IsTuple Tuple>
37 std::apply([this](auto ... components) {
38 this->registerComponents<std::decay_t<decltype(components)>...>();
39 }, Tuple{});
40 }
41
42 template <class Component>
44 return std::any_cast<SparseArray<Component> &>(componentsArrays[std::type_index(typeid(Component))]); //
45 };
46
47 template <class Component>
49 return std::any_cast<SparseArray<Component> &>(componentsArrays.at(std::type_index(typeid(Component))));
50 };
51
52 template <typename... Component, typename> // = std::enable_if_t<(sizeof...(Component) >= 1)>
53 void Registry::addComponent(Entity const &to, Component&&... c) {
54 (getComponents<std::remove_reference_t<Component>>().emplaceAt(to._idx, std::forward<Component>(c)), ...);
55 }
56
57 template <typename Component, typename ... Params>
58 void Registry::emplaceComponent(Entity const &to, Params &&... p) {
59 getComponents<Component>().emplaceAt(to._idx, std::forward<Params>(p)...);
60 return getComponents<Component>()[to._idx];
61 };
62
63 template <typename ... Component, typename>
65 (getComponents<Component>().erase(from._idx), ...);
66 };
67
69
70 template<SystemImplementation System>
72 systems[std::type_index(typeid(System))] = std::shared_ptr<ISystem>(&s, +[](ISystem*){});
73 }
74
75 template<SystemImplementation System>
77 systems[std::type_index(typeid(System))] = std::make_shared<System>(std::move(s));
78 }
79
80 template <SystemImplementation... System, typename> // = std::enable_if_t<(sizeof...(System) >= 1)>
82 (addSystem(std::forward<System>(s)), ...);
83 }
84
85 template <SystemImplementation System, typename... Args>
86 void Registry::emplaceSystem(Args&&... args) {
87 systems[std::type_index(typeid(System))] = std::make_shared<System>(std::forward<Args>(args)...);
88 }
89
90 template <SystemImplementation ... System, typename>
92 (systems.erase(std::type_index(typeid(System))), ...);
93 }
94
95 template <SystemImplementation ... System, typename>
97 (systems[std::type_index(typeid(System))]->update(*this), ...);
98 }
99}
100
103#endif // REGISTRY_IMPL_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:29
Definition System.hpp:29
define the Registry class
Definition Registry.hpp:47
void addComponent(Entity const &to, Component &&...c)
Definition Registry_impl.hpp:53
void emplaceSystem(Params &&... params)
add a system to the registry
void callSystem()
call a system
Definition Registry_impl.hpp:96
void removeSystem()
remove a system from the registry
Definition Registry_impl.hpp:91
void registerComponents()
register a list of components
Definition Registry_impl.hpp:31
SparseArray< Component > & registerComponent()
handling components (define in Registry_impl.hpp)
Definition Registry_impl.hpp:24
SparseArray< Component > & getComponents()
get components of a given type
Definition Registry_impl.hpp:43
void addSystem(System &s)
handling systems
Definition Registry_impl.hpp:71
void registerComponentsByExtraction()
register every components by extraction from a tuple
Definition Registry_impl.hpp:36
void emplaceComponent(Entity const &to, Params &&... p)
emplace a component to the given entity
Definition Registry_impl.hpp:58
void removeComponent(Entity const &from)
remove a component from the given entity
Definition Registry_impl.hpp:64
SparseArray class.
Definition SparseArray.hpp:48
Concept that ensures a type derives from System.
Definition System.hpp:58
Entity Component System.