3#include <unordered_map>
31 template<
typename Component>
54 template <
typename... Components,
typename Function>
56 auto system = [f = std::forward<Function>(f),
r =
this]() {
58 std::apply([&](
auto&&... args) {
59 f(*
r, std::forward<
decltype(args)>(args)...);
63 _systems.push_back(system);
84 template <
class Component>
86 auto type_index = std::type_index(
typeid(Component));
88 auto erase_function = [&](
Entity const& entity) {
90 container.erase(entity);
93 _erase_functions[type_index] = erase_function;
95 auto &arr = _components[type_index];
96 if (!arr.has_value()) {
97 arr = std::make_any<ComponentContainer<Component>>();
100 return std::any_cast<ComponentContainer<Component> &>(arr);
109 template <
typename... Components>
120 template <
class Component>
122 return std::any_cast<ComponentContainer<Component>&>(_components[
typeid(Component)]);
133 template <
typename Component>
136 array.insert_at(entity.
getID(), std::forward<Component>(component));
137 return array[entity];
149 template <
typename Component,
typename... Params>
152 array.emplace_at(entity.
getID(), std::forward<Params>(params)...);
153 return array[entity];
162 template <
typename Component>
165 array.erase(entity.
getID());
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;
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.
std::size_t getID() const
Retrieves the ID of the entity.
A utility class for dispatching and handling game events.
A thread-safe queue for storing and managing GameMessage objects.
Manages entities and their associated components, enabling the creation, deletion,...
typename ComponentContainer< Component >::reference reference
A type alias for a reference to a component in a ComponentContainer.
Entity entity_from_index(std::size_t idx) const
Retrieves an entity by its index.
void run_systems()
Executes all registered systems.
void kill_entity(const Entity &entity)
Deletes an entity and removes all its components.
Entity spawn_entity()
Creates a new entity.
void remove_component(const Entity &entity)
Removes a component from an entity.
reference< Component > emplace_component(const Entity &entity, Params &&... params)
Emplaces a component for an entity with constructor parameters.
EventDispatcher * dispatcher
std::tuple< ComponentContainer< Components > &... > get_component_array()
Retrieves component containers for multiple component types.
std::function< void(Entity const &)> eraseFunc
A function type for erasing components associated with an entity.
void add_system(Function &&f)
Adds a system to the registry.
ComponentContainer< Component > & register_component()
Registers a new component type.
std::vector< GameMessage > sync_game()
Creates a list of GameMessages that syncs the player to the game.
Registry(EventDispatcher *_dispatcher, Queue *_queue)
Default constructor.
ComponentContainer< Component > & get_components()
Retrieves the component container for a specific type.
reference< Component > add_component(const Entity &entity, Component &&component)
Adds a component to an entity.