/
EntityManager.cpp
139 lines (118 loc) · 3.21 KB
/
EntityManager.cpp
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
/**
* EntityManager handles the adding and removing of entities and any other higher level entity management. Has write access
* to EntityAccessor. Implementation of class declared in EntityManager.h
*
* Author: Skylar Payne
* Date: 7/16/2013
* File: EntityManager.cpp
**/
#include "EntityManager.h"
#include "Entity.h"
#include "IListener.h"
std::vector<Entity*> EntityAccessor::s_Entities;
/**
* @brief EntityManager::~EntityManager removes all entities.
*/
EntityManager::~EntityManager()
{
for(unsigned int i = 0; i < EntityAccessor::s_Entities.size(); i++)
{
if(EntityAccessor::s_Entities[i])
{
delete EntityAccessor::s_Entities[i];
EntityAccessor::s_Entities[i] = nullptr;
}
}
EntityAccessor::s_Entities.clear();
}
/**
* @brief EntityManager::AddEntity Adds an entity to the entity container
* @param e the entity to add
* @return true if entity was added, false otherwise
*/
bool EntityManager::AddEntity(Entity *e)
{
if(e == nullptr)
{
return false;
}
if(_EntitySpaces.empty())
{
e->_ID = EntityAccessor::s_Entities.size();
EntityAccessor::s_Entities.push_back(e);
EntityMessage msg;
msg.ID = e->_ID;
msg.Destroyed = false;
Emit<EntityMessage>(msg);
return true;
}
std::list<unsigned int>::iterator it;
for(it = _EntitySpaces.begin(); it != _EntitySpaces.end(); it++)
{
if(EntityAccessor::s_Entities[*it] == nullptr)
{
e->_ID = *it;
EntityAccessor::s_Entities[*it] = e;
EntityMessage msg;
msg.ID = e->_ID;
msg.Destroyed = false;
Emit<EntityMessage>(msg);
_EntitySpaces.erase(it);
return true;
}
else
{
_EntitySpaces.erase(it);
}
}
return false;
}
/**
* @brief EntityManager::RemoveEntity removes an entity from the container
* @param ID The ID of the entity to remove
*/
void EntityManager::RemoveEntity(unsigned int ID)
{
if(ID > EntityAccessor::s_Entities.size())
{
return;
}
if(EntityAccessor::s_Entities[ID] != nullptr)
{
delete EntityAccessor::s_Entities[ID];
EntityAccessor::s_Entities[ID] = nullptr;
_EntitySpaces.push_back(ID);
EntityMessage msg;
msg.ID = ID;
msg.Destroyed = true;
Emit<EntityMessage>(msg);
}
}
/**
* @brief EntityManager::Update removes all entities at a time where nothing should be iterating through them.
*/
void EntityManager::Update()
{
std::list<unsigned int>::iterator it;
for(it = _EntitiesToRemove.begin(); it != _EntitiesToRemove.end(); it++)
{
RemoveEntity(*it);
}
_EntitiesToRemove.clear();
}
/**
* @brief EntityManager::OnMessage calls AddEntity
* @param msg contains the entity to add
*/
void EntityManager::OnMessage(AddEntityMessage& msg)
{
this->AddEntity(msg.entity);
}
/**
* @brief EntityManager::OnMessage puts the entity ID into a list to be destroyed next frame
* @param msg holds the ID of the entity to destroy
*/
void EntityManager::OnMessage(DestroyEntityMessage& msg)
{
_EntitiesToRemove.push_back(msg.ID);
}