-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameObject.cpp
247 lines (184 loc) · 6.96 KB
/
GameObject.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
#include "GameObject.h"
#include "ComponentMesh.h"
#include "imgui.h"
#include "ComponentCamera.h"
#include "ComponentAABB.h"
#include "ModuleJSON.h"
#include "MD5.h"
GameObject::GameObject(const char* obj_name, GameObject* parent) : name(obj_name), parent(parent), uid(CreateUID(name.c_str(), name.length()))
{}
GameObject::GameObject(GameObject& game_obj)
{
parent = game_obj.parent;
name = game_obj.name + "_copy";
uid = CreateUID(name.c_str(), name.length());
for (std::vector<Component*>::iterator it = components.begin(); it != components.end(); it++)
components.push_back((*it)->Duplicate());
for (std::vector<GameObject*>::iterator it = children.begin(); it != children.end(); it++)
children.push_back(new GameObject(*(*it)));
}
GameObject::GameObject()
{}
GameObject::~GameObject()
{
for (std::vector<Component*>::iterator it = components.begin(); it != components.end(); it++) {
if ((*it)->type != COMPONENT_MATERIAL) // materials are stored and deleted by scene_intro
delete *it;
}
children.clear();
}
Component* GameObject::FindComponent(Component_type type) {
for (std::vector<Component*>::iterator it = components.begin(); it != components.end(); it++) {
if ((*it)->type == type)
return *it;
}
return nullptr;
}
std::vector<Component*> GameObject::FindComponents(Component_type type) {
std::vector<Component*> ret;
for (std::vector<Component*>::iterator it = components.begin(); it != components.end(); it++) {
if ((*it)->type == type)
ret.push_back(*it);
}
return ret;
}
GameObject* GameObject::FindChild(const char* obj_name) {
for (std::vector<GameObject*>::iterator it = children.begin(); it != children.end(); it++) {
if ((*it)->name == obj_name)
return *it;
}
return nullptr;
}
void GameObject::Update(float dt)
{
for (std::vector<Component*>::iterator it = components.begin(); it != components.end(); it++) {
if ((*it)->enabled)
(*it)->Update(dt);
}
}
void GameObject::Draw()
{
for (std::vector<Component*>::iterator it = components.begin(); it != components.end(); it++)
(*it)->Draw();
}
bool GameObject::Cull(ComponentCamera* cam)
{
ComponentAABB* aabb = (ComponentAABB*)FindComponent(COMPONENT_AABB);
if (!aabb) {
components.push_back(new ComponentAABB(*this)); // safeguard in case the gameobject has no AABB
aabb = (ComponentAABB*)components.back();
}
return cam->FrustumCulling(*aabb);
}
void GameObject::RayCastAgainstAABBs(math::Ray ray, std::list<RayHit>& outHits) {
ComponentAABB* comp_aabb = (ComponentAABB*)FindComponent(COMPONENT_AABB);
if (!comp_aabb) {
components.push_back(new ComponentAABB(*this)); // safeguard in case the gameobject has no AABB
comp_aabb = (ComponentAABB*)components.back();
}
float3 min_point = comp_aabb->GetMinP();
float3 max_point = comp_aabb->GetMaxP();
AABB aabb(min_point, max_point);
float near_d = 0; float far_d = 0;
if (ray.Intersects(aabb, near_d, far_d)) {
RayHit hit;
hit.hit_distance = near_d; hit.object = this;
outHits.push_back(hit);
}
}
void GameObject::RayCastAgainstMeshes(Ray ray, std::list<RayHit>& outHits) {
std::vector<Component*> meshes = FindComponents(COMPONENT_MESH);
if (meshes.empty())
return;
else
{
for (std::vector<Component*>::iterator it = meshes.begin(); it != meshes.end(); it++) {
ComponentMesh* mesh = (ComponentMesh*)(*it);
for (int i = 0; i < mesh->num_tris; i += 3) {
float3 vert1 = mesh->vertices[mesh->tris[i].vert1];
float3 vert2 = mesh->vertices[mesh->tris[i].vert2];
float3 vert3 = mesh->vertices[mesh->tris[i].vert3];
Triangle tri(vert1, vert2, vert3);
if (ray.Intersects(tri)) {
RayHit hit;
hit.object = this;
hit.hit_distance = tri.CenterPoint().Distance(ray.pos);
outHits.push_back(hit);
}
}
}
}
}
void GameObject::HierarchyTree(std::string& selected_obj_uid)
{
int flags = 0;
if (children.empty()) flags |= ImGuiTreeNodeFlags_Leaf;
if (selected_obj_uid == uid) flags |= ImGuiTreeNodeFlags_Selected;
if (ImGui::TreeNodeEx(name.c_str(), flags)) {
if (ImGui::IsItemClicked()) selected_obj_uid = uid;
for (int aux = 0; aux < children.size(); aux++) children[aux]->HierarchyTree(selected_obj_uid);
ImGui::TreePop();
}
//CHANGE STARTING LOADING SCHEME
//CITY TEST
}
void GameObject::Save(JSON_file& save_file, uint& obj_index)
{
std::string base_name = ("objects.");
base_name.append(std::to_string(obj_index));
save_file.WriteString(std::string(".name").insert(0, base_name).c_str(), name.c_str());
save_file.WriteString(std::string(".uid").insert(0, base_name).c_str(), uid.c_str());
save_file.WriteBool(std::string(".enabled").insert(0, base_name).c_str(), enabled);
if(parent)
save_file.WriteString(std::string(".parent_uid").insert(0, base_name).c_str(), parent->uid.c_str());
else
save_file.WriteString(std::string(".parent_uid").insert(0, base_name).c_str(), "none");
save_file.WriteNumber(std::string(".number of components").insert(0, base_name).c_str(), components.size());
uint comp_index = 0;
for (std::vector<Component*>::iterator it = components.begin(); it != components.end(); it++, comp_index++)
{
std::string component_code(base_name);
component_code.append(".components."); component_code.append(std::to_string(comp_index));
(*it)->Save(save_file, component_code.c_str());
}
}
void GameObject::Load(JSON_file& save_file, uint& obj_index)
{
std::string base_name = ("objects.");
base_name.append(std::to_string(obj_index));
name = save_file.ReadString(std::string(".name").insert(0, base_name).c_str());
uid = save_file.ReadString(std::string(".uid").insert(0, base_name).c_str());
enabled = save_file.ReadBool(std::string(".enabled").insert(0, base_name).c_str());
parent_uid = save_file.ReadString(std::string(".parent_uid").insert(0, base_name).c_str());
// parent pointer MUST be setup by scene loading method AFTER game object loading
uint comp_num = save_file.ReadNumber(std::string(".number of components").insert(0, base_name).c_str());
for (uint comp_index = 0; comp_index < comp_num; comp_index++)
{
std::string component_code(base_name);
component_code.append(".components."); component_code.append(std::to_string(comp_index));
Component_type type = (Component_type)((uint)save_file.ReadNumber(std::string(".type").insert(0, component_code).c_str()));
Component* new_comp = nullptr;
switch (type)
{
case COMPONENT_MESH: new_comp = (Component*) new ComponentMesh(); break;
case COMPONENT_TRANSFORM: new_comp = (Component*) new ComponentTransform(); break;
case COMPONENT_CAMERA: new_comp = (Component*) new ComponentCamera(); break;
case COMPONENT_AABB: new_comp = (Component*) new ComponentAABB(); break;
}
if (new_comp)
{
new_comp->Load(save_file, component_code.c_str());
components.push_back(new_comp);
}
}
}
void GameObject::FindParent(std::vector<GameObject*>& objects)
{
if (parent_uid != "none")
{
for (std::vector<GameObject*>::iterator it = objects.begin(); it != objects.end() && parent == nullptr; it++)
{
if (parent_uid == (*it)->uid) parent = (*it);
}
}
}