-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityManager.cpp
More file actions
60 lines (49 loc) · 1.55 KB
/
EntityManager.cpp
File metadata and controls
60 lines (49 loc) · 1.55 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
#include "EntityManager.h"
#include <algorithm>
std::array<BitTracker, Sets::MAX_ENTITIES> EntityManager::activeEntityBits;
PackedArray<unsigned short> EntityManager::deadEntities;
unsigned short EntityManager::entityCount = 0;
Entity * EntityManager::CreateEntity()
{
unsigned short id = EntityManager::entityCount;
EntityManager::activeEntityBits[id] = BitTracker();
Entity * newEntity = new Entity(id);
EntityManager::entityCount++;
return newEntity;
}
void EntityManager::AddComponentKey(Entity* entity, uint8_t componentKey) {
BitTracker * entityBits = &EntityManager::activeEntityBits[entity->_id];
if (entityBits != nullptr && !entityBits->Has(componentKey)){
entityBits->Add(componentKey);
}
};
void EntityManager::RemoveComponentKey(Entity* entity, uint8_t componentKey) {
BitTracker* entityBits = &EntityManager::activeEntityBits[entity->_id];
if (entityBits != nullptr && entityBits->Has(componentKey)) {
entityBits->Remove(componentKey);
}
};
void EntityManager::DestroyEntity(Entity* entity)
{
EntityManager::deadEntities.Add(entity->_id);
BitTracker* bits = &EntityManager::activeEntityBits[entity->_id];
for (uint8_t i = 0; i < BitTracker::TOTAL_BITS; i++)
{
if (bits->Has(i))
{
bits->Remove(i);
auto ptr = ComponentManager::allEntityRemoves[i];
ptr(entity->_id);
}
}
EntityManager::activeEntityBits[entity->_id] = BitTracker();
}
Entity::Entity(unsigned short id) {
this->_id = id;
}
bool Entity::Equals(Entity* other) {
return this->_id == other->_id;
}
void Entity::Destroy() {
EntityManager::DestroyEntity(this);
}