This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecs.hpp
446 lines (409 loc) · 12.5 KB
/
ecs.hpp
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#pragma once
#include <iostream>
#include <vector>
#include <chrono>
namespace ecs
{
typedef size_t ID;
const ID NULL_ID = 0-1; // == SIZE_MAX;
typedef std::chrono::nanoseconds Duration;
typedef std::chrono::time_point<std::chrono::high_resolution_clock> TimePoint;
typedef std::chrono::high_resolution_clock Clock;
/********************ENTITIES and COMPONENTS********************/
class Entity
{
template<typename T, typename... Targs>
friend class Iterator;
public:
inline static Entity createEntity();
inline void removeEntity();
template<typename T, typename... Args>
void createComponent(const Args&... args);
template<typename T>
void removeComponent();
template<typename T>
T & getComponent();
template<typename T>
bool hasComponents();
template<typename T1, typename T2, typename... Targs>
bool hasComponents();
ID getID()
{
return entityID;
}
Entity() : entityID(NULL_ID) {}
private:
Entity(ID entityID) : entityID(entityID) {}
ID entityID;
struct EntityEntry
{
std::vector<void (*)(ID)> removeComponentFunctions = std::vector<void (*)(ID)>();
bool exists = true;
};
inline static std::vector<EntityEntry> entityEntryArray = std::vector<EntityEntry>();
inline static std::vector<ID> freeEntityIDs = std::vector<ID>();
template<typename T>
static std::vector<T> componentArray;
template<typename T>
static std::vector<ID> entityToComponentIDs;
inline static void removeEntity(ID entityID);
template<typename T, typename... Args>
static void createComponent(ID entityID, const Args&... args);
template<typename T>
static void removeComponent(ID entityID);
template<typename T>
static T & getComponent(ID entityID);
template<typename T>
static bool hasComponents(ID entityID);
template<typename T1, typename T2, typename... Targs>
static bool hasComponents(ID entityID);
};
template<typename T>
std::vector<T> Entity::componentArray = std::vector<T>();
template<typename T>
std::vector<ID> Entity::entityToComponentIDs = std::vector<ID>();
Entity Entity::createEntity()
{
ID tempID;
if(freeEntityIDs.empty())
{
tempID = entityEntryArray.size();
entityEntryArray.emplace_back();
}
else
{
tempID = freeEntityIDs.back();
freeEntityIDs.pop_back();
entityEntryArray[tempID].exists = true;
}
return Entity(tempID);
}
void Entity::removeEntity()
{
removeEntity(this->entityID);
}
template<typename T, typename... Args>
void Entity::createComponent(const Args&... args)
{
createComponent<T>(this->entityID, args...);
}
template<typename T>
void Entity::removeComponent()
{
removeComponent<T>(this->entityID);
}
template<typename T>
T & Entity::getComponent()
{
return getComponent<T>(this->entityID);
}
template<typename T>
bool Entity::hasComponents()
{
return hasComponents<T>(this->entityID);
}
template<typename T1, typename T2, typename... Targs>
bool Entity::hasComponents()
{
return hasComponents<T1, T2, Targs...>(this->entityID);
}
void Entity::removeEntity(ID entityID)
{
if(entityEntryArray.size() <= entityID | entityEntryArray[entityID].exists == false)
{
throw std::runtime_error("Tried to remove non-existing entity.");
}
freeEntityIDs.push_back(entityID);
for(void (*f)(ID) : entityEntryArray[entityID].removeComponentFunctions)
{
f(entityID);
}
entityEntryArray[entityID].removeComponentFunctions = std::vector<void (*)(ID)>();
entityEntryArray[entityID].exists = false;
}
template<typename T, typename... Args>
void Entity::createComponent(ID entityID, const Args&... args)
{
if(hasComponents<T>(entityID))
{
throw std::runtime_error("Tried to recreate existing component.");
}
if(entityID >= entityToComponentIDs<T>.size())
{
entityToComponentIDs<T>.resize(entityID+1, NULL_ID);
entityToComponentIDs<T>[entityID] = componentArray<T>.size();
componentArray<T>.emplace_back(args...);
}
else if(entityID < entityToComponentIDs<T>.size())
{
bool foundBiggerID = false;
for(ID i = entityID + 1; i < entityToComponentIDs<T>.size(); i++)
{
if(entityToComponentIDs<T>[i] != NULL_ID)
{
if(foundBiggerID == false)
{
componentArray<T>.emplace(componentArray<T>.begin() + entityToComponentIDs<T>[i], args...);
entityToComponentIDs<T>[entityID] = entityToComponentIDs<T>[i];
foundBiggerID = true;
}
entityToComponentIDs<T>[i] += 1;
}
}
}
entityEntryArray[entityID].removeComponentFunctions.push_back(&removeComponent<T>);
}
template<typename T>
void Entity::removeComponent(ID entityID)
{
if(!hasComponents<T>(entityID))
{
throw std::runtime_error("Tried to remove non-existing component.");
}
componentArray<T>.erase(componentArray<T>.begin() + entityToComponentIDs<T>[entityID]);
for(ID i = entityID; i<entityToComponentIDs<T>.size(); i++)
{
if(entityToComponentIDs<T>[i] != NULL_ID)
{
entityToComponentIDs<T>[i] -= 1;
}
}
entityToComponentIDs<T>[entityID] = NULL_ID;
}
template<typename T>
T & Entity::getComponent(ID entityID)
{
if(!hasComponents<T>(entityID))
{
throw std::runtime_error("Tried to access non-existing component.");
}
return componentArray<T>[entityToComponentIDs<T>[entityID]];
}
template<typename T>
bool Entity::hasComponents(ID entityID)
{
return entityToComponentIDs<T>.size() > entityID && entityToComponentIDs<T>[entityID] != NULL_ID;
}
template<typename T1, typename T2, typename... Targs>
bool Entity::hasComponents(ID entityID)
{
return hasComponents<T1>(entityID) && hasComponents<T2, Targs...>(entityID);
}
template<>
inline bool Entity::hasComponents<void>(ID entityID)
{
return entityEntryArray.size() > entityID && entityEntryArray[entityID].exists;
}
template<typename T, typename... Targs>
class Iterator
{
private:
ID entityID;
public:
Iterator() : entityID(NULL_ID) {}
explicit Iterator(ID entityID) : entityID(entityID) {}
explicit Iterator(Entity entity) : entityID(entity.entityID) {}
Iterator<T, Targs...> begin();
Iterator<T, Targs...> end();
void operator= (const Iterator<T, Targs...>& a);
bool operator== (const Iterator<T, Targs...>& a);
bool operator!= (const Iterator<T, Targs...>& a);
Entity operator*();
Entity operator->();
Iterator<T, Targs...> operator++();
};
template<typename T, typename... Targs>
Iterator<T, Targs...> Iterator<T, Targs...>::begin()
{
ID i = 0;
while(end().entityID > i)
{
if(!Entity::hasComponents<T, Targs...>(i))
{
i+=1;
continue;
}
return Iterator<T, Targs...>(i);
}
return end();
}
template<typename T, typename... Targs>
Iterator<T, Targs...> Iterator<T, Targs...>::end()
{
return Iterator<T, Targs...>(Entity::entityEntryArray.size());
}
template<typename T, typename... Targs>
void Iterator<T, Targs...>::operator= (const Iterator<T, Targs...>& a)
{
entityID = a.entityID;
}
template<typename T, typename... Targs>
bool Iterator<T, Targs...>::operator== (const Iterator<T, Targs...>& a)
{
return entityID == a.entityID;
}
template<typename T, typename... Targs>
bool Iterator<T, Targs...>::operator!= (const Iterator<T, Targs...>& a)
{
return entityID != a.entityID;
}
template<typename T, typename... Targs>
Entity Iterator<T, Targs...>::operator*()
{
return Entity(entityID);
}
template<typename T, typename... Targs>
Entity Iterator<T, Targs...>::operator->()
{
return Entity(entityID);
}
template<typename T, typename... Targs>
Iterator<T, Targs...> Iterator<T, Targs...>::operator++() // Prefix Increment
{
entityID+=1;
while(end().entityID > entityID)
{
if(Entity::hasComponents<T, Targs...>(entityID))
{
return Iterator<T, Targs...>(entityID);
}
entityID+=1;
}
entityID = end().entityID;
return end();
}
/********************SYSTEMS********************/
class SystemManager
{
public:
inline static void addSystem(void (*update)(), const Duration& deltaTime);
template<typename T>
static void addSystem(void (*update)(const T&));
inline static void removeSystem(void (*update)());
template<typename T>
static void removeSystem(void (*update)(const T&));
inline static void runSystems();
template<typename T>
static void throwEvent(const T& event);
private:
struct TimeBasedSystem
{
TimeBasedSystem(void (*update)(), Duration deltaTime) :
update(update),
deltaTime(deltaTime),
lastUpdateCallTime(TimePoint())
{
}
void (*update)();
Duration deltaTime;
TimePoint lastUpdateCallTime;
};
struct EventRegisterHelper
{
explicit EventRegisterHelper(void (*f)())
{
runEventBasedSystemsList.push_back(f);
}
};
template<typename T>
static void runEventBasedSystems();
inline static std::vector<TimeBasedSystem> timeBasedSystems = std::vector<SystemManager::TimeBasedSystem>();
inline static std::vector<void(*)()> runEventBasedSystemsList = std::vector<void(*)()>();
template<typename T>
static std::vector<void(*)(const T&)> eventBasedSystem;
template<typename T>
static std::vector<T> eventQueue;
};
template<typename T>
std::vector<void(*)(const T&)> SystemManager::eventBasedSystem = std::vector<void(*)(const T&)>();
template<typename T>
std::vector<T> SystemManager::eventQueue = std::vector<T>();
void SystemManager::addSystem(void (*update)(), const Duration& deltaTime)
{
timeBasedSystems.emplace_back(update, deltaTime);
}
template<typename T>
void SystemManager::addSystem(void (*update)(const T&))
{
eventBasedSystem<T>.push_back(update);
}
void SystemManager::removeSystem(void (*update)())
{
for(size_t i = 0; i<timeBasedSystems.size(); ++i)
{
if(timeBasedSystems[i].update == update)
{
timeBasedSystems.erase(timeBasedSystems.begin() + i);
--i;
}
}
}
template<typename T>
void SystemManager::removeSystem(void (*update)(const T&))
{
for(size_t i = 0; i<eventBasedSystem<T>.size(); ++i)
{
if(eventBasedSystem<T>[i] == update)
{
eventBasedSystem<T>.erase(eventBasedSystem<T>.begin() + i);
--i;
}
}
}
void SystemManager::runSystems()
{
for(TimeBasedSystem & system : timeBasedSystems)
{
TimePoint now = Clock::now();
Duration currentDeltaTime = now - system.lastUpdateCallTime;
if(system.deltaTime <= currentDeltaTime)
{
system.update();
system.lastUpdateCallTime = now;
}
}
for(auto f : runEventBasedSystemsList)
{
f();
}
}
template<typename T>
void SystemManager::throwEvent(const T& event)
{
//register event type
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
const static EventRegisterHelper _(&runEventBasedSystems<T>);
#pragma GCC diagnostic pop
eventQueue<T>.push_back(event);
}
template<typename T>
void SystemManager::runEventBasedSystems()
{
for(T & event : eventQueue<T>)
{
for(auto f : eventBasedSystem<T>)
{
f(event);
}
}
eventQueue<T>.clear();
}
}
/*MIT License
Copyright (c) 2018 tsoj
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/