-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntitySystem.cpp
More file actions
314 lines (256 loc) · 8.94 KB
/
EntitySystem.cpp
File metadata and controls
314 lines (256 loc) · 8.94 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//
// Copyright (c) 2020-present Caps Collective & contributors
// Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
//
// This code is released under an unmodified zlib license.
// For conditions of distribution and use, please see:
// https://opensource.org/licenses/Zlib
//
#include "EntitySystem.h"
#include <utils/Logging.h>
#include <algorithm>
#include <cassert>
#include <cstdint>
#include "./Entity.h"
namespace Siege
{
int32_t GetEntityIndex(Entity* entity, const std::vector<Entity*>& storage)
{
if (storage.empty()) return -1;
size_t index = 0;
int targetZIndex = entity->GetZIndex();
bool found = storage[0] == entity;
int32_t foundIndex = 0;
// Return the index of a given element within a given entity vector
while (!found && index < storage.size())
{
// Get the branches of the current node
size_t left = (index * 2) + 1;
size_t right = (index * 2) + 2;
auto searchBranch = [&entity, &storage, targetZIndex](size_t branchIndex) -> size_t {
// Check if the index is in bounds, otherwise return 0
size_t index = (branchIndex < storage.size()) * branchIndex;
// return the entity's index if it exists, otherwise return 0.
return (storage[index]->GetZIndex() == targetZIndex) && (storage[index] == entity);
};
// Search each branch for the entity. If no entity is found, return 0.
size_t isOnLeft = searchBranch(left);
size_t isOnRight = searchBranch(right);
// Check if either branch found the entity
size_t branchResults = (isOnLeft * left) + (isOnRight * right);
// If the entity was found, return the entity's index, otherwise return -1.
foundIndex = (int) branchResults + ((branchResults == 0) * -1);
found = (foundIndex != -1);
index++;
}
return foundIndex;
}
bool EntitySystem::IsLive(Entity* entity)
{
EntitySystem* system = FindInGlobalRegister(entity);
if (system) return system->IsLive(entity->GetIndex());
CC_LOG_WARNING("Could not find storage for provided entity");
return false;
}
void EntitySystem::QueueFree(Entity* entity)
{
EntitySystem* system = FindInGlobalRegister(entity);
if (system)
{
CC_LOG_INFO("Freeing {} at ({})", entity->GetType(), entity->GetIndex().ToString());
system->AddToFreeQueue(entity);
}
else
{
CC_LOG_WARNING("Could not find storage for {} at {}",
entity->GetType(),
entity->GetIndex().ToString());
}
}
void EntitySystem::Resort(Entity* entity, int oldZIdx)
{
EntitySystem* system = FindInGlobalRegister(entity);
if (system) system->SortPartial(entity, oldZIdx);
else
{
CC_LOG_WARNING("Could not find storage for {} at {}",
entity->GetType(),
entity->GetIndex().ToString());
}
}
std::map<const Entity*, EntitySystem*> EntitySystem::globalEntityRegister;
void EntitySystem::Add(Entity* entity)
{
// If the pointer is null, stop the function
if (!entity) return;
// Generate an index and add it to the entity
entity->SetIndex(allocator.AllocateIndex());
// Queue the entity for initialisation
registeredEntities.emplace_back(entity);
}
void EntitySystem::Add(const std::vector<Entity*>& newEntities)
{
if (newEntities.empty()) return;
size_t newEntityCount = newEntities.size();
// Reserve vector space to avoid unnecessary resizing
entities.reserve(entities.size() + newEntityCount);
packedEntities.reserve(packedEntities.size() + newEntityCount);
for (auto& entity : newEntities)
{
entity->SetIndex(allocator.AllocateIndex());
registeredEntities.emplace_back(entity);
}
}
const std::vector<Entity*>& EntitySystem::GetEntities()
{
return packedEntities;
}
void EntitySystem::RegisterEntities()
{
if (registeredEntities.empty()) return;
// Iterate over queued entities and initialise them
for (auto& entity : registeredEntities)
{
// If the entity's given index already exists then override the existing entry
if (entity->GetIndex().index < entities.size()) entities[entity->GetIndex().index] = entity;
else entities.push_back(entity);
CC_LOG_INFO("Registered {} at ({})", entity->GetType(), entity->GetIndex().ToString());
packedEntities.push_back(entity);
AddToGlobalRegister(entity, this);
entity->OnStart();
}
// Once all entities are added, sort the packed storage
SortStorage();
// Remove all entities from the queue
registeredEntities.clear();
}
void EntitySystem::SortStorage()
{
// Sort packedEntities by Z index - lowest to highest
std::sort(packedEntities.begin(), packedEntities.end(), [](Entity* a, Entity* b) {
return a->GetZIndex() < b->GetZIndex();
});
}
void EntitySystem::SortPartial(Entity* entity, int oldZIdx)
{
// Get the index of the entity in packed storage
int32_t index = GetEntityIndex(entity, packedEntities);
if (index != -1)
{
// Check if the new z index is greater than the old z index. If it is, start sorting
// from the entity's index.
bool isGreater = entity->GetZIndex() > oldZIdx;
auto begin = isGreater ? packedEntities.begin() + index : packedEntities.begin();
auto end = isGreater ? packedEntities.end() : packedEntities.begin() + index;
// Sort EntitySystem from either beginning -> entity index, or entity index -> end
std::partial_sort(begin, end, packedEntities.end(), [](Entity* a, Entity* b) {
return a->GetZIndex() < b->GetZIndex();
});
}
}
void EntitySystem::Remove(Entity* entity, std::vector<Entity*>& storage)
{
// Deregistration should always be allowed at this point
assert(allowDeregistration);
// Check if the entity's index is in bounds
if (entity->GetIndex().index >= entities.size()) return;
// De-allocate the entity's index
allocator.Deallocate(entity->GetIndex());
// Get the packed index
int32_t index = GetEntityIndex(entity, storage);
// Erase the packed index entry from packed entity storage if found
if (index != -1) storage.erase(storage.begin() + index);
// Delete the entity from the heap
size_t entityIndex = entity->GetIndex().index;
delete entities[entityIndex];
}
void EntitySystem::AddToFreeQueue(Entity* entity)
{
if (!allowDeregistration) return;
// Ensure that we have no duplicates in the freedEntities vector
int32_t index = GetEntityIndex(entity, freedEntities);
if (index == -1)
{
// Push the entity back into the queue
freedEntities.push_back(entity);
}
}
void EntitySystem::Reset()
{
// Delete all entities that were queued for registration
for (auto& entity : registeredEntities) delete entity;
// Clear queuing storages
registeredEntities.clear();
freedEntities.clear();
RemoveFromGlobalRegister(this);
// Clear packedEntities, packedTools, and the allocator
ClearStorage(packedEntities);
allocator.Reset();
}
void EntitySystem::ClearStorage(std::vector<Entity*>& storage)
{
if (!allowDeregistration) return;
// append storage to the end of freedEntities
freedEntities.insert(freedEntities.begin(), storage.begin(), storage.end());
// Remove the entities from storage
for (auto& e : freedEntities)
{
Remove(e, storage);
}
// Clear all pointers from freedEntities
freedEntities.clear();
}
void EntitySystem::FreeEntities()
{
if (!allowDeregistration) return;
// Iterate over all entities that need to be freed
for (auto& entity : freedEntities)
{
RemoveFromGlobalRegister(entity);
Remove(entity, packedEntities);
}
// Clear the storage.
freedEntities.clear();
}
bool EntitySystem::IsLive(const GenerationalIndex& index)
{
return allocator.IsLive(index);
}
void EntitySystem::SetAllowDeregistration(bool canDeregister)
{
allowDeregistration = canDeregister;
}
void EntitySystem::AddToGlobalRegister(Entity* entity, EntitySystem* system)
{
auto it = globalEntityRegister.find(entity);
if (it == globalEntityRegister.end())
{
globalEntityRegister[entity] = system;
}
}
void EntitySystem::RemoveFromGlobalRegister(Entity* entity)
{
auto it = globalEntityRegister.find(entity);
if (it != globalEntityRegister.end())
{
globalEntityRegister.erase(it);
}
}
void EntitySystem::RemoveFromGlobalRegister(EntitySystem* system)
{
for (auto it = globalEntityRegister.begin(); it != globalEntityRegister.end();)
{
if (it->second == system) it = globalEntityRegister.erase(it);
else ++it;
}
}
EntitySystem* EntitySystem::FindInGlobalRegister(Entity* entity)
{
auto it = globalEntityRegister.find(entity);
return it != globalEntityRegister.end() ? it->second : nullptr;
}
void EntitySystem::ResetGlobalRegister()
{
globalEntityRegister.clear();
}
} // namespace Siege