forked from phisko/kengine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityManager.cpp
More file actions
250 lines (206 loc) · 6.34 KB
/
Copy pathEntityManager.cpp
File metadata and controls
250 lines (206 loc) · 6.34 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <filesystem>
#include "EntityManager.hpp"
#include "functions/OnTerminate.hpp"
#include "functions/OnEntityRemoved.hpp"
#ifndef KENGINE_MAX_SAVE_PATH_LENGTH
# define KENGINE_MAX_SAVE_PATH_LENGTH 64
#endif
#ifndef KENGINE_MAX_COMPONENT_NAME_LENGTH
# define KENGINE_MAX_COMPONENT_NAME_LENGTH 64
#endif
namespace kengine {
EntityManager::~EntityManager() {
for (const auto & [e, func] : getEntities<functions::OnTerminate>())
func();
}
Entity EntityManager::getEntity(Entity::ID id) {
detail::ReadLock l(_entitiesMutex);
return Entity(id, _entities[id].mask, this);
}
EntityView EntityManager::getEntity(Entity::ID id) const {
detail::ReadLock l(_entitiesMutex);
return EntityView(id, _entities[id].mask);
}
void EntityManager::removeEntity(EntityView e) {
removeEntity(e.id);
}
void EntityManager::removeEntity(Entity::ID id) {
auto e = getEntity(id);
for (const auto & [_, func] : getEntities<functions::OnEntityRemoved>())
func(e);
Entity::Mask mask;
{
detail::ReadLock entities(_entitiesMutex);
mask = _entities[id].mask;
}
{
detail::ReadLock archetypes(_archetypesMutex);
const auto archetype = std::find_if(
_archetypes.begin(), _archetypes.end(),
[mask](const auto & a) { return a.mask == mask; }
);
archetype->remove(id);
}
{
detail::WriteLock entities(_entitiesMutex);
_entities[id].mask = 0;
_entities[id].active = false;
_entities[id].shouldActivateAfterInit = true;
}
detail::WriteLock l(_toReuseMutex);
_toReuse.emplace_back(id);
_toReuseSorted = false;
}
void EntityManager::setEntityActive(EntityView e, bool active) {
setEntityActive(e.id, active);
}
void EntityManager::setEntityActive(Entity::ID id, bool active) {
detail::WriteLock l(_entitiesMutex);
_entities[id].active = active;
_entities[id].shouldActivateAfterInit = active;
}
struct ComponentIDSave {
size_t id;
putils::string<KENGINE_MAX_COMPONENT_NAME_LENGTH> name;
};
Entity EntityManager::alloc() {
{
detail::ReadLock l(_toReuseMutex);
if (_toReuse.empty()) {
detail::WriteLock l(_entitiesMutex);
const auto id = _entities.size();
_entities.push_back({ false, 0 });
return Entity(id, 0, this);
}
}
Entity::ID id;
{
detail::WriteLock l(_toReuseMutex);
if (!_toReuseSorted) {
std::sort(_toReuse.begin(), _toReuse.end(), std::greater<Entity::ID>());
_toReuseSorted = true;
}
id = _toReuse.back();
_toReuse.pop_back();
}
#ifndef KENGINE_NDEBUG
{
detail::ReadLock l(_archetypesMutex);
for (const auto & archetype : _archetypes) {
detail::ReadLock l(archetype.mutex);
assert(std::find(archetype.entities.begin(), archetype.entities.end(), id) == archetype.entities.end());
}
}
{
detail::ReadLock l(_entitiesMutex);
assert(id < _entities.size());
}
#endif
return Entity(id, 0, this);
}
void EntityManager::addComponent(Entity::ID id, size_t component) {
updateHasComponent(id, component, true);
}
void EntityManager::removeComponent(Entity::ID id, size_t component) {
updateHasComponent(id, component, false);
}
void EntityManager::updateHasComponent(Entity::ID id, size_t component, bool newHasComponent) {
Entity::Mask oldMask;
{
detail::ReadLock l(_entitiesMutex);
oldMask = _entities[id].mask;
}
auto updatedMask = oldMask;
updatedMask[component] = newHasComponent;
assert(oldMask != updatedMask);
if (oldMask != 0) {
detail::ReadLock l(_archetypesMutex);
const auto archetype = std::find_if(
_archetypes.begin(), _archetypes.end(),
[oldMask](const auto & a) { return a.mask == oldMask; }
);
archetype->remove(id);
}
if (updatedMask != 0) {
detail::WriteLock l(_archetypesMutex);
const auto archetype = std::find_if(
_archetypes.begin(), _archetypes.end(),
[updatedMask](const auto & a) { return a.mask == updatedMask; }
);
if (archetype != _archetypes.end())
archetype->add(id);
else
_archetypes.emplace_back(updatedMask, id);
}
detail::WriteLock l(_entitiesMutex);
_entities[id].mask = updatedMask;
}
/*
** Collection
*/
EntityManager::EntityCollection EntityManager::getEntities() {
return EntityCollection{ *this };
}
EntityManager::EntityCollection::EntityIterator & EntityManager::EntityCollection::EntityIterator::operator++() {
++index;
detail::ReadLock l(em._entitiesMutex);
while (index < em._entities.size() && (em._entities[index].mask == 0 || !em._entities[index].active))
++index;
return *this;
}
bool EntityManager::EntityCollection::EntityIterator::operator!=(const EntityIterator & rhs) const {
// Use `<` as it will only be compared with `end()`, and there is a risk that new entities have been added since `end()` was called
return index < rhs.index;
}
Entity EntityManager::EntityCollection::EntityIterator::operator*() const {
detail::ReadLock l(em._entitiesMutex);
return Entity(index, em._entities[index].mask, &em);
}
EntityManager::EntityCollection::EntityIterator EntityManager::EntityCollection::begin() const {
size_t i = 0;
detail::ReadLock l(em._entitiesMutex);
while (i < em._entities.size() && (em._entities[i].mask == 0 || !em._entities[i].active))
++i;
return EntityIterator{ i, em };
}
EntityManager::EntityCollection::EntityIterator EntityManager::EntityCollection::end() const {
detail::ReadLock l(em._entitiesMutex);
return EntityIterator{ em._entities.size(), em };
}
/*
** Archetype
*/
EntityManager::Archetype::Archetype(Entity::Mask mask, Entity::ID firstEntity)
: mask(mask), entities({ firstEntity })
{}
EntityManager::Archetype::Archetype(Archetype && rhs) {
mask = rhs.mask;
sorted = rhs.sorted;
detail::WriteLock l(rhs.mutex);
entities = std::move(rhs.entities);
}
EntityManager::Archetype::Archetype(const Archetype & rhs) {
mask = rhs.mask;
sorted = rhs.sorted;
detail::ReadLock l(rhs.mutex);
entities = rhs.entities;
}
void EntityManager::Archetype::add(Entity::ID id) {
detail::WriteLock l(mutex);
entities.push_back(id);
sorted = false;
}
void EntityManager::Archetype::remove(Entity::ID id) {
detail::WriteLock l(mutex);
if (!sorted)
sort();
const auto it = std::lower_bound(entities.begin(), entities.end(), id);
std::swap(*it, entities.back());
entities.pop_back();
sorted = false;
}
void EntityManager::Archetype::sort() {
std::sort(entities.begin(), entities.end(), std::less<Entity::ID>());
sorted = true;
}
}