-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLevel.cpp
executable file
·332 lines (300 loc) · 7.52 KB
/
Level.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
#include "Level.h"
#include <json.hpp>
#include <fstream>
#include "Trigger.h"
Level::Level()
{
//Meshes[(size_t)None] = nullptr;
Meshes[(size_t)Box] = Mesh::CreateCubeWithNormals();
Meshes[(size_t)Cylinder] = Mesh::CreateCylinderWithNormals();
}
Level::~Level()
{
Clear();
for (int i = 0; i < SHAPE_NUMITEMS; i++)
delete Meshes[i];
}
Level::Level(const Level& other) : Level()
{
*this = other;
}
Level& Level::operator=(const Level& other)
{
Clear();
for (GameObject *r : Objects)
Objects.push_back(new GameObject(*r));
return *this;
}
Level::Level(Level&& other) : Level()
{
*this = std::move(other);
}
Level& Level::operator=(Level&& other)
{
if (this != &other)
{
Clear();
Objects = std::move(other.Objects);
ParticleSystems = std::move(other.ParticleSystems);
Meshes = std::move(other.Meshes);
}
return *this;
}
std::size_t Level::AddGameObject(
glm::vec3 position,
glm::quat orientation,
glm::vec3 dimensions,
glm::vec4 color,
GLuint texID,
std::string type,
float mass
)
{
GameObject *p;
if(type == "box")
p = new GameObject(Meshes[(size_t)Box], Shape::Box, position,
orientation, dimensions, color, texID, mass);
else
p = new GameObject(Meshes[(size_t)Cylinder], Shape::Cylinder,
position, orientation, dimensions, color, texID, mass);
Objects.push_back(p);
return Objects.size() - 1;
}
void Level::Delete(std::size_t index)
{
Objects.erase(Objects.begin() + index);
}
void Level::Clear()
{
for (GameObject *ent : Objects)
delete ent;
Objects.clear();
}
int Level::Find(btRigidBody *r)
{
for (std::size_t i = 0, n = Objects.size(); i < n; i++)
{
if (Objects[i]->rigidbody == r)
return i;
}
return -1;
}
GameObject* Level::Find(int id)
{
for (std::size_t i = 0, n = Objects.size(); i < n; i++)
{
if (Objects[i]->ID == id)
return Objects[i];
}
return NULL;
}
void Level::Render(GLuint uMMatrix, GLuint uColor)
{
for (GameObject *ent : Objects)
{
GLuint textureID = ent->textureID;
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, textureID);
glUniformMatrix4fv(uMMatrix, 1, GL_FALSE, &ent->GetModelMatrix()[0][0]);
glUniform4fv(uColor, 1, &ent->color.r);
ent->Render();
}
}
void Level::Serialize(std::string file)
{
std::ofstream f(file);
nlohmann::json objects;
for (GameObject *ent : Objects)
{
nlohmann::json object;
glm::vec3 translation = ent->GetTranslation();
glm::quat orientation = ent->GetOrientation();
glm::vec3 scale = ent->GetScale();
if (ent->shapeType == Shape::Box)
object["type"] = "box";
else
object["type"] = "cylinder";
if(ent->motion.Points.size() == 0)
object["position"] = {
translation.x, translation.y,
translation.z };
else
object["position"] = {
ent->motion.Points[0].x,
ent->motion.Points[0].y,
ent->motion.Points[0].z };
object["orientation"] = {
orientation.w, orientation.x,
orientation.y, orientation.z };
object["dimensions"] = {
scale.x, scale.y, scale.z };
object["color"] = {
ent->trueColor.r, ent->trueColor.g, ent->trueColor.b,
ent->trueColor.a };
object["mass"] = ent->GetMass();
object["collidable"] = ent->GetCollidable();
object["id"] = ent->ID;
object["texID"] = ent->textureID;
object["drawable"] = ent->drawable;
if (!ent->motion.Points.empty())
{
nlohmann::json path;
path["speed"] = ent->motion.Speed;
path["enabled"] = ent->motion.Enabled;
path["curved"] = ent->motion.Curved;
for (auto v = ent->motion.Points.begin();
v != ent->motion.Points.end(); ++v)
path["points"].push_back({ v->x, v->y, v->z });
object["path"] = path;
}
if (ent->trigger.connectionIDs.size() > 0)
{
for (auto c : ent->trigger.connectionIDs)
object["conns"].push_back(c);
}
if (ent->trigger.bDeadly)
{
object["deadly"] = true;
}
if (ent->trigger.bLoopy)
{
object["loopy"] = true;
}
object["dof"] = {
ent->dof[0],
ent->dof[1],
ent->dof[2],
ent->dof[3],
ent->dof[4],
ent->dof[5]
};
objects.push_back(object);
}
nlohmann::json level;
level["objects"] = objects;
f << std::setw(2) << level << std::endl;
f.close();
}
Level *Level::Deserialize(std::string file)
{
std::ifstream f(file);
if (!f.is_open())
return nullptr;
std::string s(
(std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
auto data = nlohmann::json::parse(s);
Level *level = new Level();
for (auto object : data["objects"])
{
auto j_pos = object["position"];
glm::vec3 position(j_pos[0], j_pos[1], j_pos[2]);
auto j_ori = object["orientation"];
glm::quat orientation(j_ori[0], j_ori[1], j_ori[2], j_ori[3]);
auto j_dim = object["dimensions"];
glm::vec3 dimensions(j_dim[0], j_dim[1], j_dim[2]);
auto j_col = object["color"];
glm::vec4 color(j_col[0], j_col[1], j_col[2], j_col[3]);
auto mass = object["mass"];
GLuint texID;
if (!object["texID"].is_null())
texID = object["texID"];
else
texID = 3;
auto path = object["path"];
int id;
if (!object["id"].is_null())
id = object["id"];
bool collidable;
if(!object["collidable"].is_null())
collidable = object["collidable"];
std::size_t i = level->AddGameObject(position, orientation,
dimensions, color, texID, object["type"], mass);
if (!object["drawable"].is_null())
level->Objects[i]->drawable = object["drawable"];
if (!object["collidable"].is_null())
level->Objects[i]->SetCollidable(collidable);
if (!object["id"].is_null())
{
level->Objects[i]->ID = id;
if (id >= GameObject::nextID)
GameObject::nextID = id + 1;
}
if (!path.is_null())
{
GameObject *ent = level->Objects[i];
auto speed = path["speed"];
auto points = path["points"];
if (!path["enabled"].is_null())
ent->motion.Enabled = path["enabled"];
if(!path["curved"].is_null())
ent->motion.Curved = path["curved"];
else
ent->motion.Curved = false;
ent->motion.Speed = speed;
for (auto point : points)
ent->motion.Points.insert(
ent->motion.Points.end(),
glm::vec3(point[0], point[1], point[2]));
ent->rigidbody->setMassProps(ent->GetMass(),
btVector3(0, 0, 0));
}
auto conns = object["conns"];
if (!conns.is_null())
{
std::cout << conns.size() << std::endl;
for (int j = 0; j < conns.size(); j++)
level->Objects[i]->trigger.connectionIDs.push_back(conns[j]);
level->Objects[i]->trigger.bEnabled = true;
}
if (object["deadly"].is_boolean() && object["deadly"])
{
level->Objects[i]->trigger.bDeadly = true;
level->Objects[i]->trigger.bEnabled = true;
}
if (object["loopy"].is_boolean() && object["loopy"])
{
level->Objects[i]->trigger.bLoopy = true;
level->Objects[i]->trigger.bEnabled = true;
}
if (!object["dof"].is_null())
{
for (int j = 0; j < 6; j++)
level->Objects[i]->dof[j] = object["dof"][j];
}
}
f.close();
//Set links
for (int i = 0; i < level->Objects.size(); i++)
{
GameObject* entity = level->Objects[i];
if (entity->trigger.bEnabled)
{
for (int j = entity->trigger.connectionIDs.size()-1;
j >= 0; j--)
{
GameObject* plat = level->Find(
entity->trigger.connectionIDs[j]);
if (plat != NULL)
{
if (!plat->motion.Points.empty())
entity->trigger.LinkToPlatform(plat, entity, true);
}
else
{
entity->trigger.connectionIDs.erase
(entity->trigger.connectionIDs.begin() + j);
}
}
if (entity->trigger.bDeadly)
entity->trigger.RegisterCallback([]() {
Physics::CreateBlob();
}, Enter);
if (entity->trigger.bLoopy)
entity->trigger.RegisterCallback([]() {
Physics::CreateBlob(glm::vec4(0, 0, 1, 1));
}, Enter);
}
}
return level;
}