-
Notifications
You must be signed in to change notification settings - Fork 45
/
Entity.cpp
660 lines (553 loc) · 14 KB
/
Entity.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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#include <cstdio>
#include "Entity.h"
#include "Collision.h"
#include "Graphics.h"
#include "FileNode.h"
#include "MonocleToolkit.h"
#include <sstream>
namespace Monocle
{
InvokeData::InvokeData(void *me, void (*functionPointer) (void *), float delay)
: functionPointer(functionPointer), delay(delay), isDone(false), me(me)
{
}
void InvokeData::Update()
{
if (!isDone)
{
delay -= Monocle::deltaTime;
if (delay < 0)
{
isDone = true;
functionPointer(me);
}
}
}
///=====
EntityTagData::EntityTagData()
{
save = false;
}
EntityTagData::EntityTagData(const std::string &name, bool save)
: name(name), save(save)
{
}
EntityTagData::EntityTagData(const EntityTagData &entityTagData)
: name(entityTagData.name), save(entityTagData.save)
{
}
///=====
Entity::Entity(const Entity &entity)
: Transform(entity), isEnabled(true), followCamera(entity.followCamera), scene(NULL), collider(NULL), graphic(NULL), parent(NULL), depth(entity.depth), isVisible(entity.isVisible), color(entity.color), layer(entity.layer)//, tags(entity.tags)
{
EntityTags copyTags = entity.tags;
for (EntityTags::iterator i = copyTags.begin(); i != copyTags.end(); ++i)
{
AddTag((*i).name, (*i).save);
}
}
Entity::Entity()
: Transform(), isEnabled(true), scene(NULL), collider(NULL), graphic(NULL), parent(NULL), layer(0), depth(0.0f), color(Color::white), isVisible(true)
//, willDie(false)
{
}
Entity::~Entity()
{
}
Entity *Entity::Clone()
{
Debug::Log("Entity::Clone()");
return new Entity(*this);
}
void Entity::Added()
{
}
void Entity::Removed()
{
}
void Entity::Destroyed()
{
if (collider)
{
Collision::RemoveCollider(collider);
delete collider;
collider = NULL;
}
if (graphic)
{
delete graphic;
graphic = NULL;
}
//DestroyChildren();
// clean up invokes
for (std::list<InvokeData*>::iterator i = invokes.begin(); i != invokes.end(); ++i)
{
delete (*i);
}
invokes.clear();
}
//void Entity::DestroyChildren()
//{
// // clean up children
// for (std::list<Entity*>::iterator i = children.begin(); i != children.end(); ++i)
// {
// (*i)->Destroyed();
// delete (*i);
// }
// children.clear();
//}
void Entity::Update()
{
//for(std::list<Entity*>::iterator i = children.begin(); i != children.end(); ++i)
//{
// (*i)->Update();
//}
// clean up invokes
for(std::list<InvokeData*>::iterator i = invokes.begin(); i != invokes.end(); ++i)
{
(*i)->Update();
if ((*i)->isDone)
{
removeInvokes.push_back(*i);
}
}
for (std::list<InvokeData*>::iterator i = removeInvokes.begin(); i != removeInvokes.end(); ++i)
{
delete *i;
invokes.remove(*i);
}
}
void Entity::RemoveSelf()
{
if (scene)
scene->Remove(this);
}
void Entity::Enable()
{
isEnabled = true;
}
void Entity::Disable()
{
isEnabled = false;
}
bool Entity::IsEnabled()
{
return isEnabled;
}
void Entity::ApplyMatrix()
{
if (followCamera == Vector2::zero || (Debug::render && Debug::selectedEntity != this && IsDebugLayer()))
Graphics::Translate(position.x, position.y, depth);
else
{
Camera *camera = scene->GetActiveCamera();
if (!camera)
camera = scene->GetMainCamera();
if (camera != NULL)
Graphics::Translate(camera->position * followCamera + position * (Vector2::one - followCamera));
}
if (rotation != 0.0f)
Graphics::Rotate(rotation, 0, 0, 1);
Graphics::Scale(scale);
}
void Entity::Render()
{
Graphics::PushMatrix();
MatrixChain();
if (graphic != NULL)
{
Graphics::SetColor(color);
graphic->Render(this);
}
Graphics::PopMatrix();
if (Debug::showBounds && IsDebugLayer())
{
Vector2 offset;
if (parent)
offset = Vector2::one * 2;
Graphics::BindTexture(NULL);
Graphics::PushMatrix();
MatrixChain();
/*
if (followCamera == Vector2::zero || Debug::render)
Graphics::Translate(position.x, position.y, depth);
else
Graphics::Translate(scene->GetCamera()->position * followCamera + position * (Vector2::one - followCamera));
*/
if (Debug::selectedEntity == this)
Graphics::SetColor(Color::orange);
else
Graphics::SetColor(Color(0.9f,0.9f,1.0f,0.8f));
Graphics::RenderLineRect(offset.x, offset.y, ENTITY_CONTROLPOINT_SIZE, ENTITY_CONTROLPOINT_SIZE);
if (Debug::selectedEntity != this)
Graphics::SetColor(Color(0.0f,0.0f,0.25f,0.8f));
Graphics::RenderLineRect(offset.x, offset.y, ENTITY_CONTROLPOINT_SIZE * 0.75f, ENTITY_CONTROLPOINT_SIZE * 0.75f);
Graphics::PopMatrix();
}
}
const std::string& Entity::GetTag(int index)
{
#ifdef DEBUG
//Error: If the tag index to get is out of bounds
if (index >= tags.size())
Debug::Log("ERROR: Tag index out of bounds.");
#endif
return tags[index].name;
}
void Entity::AddTag(const std::string& tag, bool save)
{
#ifdef DEBUG
//Error: If the entity already has that tag
if (HasTag(tag))
Debug::Log("ERROR: Duplicate tag added to entity.");
#endif
if (!HasTag(tag))
{
tags.push_back(EntityTagData(tag, save));
if (scene != NULL)
scene->EntityAddTag(this, tag);
}
}
void Entity::RemoveTag(const std::string& tag)
{
#ifdef DEBUG
//Error: If the entity doesn't have that tag
if (!HasTag(tag))
Debug::Log("ERROR: Removing tag from an entity that doesn't have that tag.");
#endif
for (EntityTags::iterator i = tags.begin(); i != tags.end(); ++i)
{
if ((*i).name.compare(tag) == 0)
{
tags.erase(i);
break;
}
}
if (scene != NULL)
scene->EntityRemoveTag(this, tag);
}
bool Entity::HasTag(const std::string& tag)
{
for (EntityTags::iterator i = tags.begin(); i != tags.end(); ++i)
{
if ((*i).name.compare(tag) == 0)
return true;
}
return false;
}
int Entity::GetNumberOfTags()
{
return static_cast<int>(tags.size());
}
int Entity::GetLayer()
{
return layer;
}
void Entity::SetLayer(int layer)
{
this->layer = layer;
//TODO: get scene to re-sort me
}
void Entity::AdjustLayer(int layerAdjustAmount)
{
this->layer += layerAdjustAmount;
}
bool Entity::IsLayer(int layer)
{
return this->layer == layer;
}
bool Entity::IsDebugLayer()
{
if (parent)
return parent->IsDebugLayer();
return layer > Debug::layerMin && layer < Debug::layerMax;
}
/////TODO: enqueue for safety
//// add an entity as a child
//void Entity::Add(Entity *entity)
//{
// entity->parent = this;
// children.push_back(entity);
//}
/////TODO: enqueue for safety
//void Entity::Remove(Entity *entity)
//{
// entity->parent = NULL;
// children.remove(entity);
//}
void Entity::SetCollider(Collider *setCollider)
{
if (setCollider == NULL && this->collider != NULL)
{
// if we want to set null, and we already have a collider
// remove the collider that we had
Collision::RemoveCollider(this->collider);
// set it to null
this->collider = NULL;
// note the code doesn't delete it
}
else if (this->collider != NULL)
{
// we could change this so that it auto-removes the existing collider instead
Debug::Log("Error: Entity already has a collider.");
}
else
{
// set our collider pointer to the passed in collider
this->collider = setCollider;
// register the collider with the Collision manager
Collision::RegisterColliderWithEntity(setCollider, this);
}
}
Collider* Entity::GetCollider()
{
return collider;
}
Collider* Entity::Collide(const std::string &tag, CollisionData *collisionData)
{
return Collision::Collide(this, tag, collisionData);
}
Collider* Entity::CollideAt(const std::string &tag, const Vector2 &atPosition, CollisionData *collisionData)
{
Vector2 oldPosition = position;
position = atPosition;
Collider *collider = Collision::Collide(this, tag, collisionData);
position = oldPosition;
return collider;
}
Collider* Entity::CollideWith(Collider *pCollider, const std::string &tag, CollisionData *collisionData)
{
Collider *collider = Collision::Collide(pCollider, tag, collisionData);
return collider;
}
/*
RectangleCollider* Entity::AddRectangleCollider(float width, float height, const Vector2 &offset)
{
return Collision::AddRectangleCollider(this, width, height, offset);
}
*/
void Entity::SetGraphic(Graphic *graphic)
{
// not sure if we want this yet, sets our graphic's entity pointer to NULL
// if we're about to set the graphic pointer to NULL
if (graphic == NULL && this->graphic != NULL)
{
//this->graphic->entity = NULL;
}
if (this->graphic != NULL)
{
Debug::Log("Note: Entity already has a graphic.");
}
this->graphic = graphic;
}
Graphic* Entity::GetGraphic()
{
return graphic;
}
bool Entity::IsPositionInGraphic(const Vector2 &point)
{
Graphic* graphic = GetGraphic();
if (graphic != NULL)
{
int width, height;
graphic->GetWidthHeight(&width, &height);
Vector2 ul = GetWorldPosition(Vector2( - width * 0.5f, - height * 0.5f));
Vector2 lr = GetWorldPosition(Vector2( + width * 0.5f, + height * 0.5f));
printf("p(%d, %d) ul(%d, %d) lr(%d, %d)\n", (int)point.x, (int)point.y, (int)ul.x, (int)ul.y, (int)lr.x, (int)lr.y);
return (point.x > ul.x && point.x < lr.x && point.y > ul.y && point.y < lr.y);
}
return false;
}
/// TODO: write our own matrix functions to replace this stuff
Vector2 Entity::GetWorldPosition(const Vector2 &position)
{
Vector2 returnPos;
Graphics::PushMatrix();
Graphics::IdentityMatrix();
MatrixChain();
Graphics::Translate(position);
returnPos = Graphics::GetMatrixPosition();
Graphics::PopMatrix();
return returnPos;
}
void Entity::MatrixChain()
{
std::list<Entity*> entityChain;
Entity *current = this;
while (current)
{
entityChain.push_back(current);
current = current->parent;
}
for (std::list<Entity*>::reverse_iterator i = entityChain.rbegin(); i != entityChain.rend(); ++i)
{
(*i)->ApplyMatrix();
//Graphics::Translate(scene->GetCamera()->position * (*i)->followCamera + (*i)->position * (Vector2::one - (*i)->followCamera));
//Graphics::Rotate((*i)->rotation, 0, 0, 1);
//Graphics::Scale((*i)->scale);
}
}
/// TODO: write our own matrix functions to replace this stuff
Vector2 Entity::GetLocalPosition(const Vector2 &worldPosition)
{
Vector2 returnPos;
Graphics::PushMatrix();
Graphics::IdentityMatrix();
std::list<Entity*> entityChain;
Entity *current = this;
while (current)
{
entityChain.push_back(current);
current = current->parent;
}
Graphics::Translate(worldPosition);
for (std::list<Entity*>::iterator i = entityChain.begin(); i != entityChain.end(); ++i)
{
Graphics::Scale(1.0f/(*i)->scale);
Graphics::Rotate(-(*i)->rotation, 0, 0, 1);
Graphics::Translate(-(*i)->position);
}
returnPos = Graphics::GetMatrixPosition();
Graphics::PopMatrix();
return returnPos;
}
//Entity* Entity::GetChildEntityAtPosition(const Vector2 &position)
//{
// for (std::list<Entity*>::iterator i = children.begin(); i != children.end(); ++i)
// {
// Entity *entity = (*i)->GetChildEntityAtPosition(position);
// if (entity)
// return entity;
// }
//
// if (IsPositionInGraphic(position))
// return this;
// return NULL;
//}
void Entity::Save(FileNode *fileNode)
{
Transform::Save(fileNode);
if (layer != 0)
fileNode->Write("layer", layer);
if (color != Color::white)
fileNode->Write("color", color);
if (tags.size() != 0)
{
std::ostringstream os;
for (EntityTags::iterator i = tags.begin(); i != tags.end(); ++i)
{
if ((*i).save)
os << (*i).name << " ";
}
std::string saveString = os.str();
if (!saveString.empty())
{
fileNode->Write("tags", os.str());
}
}
if (followCamera != Vector2::zero)
fileNode->Write("followCamera", followCamera);
}
void Entity::Load(FileNode *fileNode)
{
Transform::Load(fileNode);
int newLayer =0;
fileNode->Read("layer", newLayer);
SetLayer(newLayer);
fileNode->Read("color", color);
std::string tags;
fileNode->Read("tags", tags);
if (tags.size() > 0)
{
std::string tag;
std::istringstream is(tags);
while (is >> tag)
{
AddTag(tag, true);
}
}
fileNode->Read("followCamera", followCamera);
}
void Entity::SetParent(Entity *parent)
{
this->parent = parent;
}
Entity *Entity::GetParent()
{
return parent;
}
Scene* Entity::GetScene()
{
return scene;
}
///// TODO: make recursive
//Entity *Entity::GetNearestEntityByControlPoint(const Vector2 &position, const std::string &tag, Entity *ignoreEntity, float &smallestSqrMag)
//{
// Entity *nearestChild = NULL;
// for (std::list<Entity*>::iterator i = children.begin(); i != children.end(); ++i)
// {
// if ((*i) != ignoreEntity)
// {
// Vector2 wp = (*i)->GetWorldPosition();
// Vector2 diff = wp - position;
// //printf("wp: %f, %f\n", wp.x, wp.y);
// //printf("diff: %f, %f\n", diff.x, diff.y);
// if (diff.IsInRange(ENTITY_CONTROLPOINT_SIZE))
// {
// float sqrMag = diff.GetSquaredMagnitude();
// if (smallestSqrMag == -1 || sqrMag < smallestSqrMag)
// {
// smallestSqrMag = sqrMag;
// nearestChild = (*i);
// }
// }
// }
// Entity *newNearestChild = (*i)->GetNearestEntityByControlPoint(position, tag, ignoreEntity, smallestSqrMag);
// if (newNearestChild)
// {
// nearestChild = newNearestChild;
// }
// }
// return nearestChild;
//}
//const std::list<Entity*>* Entity::GetChildren()
//{
// return &children;
//}
void Entity::Invoke(void (*functionPointer)(void*), float delay)
{
invokes.push_back(new InvokeData((void*)this, functionPointer, delay));
}
/*
void Entity::Die()
{
if (scene)
{
willDie = true;
scene->Remove(this);
}
else
{
Debug::Log("Error: Entity is not in a scene. 'Die' will not function.");
}
}
*/
/*
void Entity::CleanUp()
{
std::list<Entity*> toKill;
for (std::list<Entity*>::iterator i = children.begin(); i != children.end(); ++i)
{
if ((*i)->willDie)
{
toKill.push_back(*i);
}
(*i)->CleanUp();
}
for (std::list<Entity*>::iterator i = toKill.begin(); i != toKill.end(); ++i)
{
children.remove(*i);
}
}
*/
}