ecs
Loading...
Searching...
No Matches
Registry_impl.hpp
Go to the documentation of this file.
1
8#ifndef REGISTRY_IMPL_HPP
9#define REGISTRY_IMPL_HPP
10
11#include "Registry.hpp"
12#include "Entity.hpp"
13#include "SparseArray.hpp"
14#include "Concepts.hpp"
15
16#include "System.hpp"
17
18namespace ecs {
19
20 template <class Component>
22 componentsArrays[std::type_index(typeid(Component))] = std::make_any<SparseArray<Component>>();
23 componentsRemoves.push_back([] (Registry &r, Entity const &e) { r.getComponents<Component>().erase(e._idx); } );
24 return std::any_cast<SparseArray<Component> &>(componentsArrays[std::type_index(typeid(Component))]);
25 };
26
27 template <class ... Components>
31
32 template <IsTuple Tuple>
34 std::apply([this](auto ... components) {
35 this->registerComponents<std::decay_t<decltype(components)>...>();
36 }, Tuple{});
37 }
38
39 template <class Component>
41 return std::any_cast<SparseArray<Component> &>(componentsArrays[std::type_index(typeid(Component))]); //
42 };
43
44 template <class Component>
46 return std::any_cast<SparseArray<Component> &>(componentsArrays.at(std::type_index(typeid(Component))));
47 };
48
49 template <typename... Component, typename> // = std::enable_if_t<(sizeof...(Component) >= 1)>
50 void Registry::addComponent(Entity const &to, Component&&... c) {
51 (getComponents<std::remove_reference_t<Component>>().emplaceAt(to._idx, std::forward<Component>(c)), ...);
52 }
53
54 template <typename Component, typename ... Params>
55 void Registry::emplaceComponent(Entity const &to, Params &&... p) {
56 getComponents<Component>().emplaceAt(to._idx, std::forward<Params>(p)...);
57 return getComponents<Component>()[to._idx];
58 };
59
60 template <typename ... Component, typename>
62 (getComponents<Component>().erase(from._idx), ...);
63 };
64
66
67 template<SystemImplementation System>
69 systems[std::type_index(typeid(System))] = std::shared_ptr<ISystem>(&s, +[](ISystem*){});
70 }
71
72 template<SystemImplementation System>
74 systems[std::type_index(typeid(System))] = std::make_shared<System>(std::move(s));
75 }
76
77 template <SystemImplementation... System, typename> // = std::enable_if_t<(sizeof...(System) >= 1)>
79 (addSystem(std::forward<System>(s)), ...);
80 }
81
82 template <SystemImplementation System, typename... Args>
83 void Registry::emplaceSystem(Args&&... args) {
84 systems[std::type_index(typeid(System))] = std::make_shared<System>(std::forward<Args>(args)...);
85 }
86
87 template <SystemImplementation ... System, typename>
89 (systems.erase(std::type_index(typeid(System))), ...);
90 }
91
92 template <SystemImplementation ... System, typename>
94 (systems[std::type_index(typeid(System))]->update(*this), ...);
95 }
96}
97
98#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:26
Definition System.hpp:26
define the Registry class
Definition Registry.hpp:44
void addComponent(Entity const &to, Component &&...c)
Definition Registry_impl.hpp:50
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
SparseArray< Component > & getComponents()
get components of a given type
Definition Registry_impl.hpp:40
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.