-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyEntityManager.cpp
More file actions
274 lines (255 loc) · 7.04 KB
/
Copy pathMyEntityManager.cpp
File metadata and controls
274 lines (255 loc) · 7.04 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
#include "MyEntityManager.h"
#include "MyCollider.h"
using namespace Simplex;
// MyEntityManager
MyEntityManager* MyEntityManager::m_pInstance = nullptr;
void MyEntityManager::Init(void)
{
m_uEntityCount = 0;
m_entityList.clear();
}
void MyEntityManager::Release(void)
{
for (uint uEntity = 0; uEntity < m_uEntityCount; ++uEntity)
{
MyEntity* pEntity = m_entityList[uEntity];
SafeDelete(pEntity);
}
m_uEntityCount = 0;
m_entityList.clear();
}
MyEntityManager* MyEntityManager::GetInstance()
{
if(m_pInstance == nullptr)
{
m_pInstance = new MyEntityManager();
}
return m_pInstance;
}
void MyEntityManager::ReleaseInstance()
{
if(m_pInstance != nullptr)
{
delete m_pInstance;
m_pInstance = nullptr;
}
}
int Simplex::MyEntityManager::GetEntityIndex(String a_sUniqueID)
{
//look one by one for the specified unique id
for (uint uIndex = 0; uIndex < m_uEntityCount; ++uIndex)
{
if (a_sUniqueID == m_entityList[uIndex]->GetUniqueID())
return uIndex;
}
//if not found return -1
return -1;
}
//Accessors
Model* Simplex::MyEntityManager::GetModel(uint a_uIndex)
{
//if the list is empty return blank
if (m_entityList.size() == 0)
return nullptr;
// if out of bounds
if (a_uIndex >= m_uEntityCount)
a_uIndex = m_uEntityCount - 1;
return m_entityList[a_uIndex]->GetModel();
}
Model* Simplex::MyEntityManager::GetModel(String a_sUniqueID)
{
//Get the entity
MyEntity* pTemp = MyEntity::GetEntity(a_sUniqueID);
//if the entity exists
if (pTemp)
{
return pTemp->GetModel();
}
return nullptr;
}
Collider Simplex::MyEntityManager::GetCollider(uint a_uIndex)
{
// if out of bounds
if (a_uIndex >= m_uEntityCount)
a_uIndex = m_uEntityCount - 1;
return m_entityList[a_uIndex]->GetCollider();
}
Collider Simplex::MyEntityManager::GetCollider(String a_sUniqueID)
{
//Get the entity
MyEntity* pTemp = MyEntity::GetEntity(a_sUniqueID);
//if the entity exists
if (pTemp)
{
return pTemp->GetCollider();
}
throw std::runtime_error("shit got broke yo");
}
matrix4 Simplex::MyEntityManager::GetModelMatrix(uint a_uIndex)
{
//if the list is empty return blank
if (m_entityList.size() == 0)
return IDENTITY_M4;
// if out of bounds
if (a_uIndex >= m_uEntityCount)
a_uIndex = m_uEntityCount - 1;
return m_entityList[a_uIndex]->GetModelMatrix();
}
matrix4 Simplex::MyEntityManager::GetModelMatrix(String a_sUniqueID)
{
//Get the entity
MyEntity* pTemp = MyEntity::GetEntity(a_sUniqueID);
//if the entity exists
if (pTemp)
{
return pTemp->GetModelMatrix();
}
return IDENTITY_M4;
}
void Simplex::MyEntityManager::SetModelMatrix(matrix4 a_m4ToWorld, String a_sUniqueID)
{
//Get the entity
MyEntity* pTemp = MyEntity::GetEntity(a_sUniqueID);
//if the entity exists
if (pTemp)
{
pTemp->SetModelMatrix(a_m4ToWorld);
}
}
void Simplex::MyEntityManager::SetAxisVisibility(bool a_bVisibility, uint a_uIndex)
{
//if the list is empty return blank
if (m_entityList.size() == 0)
return;
// if out of bounds
if (a_uIndex >= m_uEntityCount)
a_uIndex = m_uEntityCount - 1;
return m_entityList[a_uIndex]->SetAxisVisible(a_bVisibility);
}
void Simplex::MyEntityManager::SetAxisVisibility(bool a_bVisibility, String a_sUniqueID)
{
//Get the entity
MyEntity* pTemp = MyEntity::GetEntity(a_sUniqueID);
//if the entity exists
if (pTemp)
{
pTemp->SetAxisVisible(a_bVisibility);
}
}
void Simplex::MyEntityManager::SetModelMatrix(matrix4 a_m4ToWorld, uint a_uIndex)
{
//if the list is empty return blank
if (m_entityList.size() == 0)
return;
// if out of bounds
if (a_uIndex >= m_uEntityCount)
a_uIndex = m_uEntityCount -1;
m_entityList[a_uIndex]->SetModelMatrix(a_m4ToWorld);
}
//The big 3
MyEntityManager::MyEntityManager(){Init();}
MyEntityManager::MyEntityManager(MyEntityManager const& other){ }
MyEntityManager& MyEntityManager::operator=(MyEntityManager const& other) { return *this; }
MyEntityManager::~MyEntityManager(){Release();};
// other methods
void Simplex::MyEntityManager::Update(void)
{
if (m_uEntityCount == 0) { return; }
//check collisions
for (uint i = 0; i < m_uEntityCount - 1; i++)
{
for (uint j = i + 1; j < m_uEntityCount; j++)
{
m_entityList[i]->IsColliding(m_entityList[j]);
}
}
}
void Simplex::MyEntityManager::AddEntity(String a_sFileName, Collider collider, String a_sUniqueID)
{
//Create a temporal entity to store the object
MyEntity* pTemp = new MyEntity(a_sFileName, collider, a_sUniqueID);
//if I was able to generate it add it to the list
if (pTemp->IsInitialized())
{
m_entityList.push_back(pTemp);
m_uEntityCount = m_entityList.size();
} else{
std::cout << "Error" << std::endl;
}
}
void Simplex::MyEntityManager::RemoveEntity(uint a_uIndex)
{
//if the list is empty return
if (m_entityList.size() == 0)
return;
// if out of bounds choose the last one
if (a_uIndex >= m_uEntityCount)
a_uIndex = m_uEntityCount - 1;
// if the entity is not the very last we swap it for the last one
if (a_uIndex != m_uEntityCount - 1)
{
std::swap(m_entityList[a_uIndex], m_entityList[m_uEntityCount - 1]);
}
//and then pop the last one
MyEntity* pTemp = m_entityList[m_uEntityCount - 1];
SafeDelete(pTemp);
m_entityList.pop_back();
--m_uEntityCount;
return;
}
void Simplex::MyEntityManager::RemoveEntity(String a_sUniqueID)
{
int nIndex = GetEntityIndex(a_sUniqueID);
RemoveEntity((uint)nIndex);
}
String Simplex::MyEntityManager::GetUniqueID(uint a_uIndex)
{
//if the list is empty return blank
if (m_entityList.size() == 0)
return "";
//if the index is larger than the number of entries we are asking for the last one
if (a_uIndex >= m_entityList.size())
a_uIndex = m_entityList.size() - 1;
return m_entityList[a_uIndex]->GetUniqueID();
}
MyEntity* Simplex::MyEntityManager::GetEntity(uint a_uIndex)
{
//if the list is empty return blank
if (m_entityList.size() == 0)
return nullptr;
//if the index is larger than the number of entries we are asking for the last one
if (a_uIndex >= m_entityList.size())
a_uIndex = m_entityList.size() - 1;
return m_entityList[a_uIndex];
}
void Simplex::MyEntityManager::AddEntityToRenderList(uint a_uIndex, bool a_bCollider)
{
//if out of bounds will do it for all
if (a_uIndex >= m_uEntityCount)
{
//add for each one in the entity list
for (a_uIndex = 0; a_uIndex < m_uEntityCount; ++a_uIndex)
{
m_entityList[a_uIndex]->AddToRenderList(a_bCollider);
}
}
else //do it for the specified one
{
m_entityList[a_uIndex]->AddToRenderList(a_bCollider);
}
}
void Simplex::MyEntityManager::AddEntityToRenderList(String a_sUniqueID, bool a_bCollider)
{
//Get the entity
MyEntity* pTemp = MyEntity::GetEntity(a_sUniqueID);
//if the entity exists
if (pTemp)
{
pTemp->AddToRenderList(a_bCollider);
}
}
glm::vec3 Simplex::MyEntityManager::GetHalfWidth(uint a_uIndex) {
if (a_uIndex >= m_entityList.size())
a_uIndex = m_entityList.size() - 1;
return m_entityList[a_uIndex]->GetHalfSize();
}