-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistry.cpp
More file actions
154 lines (131 loc) · 3.18 KB
/
Registry.cpp
File metadata and controls
154 lines (131 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** Registry
*/
#include "Registry.hpp"
#include <string>
#include "Clock.hpp"
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
Registry Registry::_instance = Registry();
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
Registry &Registry::getInstance()
{
return _instance;
}
std::size_t Registry::addEntity()
{
for (auto function : _addComponentPlaceFunctions) {
function(*this);
}
_entitiesNb++;
return _entitiesNb - 1;
}
void Registry::removeEntity(std::size_t id)
{
unloadRaylibComponents(id);
for (auto function : _removeComponentFunctions) {
function(*this, id);
}
}
void Registry::clear()
{
for (std::size_t i = 0; i < _entitiesNb; i++) {
unloadRaylibComponents(i);
}
_data.clear();
_addComponentPlaceFunctions.clear();
_removeComponentFunctions.clear();
_getExistingsId.clear();
_entitiesNb = 0;
}
static std::vector<std::size_t>
match(std::vector<std::size_t> fst, std::vector<std::size_t> scd)
{
std::vector<std::size_t> res;
for (auto it = fst.begin(); it != fst.end(); it++) {
for (auto scdIt = scd.begin(); scdIt != scd.end(); scdIt++) {
if (*it == *scdIt) {
res.push_back(*it);
}
}
}
return res;
}
std::vector<std::size_t> Registry::getExistings(std::type_index type)
{
auto funcIt = _getExistingsId.find(type);
if (funcIt == _getExistingsId.end()) {
return {};
}
return (funcIt->second)(*this);
}
std::vector<std::size_t>
Registry::getEntitiesByComponents(std::vector<std::type_index> types)
{
auto it = types.begin();
std::vector<std::size_t> res = getExistings(*it);
it++;
for (; it != types.end(); it++) {
res = match(res, getExistings(*it));
}
return res;
}
void Registry::setToBackLayers(std::size_t id, BackLayers layer)
{
removeFromDefaultLayer(id);
_backLayers[layer].push_back(id);
}
void Registry::setToDefaultLayer(std::size_t id)
{
_defaultLayer.push_back(id);
}
void Registry::setToFrontLayers(std::size_t id, FrontLayers layer)
{
_backLayers[layer].push_back(id);
}
std::vector<std::vector<std::size_t>> Registry::getBackLayers()
{
return _backLayers;
}
std::vector<std::size_t> Registry::getDefaultLayer()
{
return _defaultLayer;
}
std::vector<std::vector<std::size_t>> Registry::getFrontLayers()
{
return _frontLayers;
}
void Registry::initLayers(bool back)
{
auto max = static_cast<std::size_t>(
back ? BackLayers::BACKMAX : FrontLayers::FRONTMAX);
for (std::size_t i = 0; i < max; i++) {
std::vector<std::vector<std::size_t>> &layers =
back ? _backLayers : _frontLayers;
layers.emplace_back();
}
if (back) {
initLayers(false);
}
}
void Registry::removeFromDefaultLayer(std::size_t id)
{
auto i = _defaultLayer.end();
while (i != _defaultLayer.begin()) {
--i;
if (*i == id) {
_defaultLayer.erase(i);
break;
}
}
}
Registry::Registry() : _entitiesNb(0)
{
initLayers(true);
}
Clock &Registry::getClock()
{
return _clock;
}