-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGameObject.cpp
More file actions
782 lines (697 loc) · 18.1 KB
/
GameObject.cpp
File metadata and controls
782 lines (697 loc) · 18.1 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
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
#include "Application.h"
#include "ModuleEditor.h"
#include "GameObject.h"
#include "Component.h"
#include "ComponentTransform.h"
#include "ComponentMesh.h"
#include "ComponentMaterial.h"
#include "ComponentCamera.h"
#include "ComponentLight.h"
#include "ComponentAudioSource.h"
#include "ComponentAudioListener.h"
#include "ComponentCollider.h"
#include "ComponentCar.h"
#include "ComponentRectTransform.h"
#include "ComponentUiImage.h"
#include "ComponentUiText.h"
#include "ComponentCanvas.h"
#include "ComponentUiButton.h"
#include "ComponentGrid.h"
#include "ComponentSprite.h"
#include "ComponentParticleSystem.h"
#include "MeshImporter.h"
#include "RaycastHit.h"
#include "ComponentScript.h"
#include "ComponentLight.h"
#include "ComponentAnimation.h"
#include "ComponentBone.h"
#include "ModuleGOManager.h"
#include "ResourceFilePrefab.h"
#include "Random.h"
GameObject::GameObject()
{
name.resize(30);
name = "Empty GameObject";
uuid = App->rnd->RandomInt();
AddComponent(C_TRANSFORM);
}
GameObject::GameObject(GameObject* parent) : parent(parent)
{
name.resize(30);
name = "Empty GameObject";
uuid = App->rnd->RandomInt();
AddComponent(C_TRANSFORM);
if (parent)
{
if (parent->IsPrefab())
{
is_prefab = true;
prefab_path = parent->prefab_path;
prefab_root_uuid = parent->prefab_root_uuid;
}
}
}
GameObject::GameObject(const char* name, unsigned int uuid, GameObject* parent, bool active, bool is_static, bool is_prefab, int layer, unsigned int prefab_root_uuid, const string& prefab_path)
: name(name), uuid(uuid), parent(parent), active(active), is_static(is_static), is_prefab(is_prefab), layer(layer), prefab_root_uuid(prefab_root_uuid), prefab_path(prefab_path)
{
AddComponent(C_TRANSFORM);
}
GameObject::~GameObject()
{
global_matrix = nullptr;
bounding_box = nullptr;
mesh_to_draw = nullptr;
for (std::vector<Component*>::iterator component = components.begin(); component != components.end(); ++component)
{
delete (*component);
(*component) = nullptr;
}
components.clear();
components_to_remove.clear();
if (rc_prefab)
{
rc_prefab->UnloadInstance(this);
rc_prefab->Unload();
}
}
void GameObject::PreUpdate()
{
//Reset elements to draw
mesh_to_draw = nullptr;
//Remove all components that need to be removed. Secure way.
for (std::vector<Component*>::iterator component = components_to_remove.begin(); component != components_to_remove.end(); ++component)
{
for (std::vector<Component*>::iterator comp_remove = components.begin(); comp_remove != components.end(); ++comp_remove) //Remove the component from the components list
{
if ((*comp_remove) == (*component))
{
components.erase(comp_remove);
break;
}
}
delete (*component);
}
components_to_remove.clear();
}
void GameObject::Update()
{
for (std::vector<Component*>::iterator comp = components.begin(); comp != components.end(); comp++)
{
(*comp)->PreUpdate();
}
for (std::vector<Component*>::iterator comp = components.begin(); comp != components.end(); comp++)
{
(*comp)->Update();
}
for (std::vector<Component*>::iterator comp = components.begin(); comp != components.end(); comp++)
{
(*comp)->PostUpdate();
}
}
bool GameObject::AddChild(GameObject* child)
{
bool ret = false;
if (child)
{
childs.push_back(child);
ret = true;
}
return ret;
}
bool GameObject::RemoveChild(GameObject* child)
{
bool ret = false;
if (child)
{
std::vector<GameObject*>::iterator item = childs.begin();
while (item != childs.end())
{
if (*item == child)
{
childs.erase(item);
ret = true;
break;
}
++item;
}
}
return ret;
}
void GameObject::RemoveAllChilds()
{
for (size_t i = 0; i < childs.size(); i++)
{
App->go_manager->FastRemoveGameObject(childs[i]);
}
childs.clear();
}
GameObject* GameObject::GetParent()const
{
return parent;
}
//TODO: keep world transformation matrix, change local
void GameObject::SetParent(GameObject * parent)
{
if (this->parent != parent && parent != this)
{
if (this->parent != nullptr)
{
this->parent->RemoveChild(this);
}
float4x4 global = transform->GetGlobalMatrix();
this->parent = parent;
if (parent)
{
float4x4 new_local = parent->transform->GetGlobalMatrix().Inverted() * global;
float3 translate, scale;
Quat rotation;
new_local.Decompose(translate, rotation, scale);
transform->SetPosition(translate);
transform->SetRotation(rotation);
transform->SetScale(scale);
parent->AddChild(this);
if (rc_prefab == nullptr)
{
if (parent->IsPrefab())
{
prefab_path = parent->prefab_path;
SetAsPrefab(parent->prefab_root_uuid);
}
else
{
if (is_prefab)
{
is_prefab = false;
prefab_root_uuid = 0;
prefab_path = "";
local_uuid = 0;
for (vector<GameObject*>::iterator child = childs.begin(); child != childs.end(); ++child)
{
(*child)->is_prefab = false;
(*child)->prefab_root_uuid = 0;
(*child)->prefab_path = "";
(*child)->local_uuid = 0;
}
}
}
}
}
}
}
const std::vector<GameObject*>* GameObject::GetChilds()
{
return &childs;
}
size_t GameObject::ChildCount()
{
return childs.size();
}
void GameObject::CollectAllChilds(std::vector<GameObject*>& vector)
{
vector.push_back(this);
for (uint i = 0; i < childs.size(); i++)
childs[i]->CollectAllChilds(vector);
}
void GameObject::OnPlay()
{
for (std::vector<Component*>::iterator comp = components.begin(); comp != components.end(); comp++)
{
(*comp)->OnPlay();
}
}
void GameObject::OnStop()
{
for (std::vector<Component*>::iterator comp = components.begin(); comp != components.end(); comp++)
{
(*comp)->OnStop();
}
}
void GameObject::OnPause()
{
for (std::vector<Component*>::iterator comp = components.begin(); comp != components.end(); comp++)
{
(*comp)->OnPause();
}
}
bool *GameObject::GetActiveBoolean()
{
return &active;
}
bool GameObject::IsActive() const
{
// Obtaning all gameobjects from "this" to root, up in the hierarchy
const GameObject *curr_go = this;
std::vector<const GameObject*> hierarchy_gos;
while (curr_go != nullptr && !App->go_manager->IsRoot(curr_go))
{
hierarchy_gos.push_back(curr_go);
curr_go = curr_go->parent;
}
// Checking if all Gameobjects are active
bool is_active = true;
for (std::vector<const GameObject*>::const_iterator it = hierarchy_gos.begin(); it != hierarchy_gos.end(); ++it)
{
if (!(*it)->active)
{
is_active = false; // There is one that it's not active, so...
break; // this gameobject is not active at the end.
}
}
return is_active;
}
void GameObject::SetActive(bool value)
{
if (value != active) active = value;
}
void GameObject::SetAllActive(bool value)
{
if (value != active) active = value;
for (uint i = 0; i < childs.size(); i++)
{
childs[i]->SetAllActive(value);
}
}
bool GameObject::IsStatic() const
{
return is_static;
}
void GameObject::SetStatic(bool value, bool changeChilds)
{ //Conditions: if is static parent MUST be static too. If is dynamic childs must be dynamic too.
if (is_static != value && App->go_manager->IsRoot(this) == false)
{
is_static = value;
if (is_static) //Set parents static too. Except root.
{
bool ret = App->go_manager->InsertGameObjectInOctree(this);
if (!ret)
LOG("INSERTION FAILED");
if (parent)
parent->SetStatic(true);
}
else //Set childs dynamic too
{
bool ret = App->go_manager->RemoveGameObjectOfOctree(this);
if (!ret)
LOG("REMOVING FAILED");
for (std::vector<GameObject*>::iterator child = childs.begin(); child != childs.end(); ++child)
(*child)->SetStatic(false);
}
if (changeChilds && childs.empty() == false)
{
for (std::vector<GameObject*>::iterator it = childs.begin(); it != childs.end(); it++)
{
(*it)->SetStatic(value, changeChilds);
}
}
}
}
void GameObject::ForceStatic(bool value)
{
//Only inserts the gameobject in the octree or dynamic vector. Doesn't remove if was inserted previously. Doesn't update parent or children
is_static = value;
if (is_static)
{
bool ret = App->go_manager->InsertGameObjectInOctree(this);
if (!ret)
LOG("INSERTION FAILED");
}
else
{
bool ret = App->go_manager->RemoveGameObjectOfOctree(this);
if (!ret)
LOG("REMOVING FAILED");
}
}
void GameObject::SetAsPrefab(unsigned int root_uuid)
{
is_prefab = true;
local_uuid = uuid;
prefab_root_uuid = root_uuid;
for (vector<GameObject*>::iterator child = childs.begin(); child != childs.end(); ++child)
(*child)->SetAsPrefab(root_uuid);
}
bool GameObject::IsPrefab() const
{
return is_prefab;
}
void GameObject::SetLayerChilds(int _layer)
{
layer = _layer;
for (vector<GameObject*>::iterator child = childs.begin(); child != childs.end(); ++child)
(*child)->SetLayerChilds(_layer);
}
Component* GameObject::AddComponent(ComponentType type)
{
Component* item = nullptr;
switch (type)
{
case C_TRANSFORM:
if (!transform) //Only one transform component for gameobject
{
item = new ComponentTransform(type, this, &global_matrix);
transform = (ComponentTransform*)item;
}
break;
case C_MESH:
if(transform)
item = new ComponentMesh(type, this);
break;
case C_MATERIAL:
if (transform && GetComponent(C_MESH))
item = new ComponentMaterial(type, this);
break;
case C_CAMERA:
if (transform)
item = new ComponentCamera(type, this);
break;
case C_COLLIDER:
if (transform)
item = new ComponentCollider(this);
break;
case C_LIGHT:
if (transform)
item = new ComponentLight(type, this);
break;
case C_ANIMATION:
if (transform && GetComponent(C_ANIMATION) == nullptr)
item = new ComponentAnimation(this);
break;
case C_BONE:
if (transform)
item = new ComponentBone(this);
break;
case C_CAR:
if (transform)
item = new ComponentCar(this);
break;
case C_AUDIO_SOURCE:
if (transform)
item = new ComponentAudioSource(type, this);
break;
case C_AUDIO_LISTENER:
if (transform)
item = new ComponentAudioListener(type, this);
break;
case C_SCRIPT:
item = new ComponentScript(type, this);
break;
case C_RECT_TRANSFORM:
if (GetComponent(type) == nullptr) // Only one rect transform component for gameobject
item = new ComponentRectTransform(type, this);
break;
case C_UI_IMAGE:
if (!GetComponent(C_RECT_TRANSFORM)) AddComponent(C_RECT_TRANSFORM);
item = new ComponentUiImage(type, this);
break;
case C_UI_TEXT:
if (!GetComponent(C_RECT_TRANSFORM)) AddComponent(C_RECT_TRANSFORM);
item = new ComponentUiText(type,this);
break;
case C_UI_BUTTON:
if (!GetComponent(C_RECT_TRANSFORM)) AddComponent(C_RECT_TRANSFORM);
item = new ComponentUiButton(type, this);
break;
case C_GRID:
if (!GetComponent(C_RECT_TRANSFORM)) AddComponent(C_RECT_TRANSFORM);
item = new ComponentGrid(type, this);
break;
case C_CANVAS:
if (App->go_manager->current_scene_canvas == nullptr)
{
if (!GetComponent(C_RECT_TRANSFORM)) AddComponent(C_RECT_TRANSFORM);
item = new ComponentCanvas(type, this);
App->go_manager->current_scene_canvas = (ComponentCanvas*)item;
}
break;
case C_SPRITE:
if (transform)
item = new ComponentSprite(type, this);
break;
case C_PARTICLE_SYSTEM:
item = new ComponentParticleSystem(type, this);
break;
default:
LOG("[WARNING] Unknown type specified for GameObject %s", name);
App->editor->DisplayWarning(WarningType::W_WARNING, "Unknown type specified for GameObject %s", name);
break;
}
if (item != nullptr)
{
components.push_back(item);
}
else
{
LOG("[ERROR] When adding component to %s", this->name.c_str());
App->editor->DisplayWarning(WarningType::W_ERROR, "When adding component to %s", this->name.c_str());
}
return item;
}
const std::vector<Component*>* GameObject::GetComponents()
{
return &components;
}
Component* GameObject::GetComponent(ComponentType type)const
{
if (components.empty() == false)
{
if (type == C_TRANSFORM)
return transform;
std::vector<Component*>::const_iterator comp = components.begin();
while (comp != components.end())
{
if ((*comp)->GetType() == type)
{
return (*comp);
}
++comp;
}
}
return nullptr;
}
Component* GameObject::GetComponentInChilds(ComponentType type) const
{
Component* ret = nullptr;
if ((ret = GetComponent(type)) != nullptr)
return ret;
vector<GameObject*>::const_iterator it = childs.begin();
for (it; it != childs.end(); ++it)
{
if ((ret = (*it)->GetComponentInChilds(type)) != nullptr)
{
return ret;
}
}
return ret;
}
void GameObject::GetComponentsInChilds(ComponentType type, std::vector<Component*>& vector) const
{
Component* comp = GetComponent(type);
if (comp != nullptr)
vector.push_back(comp);
std::vector<GameObject*>::const_iterator it = childs.begin();
for (it; it != childs.end(); ++it)
{
(*it)->GetComponentsInChilds(type, vector);
}
}
void GameObject::RemoveComponent(Component * component)
{
//Search if the component exists inside the GameObject
std::vector<Component*>::iterator it = components.begin();
for (it; it != components.end(); ++it)
{
if ((*it) == component)
{
components_to_remove.push_back(component);
break;
}
}
}
float4x4 GameObject::GetGlobalMatrix() const
{
return (global_matrix) ? *global_matrix : float4x4::identity;
}
unsigned int GameObject::GetUUID() const
{
return uuid;
}
void GameObject::TransformModified()
{
if (global_matrix == nullptr)
return;
std::vector<Component*>::iterator component = components.begin();
for (component; component != components.end(); component++)
{
(*component)->OnTransformModified();
}
}
void GameObject::Save(Data & file, bool ignore_prefab) const
{
Data data;
//GameObject data
data.AppendString("name", name.data());
data.AppendUInt("UUID", uuid);
data.AppendUInt("local_UUID", local_uuid);
if(parent == nullptr)
data.AppendUInt("parent", 0);
else
data.AppendUInt("parent", parent->GetUUID());
data.AppendBool("active", active);
data.AppendBool("is_prefab", is_prefab);
data.AppendUInt("prefab_root_uuid", prefab_root_uuid);
data.AppendString("prefab_path", prefab_path.data());
if (is_prefab == false || ignore_prefab == true)
{ //Normal GameObject
data.AppendInt("layer", layer);
data.AppendBool("static", is_static);
data.AppendArray("components");
//Components data
vector<Component*>::const_iterator component = components.begin();
for (component; component != components.end(); component++)
{
(*component)->Save(data);
}
file.AppendArrayValue(data);
for (vector<GameObject*>::const_iterator child = childs.begin(); child != childs.end(); ++child)
(*child)->Save(file, ignore_prefab);
}
else
{ //Prefab GameObject
//Save Component Transform (translation & rotation)
ComponentTransform* c_transform = (ComponentTransform*)GetComponent(ComponentType::C_TRANSFORM);
data.AppendArray("components");
c_transform->SaveAsPrefab(data);
//Save all children uuids
data.AppendArray("children_uuids");
for (vector<GameObject*>::const_iterator child = childs.begin(); child != childs.end(); ++child)
(*child)->SaveAsChildPrefab(data);
file.AppendArrayValue(data);
}
}
void GameObject::SaveAsChildPrefab(Data & file) const
{
Data data;
data.AppendUInt("uuid", uuid);
data.AppendUInt("local_uuid", local_uuid);
for (vector<GameObject*>::const_iterator child = childs.begin(); child != childs.end(); ++child)
(*child)->SaveAsChildPrefab(file);
file.AppendArrayValue(data);
}
bool GameObject::RayCast(Ray raycast, RaycastHit & hit_OUT)
{
RaycastHit hit_info;
bool ret = false;
ComponentMesh* c_mesh = (ComponentMesh*)GetComponent(C_MESH);
if (c_mesh)
{
const Mesh* mesh = c_mesh->GetMesh();
if (mesh)
{
//Transform ray into local coordinates
raycast.Transform(global_matrix->Inverted());
uint u1, u2, u3;
float distance;
vec hit_point;
Triangle triangle;
for (unsigned int i = 0; i < mesh->num_indices; i+=3)
{
u1 = mesh->indices[i];
u2 = mesh->indices[i+1];
u3 = mesh->indices[i+2];
triangle = Triangle(float3(&mesh->vertices[u1 * 3]), float3(&mesh->vertices[u2 * 3]), float3(&mesh->vertices[u3 * 3]));
if (raycast.Intersects(triangle, &distance, &hit_point))
{
ret = true;
if (hit_info.distance > distance || hit_info.distance == 0)
{
hit_info.distance = distance;
hit_info.point = hit_point;
hit_info.normal = triangle.NormalCCW();
}
}
if (ret == true)
{
//Transfrom the hit parameters to global coordinates
hit_OUT.point = global_matrix->MulPos(hit_info.point);
hit_OUT.distance = hit_info.distance;
//hit_OUT.distance = ray.pos.Distance(hit.point);
hit_OUT.object = this;
hit_OUT.normal = global_matrix->MulDir(hit_info.normal); //TODO: normal needs revision. May not work as expected.
hit_OUT.normal.Normalize();
}
}
}
}
return ret;
}
void GameObject::ApplyPrefabChanges()
{
if (is_prefab)
{
if (rc_prefab)
{
rc_prefab->ApplyChanges(this);
}
else
{
GameObject* prefab_go = App->go_manager->FindGameObjectByUUID(App->go_manager->root, prefab_root_uuid);
if (prefab_go)
prefab_go->rc_prefab->ApplyChanges(prefab_go);
}
}
}
void GameObject::CollectChildrenUUID(vector<unsigned int>& uuid, vector<unsigned int>& local_uuid) const
{
vector<GameObject*>::const_iterator it = childs.begin();
for (it; it != childs.end(); ++it)
{
uuid.push_back((*it)->GetUUID());
local_uuid.push_back((*it)->local_uuid);
(*it)->CollectChildrenUUID(uuid, local_uuid);
}
}
void GameObject::RevertPrefabChanges()
{
if (is_prefab)
{
if (rc_prefab)
{
rc_prefab->RevertChanges(this);
}
else
{
GameObject* prefab_go = App->go_manager->FindGameObjectByUUID(App->go_manager->root, prefab_root_uuid);
if (prefab_go)
prefab_go->rc_prefab->RevertChanges(prefab_go);
}
}
}
void GameObject::UnlinkPrefab()
{
if (is_prefab)
{
if (rc_prefab == nullptr)
{
GameObject* prefab_go = App->go_manager->FindGameObjectByUUID(App->go_manager->root, prefab_root_uuid);
if (prefab_go && prefab_go->IsPrefab())
{
prefab_go->UnlinkPrefab();
return;
}
}
else
{
rc_prefab->Unload();
rc_prefab->UnloadInstance(this);
rc_prefab = nullptr;
}
is_prefab = false;
prefab_path = "";
prefab_root_uuid = 0;
local_uuid = 0;
for (std::vector<GameObject*>::iterator it = childs.begin(); it != childs.end(); it++)
{
(*it)->UnlinkPrefab();
}
}
}