R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
Registry.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** rtype
4** File description:
5** Registry
6*/
7
8#include "Registry.hpp"
9
10namespace ecs {
11 Registry::Registry() : _nextAddress(1) {}
12
14 _signatures.clear();
15 _componentMap.clear();
16 _componentStorage.clear();
17 }
18
20 // Note: This is called with the mutex already held by newEntity()
21 // Reuse a freed address if available
22 if (!_freeAddresses.empty()) {
23 Address addr = _freeAddresses.top();
24 _freeAddresses.pop();
25 return addr;
26 }
27
28 // Otherwise, generate a new sequential address
29 return _nextAddress++;
30 }
31
33 Signature sign = 0;
34
35 if (_componentMap.contains(componentType))
36 return _componentMap[componentType];
37
38 if (componentType >= N_MAX_COMPONENTS) {
39 return sign;
40 }
41 sign.set(componentType);
42 _componentMap[componentType] = sign;
43
44 return sign;
45 }
46
48 std::unique_lock lock(_mutex);
49 const Address addr = this->_generateAddress();
50 Signature signature;
51
52 _signatures[addr] = signature;
53 return addr;
54 }
55
57 std::unique_lock lock(_mutex);
58 // Remove from signatures
59 _signatures.erase(addr);
60
61 // Remove all components for this entity
62 for (auto &[componentType, storage] : _componentStorage) {
63 storage.erase(addr);
64 }
65
66 // Add address to the pool for reuse
67 _freeAddresses.push(addr);
68 }
69
71 std::shared_lock lock(_mutex);
72 Signature sign = 0u;
73 if (_signatures.contains(address)) {
74 sign = _signatures[address];
75 }
76 return sign;
77 }
78
79 std::vector<Address> Registry::getEntitiesWithMask(Signature requiredMask) {
80 std::shared_lock lock(_mutex);
81 std::vector<Address> result;
82
83 if (requiredMask == 0) {
84 return result;
85 }
86
87 for (const auto &[address, signature] : _signatures) {
88 if ((signature & requiredMask) == requiredMask) {
89 result.push_back(address);
90 }
91 }
92
93 return result;
94 }
95} // namespace ecs
Registry for the ECS (Entity-Component System).
#define N_MAX_COMPONENTS
Definition Registry.hpp:28
std::shared_mutex _mutex
Definition Registry.hpp:71
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
Address _generateAddress()
Definition Registry.cpp:19
std::priority_queue< Address, std::vector< Address >, std::greater< Address > > _freeAddresses
Definition Registry.hpp:80
std::unordered_map< ComponentType, std::unordered_map< Address, std::any > > _componentStorage
Definition Registry.hpp:82
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
Registry()
Construct a new Registry object.
Definition Registry.cpp:11
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