R-Type  2
Doom but in better
Loading...
Searching...
No Matches
Registry.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <unordered_map>
4#include <typeindex>
5#include <any>
6#include <vector>
7#include <functional>
8
9#include "EventDispatcher.hpp"
10#include "Queue.hpp"
12#include "Entity.hpp"
13#include "Systems.hpp"
14#include "SyncingSystem.hpp"
15
23class Registry {
24public:
31 template<typename Component>
33
38 using eraseFunc = std::function<void(Entity const&)>;
39
43 Registry(EventDispatcher *_dispatcher, Queue *_queue);
44
54 template <typename... Components, typename Function>
55 void add_system(Function&& f) {
56 auto system = [f = std::forward<Function>(f), r = this]() {
57 auto component_arrays = r->get_component_array<Components...>();
58 std::apply([&](auto&&... args) {
59 f(*r, std::forward<decltype(args)>(args)...);
60 }, component_arrays);
61 };
62
63 _systems.push_back(system);
64 }
65
69 void run_systems();
70
76 std::vector<GameMessage> sync_game();
77
84 template <class Component>
86 auto type_index = std::type_index(typeid(Component));
87
88 auto erase_function = [&](Entity const& entity) {
89 auto &container = this->get_components<Component>();
90 container.erase(entity);
91 };
92
93 _erase_functions[type_index] = erase_function;
94
95 auto &arr = _components[type_index];
96 if (!arr.has_value()) {
97 arr = std::make_any<ComponentContainer<Component>>();
98 }
99
100 return std::any_cast<ComponentContainer<Component> &>(arr);
101 }
102
109 template <typename... Components>
110 std::tuple<ComponentContainer<Components>&...> get_component_array() {
111 return std::tie(get_components<Components>()...);
112 }
113
120 template <class Component>
122 return std::any_cast<ComponentContainer<Component>&>(_components[typeid(Component)]);
123 }
124
133 template <typename Component>
134 reference<Component> add_component(const Entity& entity, Component&& component) {
135 auto& array = get_components<Component>();
136 array.insert_at(entity.getID(), std::forward<Component>(component));
137 return array[entity];
138 }
139
149 template <typename Component, typename... Params>
150 reference<Component> emplace_component(const Entity& entity, Params&&... params) {
151 auto& array = get_components<Component>();
152 array.emplace_at(entity.getID(), std::forward<Params>(params)...);
153 return array[entity];
154 }
155
162 template <typename Component>
163 void remove_component(const Entity& entity) {
164 auto& array = get_components<Component>();
165 array.erase(entity.getID());
166 }
167
174
181 Entity entity_from_index(std::size_t idx) const;
182
188 void kill_entity(const Entity& entity);
189
190public:
193
194private:
195 std::unordered_map<std::type_index, std::any> _components;
196 std::size_t _next_entity_id;
197 std::vector<std::size_t> _entities;
198 std::unordered_map<std::type_index, eraseFunc> _erase_functions;
199 std::vector<std::function<void()>> _systems;
200};
Registry * r
Definition Tests.cpp:4
Manages a collection of components associated with entities in an ECS (Entity-Component-System) archi...
Represents an entity in an Entity-Component-System (ECS) architecture.
Definition Entity.hpp:12
std::size_t getID() const
Retrieves the ID of the entity.
Definition Entity.cpp:9
A utility class for dispatching and handling game events.
A thread-safe queue for storing and managing GameMessage objects.
Definition Queue.hpp:17
Manages entities and their associated components, enabling the creation, deletion,...
Definition Registry.hpp:23
typename ComponentContainer< Component >::reference reference
A type alias for a reference to a component in a ComponentContainer.
Definition Registry.hpp:32
Entity entity_from_index(std::size_t idx) const
Retrieves an entity by its index.
Definition Registry.cpp:30
void run_systems()
Executes all registered systems.
Definition Registry.cpp:11
void kill_entity(const Entity &entity)
Deletes an entity and removes all its components.
Definition Registry.cpp:35
Entity spawn_entity()
Creates a new entity.
Definition Registry.cpp:23
void remove_component(const Entity &entity)
Removes a component from an entity.
Definition Registry.hpp:163
reference< Component > emplace_component(const Entity &entity, Params &&... params)
Emplaces a component for an entity with constructor parameters.
Definition Registry.hpp:150
EventDispatcher * dispatcher
Definition Registry.hpp:191
std::tuple< ComponentContainer< Components > &... > get_component_array()
Retrieves component containers for multiple component types.
Definition Registry.hpp:110
std::function< void(Entity const &)> eraseFunc
A function type for erasing components associated with an entity.
Definition Registry.hpp:38
void add_system(Function &&f)
Adds a system to the registry.
Definition Registry.hpp:55
ComponentContainer< Component > & register_component()
Registers a new component type.
Definition Registry.hpp:85
std::vector< GameMessage > sync_game()
Creates a list of GameMessages that syncs the player to the game.
Definition Registry.cpp:18
Registry(EventDispatcher *_dispatcher, Queue *_queue)
Default constructor.
Definition Registry.cpp:3
ComponentContainer< Component > & get_components()
Retrieves the component container for a specific type.
Definition Registry.hpp:121
Queue * queue
Definition Registry.hpp:192
reference< Component > add_component(const Entity &entity, Component &&component)
Adds a component to an entity.
Definition Registry.hpp:134