-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityManager.cpp
More file actions
47 lines (40 loc) · 1.41 KB
/
EntityManager.cpp
File metadata and controls
47 lines (40 loc) · 1.41 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
#include "EngineCommon.hpp"
std::vector< EntityManager::Generation > EntityManager::generations;
std::deque< u32 > EntityManager::freeIndices;
std::unordered_multimap< u32, RmvCompCallback > EntityManager::removeComponentCallbacks;
EntityHandle::operator u32() const {
return this->generation << HANDLE_INDEX_BITS | this->index;
}
void EntityManager::initialize() {
}
void EntityManager::shutdown() {
}
EntityHandle EntityManager::create() {
u32 index;
if ( freeIndices.size() < MIN_FREE_INDICES ) {
generations.push_back( { 0 } );
// we count from 1
index = generations.size();
ASSERT( index < MAX_ENTITIES, "Tried to create more than %d entities", MAX_ENTITIES );
} else {
// we count from 1
index = freeIndices.front() + 1;
freeIndices.pop_front();
}
return { index, generations[ index - 1 ].generation };
}
bool EntityManager::isAlive( EntityHandle entity ) {
return entity.index > 0 && generations[ entity.index - 1 ].generation == entity.generation;
}
void EntityManager::destroy( EntityHandle entity ) {
VALIDATE_ENTITY( entity );
// remove all of this entity's components
auto range = removeComponentCallbacks.equal_range( entity );
for ( auto iter = range.first; iter != range.second; ++iter ) {
( iter->second )( entity );
}
// remove the entity
u32 index = entity.index - 1; // we count from 1
++generations[ index ].generation;
freeIndices.push_back( index );
}