R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
Registry.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** rtype
4** File description:
5** Registry
6*/
7
16#pragma once
17
18#include <any>
19#include <bitset>
20#include <mutex>
21#include <queue>
22#include <shared_mutex>
23#include <unordered_map>
24#include <vector>
25
27
28#define N_MAX_COMPONENTS 64
37namespace ecs {
38
45 typedef std::bitset<N_MAX_COMPONENTS> Signature;
46
52 typedef uint32_t Address;
53
68 class Registry {
69
70 private:
71 mutable std::shared_mutex _mutex;
72 std::unordered_map<Address, Signature> _signatures;
73
75
77
79
80 std::priority_queue<Address, std::vector<Address>, std::greater<Address>> _freeAddresses = {};
81 std::unordered_map<ComponentType, Signature> _componentMap = {};
82 std::unordered_map<ComponentType, std::unordered_map<Address, std::any>> _componentStorage = {};
83
84 public:
91 Registry();
92
96 ~Registry();
97
107
119 template <typename T>
120 void setComponent(Address address, const T &component);
121
130 template <typename T>
132
140 template <typename T>
141 bool hasComponent(Address address);
142
151 template <typename T>
153
167 template <typename T>
168 void addEntityProp(Address address);
169
175 void destroyEntity(Address address);
176
186
207 template <typename... Components>
208 std::vector<Address> view();
209
227 std::vector<Address> getEntitiesWithMask(Signature requiredMask);
228 };
229} // namespace ecs
230
231#include "Registry.tpp"
Manages entities, their signatures and component type registrations.
Definition Registry.hpp:68
std::shared_mutex _mutex
Definition Registry.hpp:71
void removeComponent(Address address)
Remove a component from an entity.
void destroyEntity(Address address)
Remove an entity and its Signature from the registry.
Definition Registry.cpp:56
std::vector< Address > getEntitiesWithMask(Signature requiredMask)
Get all entities matching a specific component mask.
Definition Registry.cpp:79
Signature _registerComponent(ComponentType componentType)
Definition Registry.cpp:32
Address newEntity()
Create and register a new entity, returning its Address.
Definition Registry.cpp:47
std::unordered_map< Address, Signature > _signatures
Definition Registry.hpp:72
std::unordered_map< ComponentType, Signature > _componentMap
Definition Registry.hpp:81
T & getComponent(Address address)
Get a component from an entity.
Address _generateAddress()
Definition Registry.cpp:19
std::priority_queue< Address, std::vector< Address >, std::greater< Address > > _freeAddresses
Definition Registry.hpp:80
bool hasComponent(Address address)
Check if an entity has a specific component.
std::unordered_map< ComponentType, std::unordered_map< Address, std::any > > _componentStorage
Definition Registry.hpp:82
std::vector< Address > view()
Get all entities that have a specific set of components.
Signature getSignature(Address address)
Retrieve the Signature for a given entity address.
Definition Registry.cpp:70
Address _nextAddress
Definition Registry.hpp:74
~Registry()
Destroy the Registry object and clear internal containers.
Definition Registry.cpp:13
void setComponent(Address address, const T &component)
Set/add a component to an entity with its data.
Registry()
Construct a new Registry object.
Definition Registry.cpp:11
void addEntityProp(Address address)
Attach a component type T to an entity (set the component bit).
Maximum number of distinct component types supported by the Registry.
Definition GameLogic.hpp:26
std::size_t ComponentType
Type alias for component identification.
std::uint32_t Address
Type used to represent an entity address/ID.
std::bitset< N_MAX_COMPONENTS > Signature
Bitset representing the set of components attached to an entity.
Definition Registry.hpp:45