-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.cpp
More file actions
429 lines (333 loc) · 14.3 KB
/
Copy pathlevel.cpp
File metadata and controls
429 lines (333 loc) · 14.3 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
#include "level.h"
#include <engine/input/asset.h>
#include <iostream>
#include <memory>
void Level::load_json(const std::string path) {
char real_path[256];
asset_find(path.c_str(), real_path);
simdjson::padded_string json;
try {
if (this->verbose)
std::cout << "Loading level JSON..." << std::endl;
json = simdjson::padded_string::load(real_path);
} catch (...) {
std::cout << "Level " << path << " does not exist or is invalid!" << std::endl;
return;
}
this->json_data = this->json_parser.iterate(json);
try {
if (this->verbose)
std::cout << "Loading layers..." << std::endl;
this->parse_layers(this->json_data["layers"]);
if (this->verbose)
std::cout << "Loading tilesets..." << std::endl;
this->parse_tilesets(this->json_data["tilesets"]);
} catch (...) {
std::cout << "Level " << path << " is invalid!" << std::endl;
return;
}
this->level_path = path;
this->loaded = true;
}
void Level::parse_layers(simdjson::ondemand::array layers) {
for (simdjson::ondemand::value layer : layers) {
if (this->verbose)
std::cout << " -> Parsing layer (determining type)..." << std::endl;
if (layer.find_field_unordered("type") == std::string_view("tilelayer")) {
if (this->verbose)
std::cout << " -> Tile layer, determining if collision..." << std::endl;
if (layer.find_field_unordered("name") == std::string_view("collision")) {
if (this->verbose)
std::cout << " -> Parsing collision layer..." << std::endl;
this->parse_collision_layer(layer);
} else {
if (this->verbose)
std::cout << " -> Parsing background layer..." << std::endl;
this->parse_background_layer(layer);
}
} else if (layer.find_field_unordered("type") == std::string_view("objectgroup")) {
if (this->verbose)
std::cout << " -> Parsing object layer..." << std::endl;
this->parse_object_layer(layer);
}
}
}
void Level::parse_background_layer(simdjson::ondemand::value layer) {
if (this->verbose)
std::cout << " -> Reading chunks..." << std::endl;
for (simdjson::ondemand::value chunk : layer.find_field_unordered("chunks").get_array()) {
const i64 height = chunk.find_field("height");
const i64 width = chunk.find_field("width");
const i64 x = chunk.find_field("x");
const i64 y = chunk.find_field("y");
u32 i = 0;
if (this->verbose) {
std::cout << " -> Reading chunk at { x: " << x << ", y: " << y << ", width: " << width
<< ", height: " << height << " }" << std::endl;
}
for (simdjson::ondemand::value data : chunk.find_field_unordered("data").get_array()) {
const i64 tileset_id = data.get_int64();
if (tileset_id > 0) {
this->background.push_back(
{ .tileset_id = tileset_id, .x = x * 0.1f + (i % width) * 0.1f, .y = y * 0.1f + (i / height) * 0.1f }
);
}
i++;
}
}
}
void Level::parse_collision_layer(simdjson::ondemand::value layer) {
if (this->verbose)
std::cout << " -> Reading chunks..." << std::endl;
for (simdjson::ondemand::value chunk : layer.find_field_unordered("chunks").get_array()) {
const i64 height = chunk.find_field("height");
const i64 width = chunk.find_field("width");
const i64 x = chunk.find_field("x");
const i64 y = chunk.find_field("y");
u32 i = 0;
if (this->verbose) {
std::cout << " -> Reading chunk at { x: " << x << ", y: " << y << ", width: " << width
<< ", height: " << height << " }" << std::endl;
}
for (simdjson::ondemand::value data : chunk.find_field_unordered("data").get_array()) {
const i64 tileset_id = data.get_int64();
if (tileset_id > 0)
this->collisions.push_back({ .x = x * 0.1f + (i % width) * 0.1f, .y = y * 0.1f + (i / height) * 0.1f });
i++;
}
}
}
void Level::parse_object_layer(simdjson::ondemand::value layer) {
if (this->verbose)
std::cout << " -> Reading objects..." << std::endl;
for (simdjson::ondemand::value obj : layer.find_field_unordered("objects").get_array()) {
const i64 uid = obj.find_field("gid");
const f64 height = obj.find_field("height");
const u64 id = obj.find_field("id");
const f64 width = obj.find_field("width");
const f64 x = obj.find_field("x");
const f64 y = obj.find_field("y");
std::string script = "";
if (this->verbose) {
std::cout << " -> Read object { x: " << x << ", y: " << y << ", width: " << width << ", height: " << height
<< ", id: " << id << ", uid: " << uid << " }" << std::endl;
}
try {
for (simdjson::ondemand::value props : obj.find_field_unordered("properties").get_array()) {
const std::string_view name = props.find_field_unordered("name");
const std::string_view t = props.find_field_unordered("type");
if (name == "script" && t == "string") {
const std::string_view val = props.find_field_unordered("value");
script = std::string(val);
}
}
} catch (...) {
// ...
}
this->objects.push_back(
{ .script = script,
.uid = id,
.tileset_id = uid,
.width = (f32)width / 32.0f,
.height = (f32)height / 32.0f,
.x = (f32)(x + width / 2) / 32.0f * 0.1f - 0.05f,
.y = (f32)(y - height / 2) / 32.0f * 0.1f - 0.05f }
);
}
}
void Level::parse_tilesets(simdjson::ondemand::array tilesets) {
if (this->verbose)
std::cout << " -> Reading tilesets..." << std::endl;
for (simdjson::ondemand::value tileset : tilesets) {
const u64 uid = tileset.find_field_unordered("firstgid").get_uint64();
const std::string_view path = tileset.find_field_unordered("source").get_string();
if (this->verbose)
std::cout << " -> Reading tileset { uid: " << uid << ", path: " << path << " }" << std::endl;
this->load_tileset(uid, path);
}
}
std::string absolute_path(const std::string str) {
if (str.rfind("../", 0) != std::string::npos)
return absolute_path(str.substr(3, str.length()));
return str;
}
void Level::load_tileset(const u64 uid, const std::string_view path) {
char real_path[256];
if (this->verbose)
std::cout << " -> Finding asset..." << std::endl;
asset_find(std::string(path).c_str(), real_path);
simdjson::padded_string json;
try {
if (this->verbose)
std::cout << " -> Loading tileset..." << std::endl;
json = simdjson::padded_string::load(real_path);
} catch (...) {
std::cout << "Tileset " << path << " does not exist!" << std::endl;
return;
}
try {
if (this->verbose)
std::cout << " -> Parsing tileset..." << std::endl;
simdjson::ondemand::document data = this->json_parser.iterate(json);
const std::string_view image = data.find_field("image").get_string();
const u64 height = data.find_field("imageheight");
const u64 width = data.find_field("imagewidth");
const u64 count = data.find_field("tilecount");
const u64 tileheight = data.find_field("tileheight");
const u64 tilewidth = data.find_field("tilewidth");
LevelTileset ts = { .path = absolute_path(std::string(image)),
.uid = uid,
.count = count,
.aperture_width = (f32)tilewidth / (f32)width,
.aperture_height = (f32)tileheight / (f32)height };
this->tilesets.push_back(ts);
} catch (...) {
std::cout << "Tileset " << path << " is invalid!" << std::endl;
return;
}
}
LevelTileset *Level::find_tileset(const u64 uid) {
LevelTileset *winner = nullptr;
u64 highest_uid = 0;
for (LevelTileset &ts : this->tilesets) {
if (ts.uid <= uid && ts.uid > highest_uid) {
highest_uid = ts.uid;
winner = &ts;
}
}
return winner;
}
void Level::init() {
if (!this->loaded)
return;
if (this->verbose)
std::cout << "Initializing level..." << std::endl;
LUA_EVENT_RUN(this->lua, "level_init");
lua_pushstring(this->lua, this->level_path.c_str());
LUA_EVENT_CALL(this->lua, 1, 1);
if (lua_isboolean(this->lua, -1) && lua_toboolean(this->lua, -1) == 0)
return;
if (this->verbose)
std::cout << " -> Spawning background" << std::endl;
for (const LevelBackgroundTile tile : this->background) {
LevelTileset *ts = this->find_tileset(tile.tileset_id);
LUA_EVENT_RUN(this->lua, "pre_level_background_ent_create");
if (ts != nullptr) {
lua_createtable(this->lua, 0, 6);
LUA_TABLE_SET_NAMED(this->lua, number, "id", tile.tileset_id);
LUA_TABLE_SET_NAMED(this->lua, number, "uid", ts->uid);
LUA_TABLE_SET_NAMED(this->lua, number, "aperture_width", ts->aperture_width);
LUA_TABLE_SET_NAMED(this->lua, number, "aperture_height", ts->aperture_height);
LUA_TABLE_SET_NAMED(this->lua, number, "count", ts->count);
LUA_TABLE_SET_NAMED(this->lua, string, "texture", ts->path.c_str());
} else {
lua_pushnil(this->lua);
}
LUA_EVENT_CALL(this->lua, 1, 1);
if (lua_isboolean(this->lua, -1) && lua_toboolean(this->lua, -1) == 0)
continue;
Entity *ent = this->ent_manager->ent_create();
ent->set_ent_class(EntClass::BACKGROUND);
ent->set_pos(vector_make3(tile.x, tile.y, 0.0f));
ent->set_active(true);
ent->set_texture_path(ts != nullptr ? ts->path : "assets/debug/missing.png");
LUA_EVENT_RUN(this->lua, "level_background_ent_create");
lua_push_entity(this->lua, ent);
if (ts != nullptr) {
const u64 real_id = tile.tileset_id - ts->uid;
const u64 row_size = (u64)(1 / ts->aperture_width);
if (real_id >= ts->count)
std::cout << "[Level Warning] Tile id " << tile.tileset_id << " is out of range for tileset " << ts->path
<< " (" << ts->count << " tiles) - stale tileset metadata?" << std::endl;
ent->set_uv_size({ ts->aperture_width, ts->aperture_height });
ent->set_uv_offset({ ts->aperture_width * (real_id % row_size), ts->aperture_height * (real_id / row_size) });
lua_createtable(this->lua, 0, 6);
LUA_TABLE_SET_NAMED(this->lua, number, "id", tile.tileset_id);
LUA_TABLE_SET_NAMED(this->lua, number, "uid", ts->uid);
LUA_TABLE_SET_NAMED(this->lua, number, "aperture_width", ts->aperture_width);
LUA_TABLE_SET_NAMED(this->lua, number, "aperture_height", ts->aperture_height);
LUA_TABLE_SET_NAMED(this->lua, number, "count", ts->count);
LUA_TABLE_SET_NAMED(this->lua, string, "texture", ts->path.c_str());
} else {
lua_pushnil(this->lua);
}
LUA_EVENT_CALL(this->lua, 2, 0);
}
if (this->verbose)
std::cout << " -> Spawning objects" << std::endl;
for (const LevelObject obj : this->objects) {
LevelTileset *ts = this->find_tileset(obj.tileset_id);
LUA_EVENT_RUN(this->lua, "pre_level_object_create");
if (ts != nullptr) {
lua_createtable(this->lua, 0, 6);
LUA_TABLE_SET_NAMED(this->lua, number, "id", obj.tileset_id);
LUA_TABLE_SET_NAMED(this->lua, number, "uid", ts->uid);
LUA_TABLE_SET_NAMED(this->lua, number, "aperture_width", ts->aperture_width);
LUA_TABLE_SET_NAMED(this->lua, number, "aperture_height", ts->aperture_height);
LUA_TABLE_SET_NAMED(this->lua, number, "count", ts->count);
LUA_TABLE_SET_NAMED(this->lua, string, "texture", ts->path.c_str());
} else {
lua_pushnil(this->lua);
}
LUA_EVENT_CALL(this->lua, 1, 1);
if (lua_isboolean(this->lua, -1) && lua_toboolean(this->lua, -1) == 0)
continue;
Entity *ent = this->ent_manager->ent_create();
ent->set_pos({ obj.x, obj.y, 0.0f });
ent->set_scale({ obj.width, obj.height });
ent->set_texture_path(ts != nullptr ? ts->path : "assets/debug/missing.png");
ent->set_id(obj.tileset_id);
ent->set_active(true);
LUA_EVENT_RUN(this->lua, "level_object_create");
lua_push_entity(this->lua, ent);
if (ts != nullptr) {
const u64 real_id = obj.tileset_id - ts->uid;
const u64 row_size = (u64)(1 / ts->aperture_width);
if (real_id >= ts->count)
std::cout << "[Level Warning] Tile id " << obj.tileset_id << " is out of range for tileset " << ts->path << " ("
<< ts->count << " tiles) - stale tileset metadata?" << std::endl;
ent->set_uv_size({ ts->aperture_width, ts->aperture_height });
ent->set_uv_offset({ ts->aperture_width * (real_id % row_size), ts->aperture_height * (real_id / row_size) });
lua_createtable(this->lua, 0, 6);
LUA_TABLE_SET_NAMED(this->lua, number, "id", obj.tileset_id);
LUA_TABLE_SET_NAMED(this->lua, number, "uid", ts->uid);
LUA_TABLE_SET_NAMED(this->lua, number, "aperture_width", ts->aperture_width);
LUA_TABLE_SET_NAMED(this->lua, number, "aperture_height", ts->aperture_height);
LUA_TABLE_SET_NAMED(this->lua, number, "count", ts->count);
LUA_TABLE_SET_NAMED(this->lua, string, "texture", ts->path.c_str());
} else {
lua_pushnil(this->lua);
}
LUA_EVENT_CALL(this->lua, 2, 0);
lua_push_entity(this->lua, ent);
lua_setglobal(this->lua, "ENT");
if (luaL_dostring(this->lua, obj.script.c_str())) {
std::cout << "Lua error in LevelObject #" << obj.uid << ":" << std::endl;
if (lua_isstring(this->lua, -1)) {
const char *err = luaL_checkstring(this->lua, -1);
std::cout << err << std::endl;
} else {
std::cout << "(error not on stack)" << std::endl;
}
}
lua_pushnil(this->lua);
lua_setglobal(this->lua, "ENT");
}
lua_pushnil(this->lua);
lua_setglobal(this->lua, "ENT");
LUA_EVENT_RUN(this->lua, "level_should_spawn_collission");
LUA_EVENT_CALL(this->lua, 0, 1);
if (lua_isboolean(this->lua, -1) && lua_toboolean(this->lua, -1) == 0)
return;
if (this->verbose)
std::cout << " -> Spawning collisions" << std::endl;
for (const LevelCollisionTile tile : this->collisions) {
Entity *ent = this->ent_manager->ent_create();
ent->set_ent_class(EntClass::CLIP);
ent->set_texture_path("assets/debug/collide_32x32.png");
ent->set_pos(vector_make3(tile.x, tile.y, 0.0f));
ent->set_visible(false);
ent->set_active(true);
}
}
void Level::update() {}