-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLevelManager.cpp
417 lines (338 loc) · 10.5 KB
/
LevelManager.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
#include "LevelManager.hpp"
Camera::Camera() {
}
Camera::Camera(float x, float y, float ratio, uint16_t screen_mid_x, uint16_t screen_mid_y) {
this->x = x;
this->y = y;
this->ratio = ratio;
this->screen_mid_x = screen_mid_x;
this->screen_mid_y = screen_mid_y;
}
void Camera::update(float dt, float player_x, float player_y) {
x += (player_x - x) * ratio * dt;
y += (player_y - y) * ratio * dt;
}
float Camera::get_view_x(float x) {
return x - this->x + screen_mid_x;
}
float Camera::get_view_y(float y) {
return y - this->y + screen_mid_y;
}
Tile::Tile() {
sprite_index = 0;
x = y = 0;
}
Tile::Tile(uint16_t sprite_index, uint16_t x, uint16_t y) {
this->sprite_index = sprite_index;
this->x = x;
this->y = y;
}
void Tile::render(Spritesheet& spritesheet) {
spritesheet.sprite_scaled(sprite_index, x, y);
}
void Tile::render(Spritesheet& spritesheet, Camera& camera) {
spritesheet.sprite_scaled(sprite_index, camera.get_view_x(x), camera.get_view_y(y));
}
uint16_t Tile::get_x() {
return x;
}
uint16_t Tile::get_y() {
return y;
}
LevelHandler::LevelHandler() {
sprite_size = 0;
level_spawn_blue_x = 0;
level_spawn_blue_y = 0;
level_spawn_pink_x = 0;
level_spawn_pink_y = 0;
level_finish_blue_x = 0;
level_finish_blue_y = 0;
level_finish_pink_x = 0;
level_finish_pink_y = 0;
}
LevelHandler::LevelHandler(uint8_t sprite_size) {
this->sprite_size = sprite_size;
level_spawn_blue_x = 0;
level_spawn_blue_y = 0;
level_spawn_pink_x = 0;
level_spawn_pink_y = 0;
level_finish_blue_x = 0;
level_finish_blue_y = 0;
level_finish_pink_x = 0;
level_finish_pink_y = 0;
}
void LevelHandler::load_level(const uint8_t level_data[]) {
level_spawn_blue_x = 0;
level_spawn_blue_y = 0;
level_spawn_pink_x = 0;
level_spawn_pink_y = 0;
level_finish_blue_x = 0;
level_finish_blue_y = 0;
level_finish_pink_x = 0;
level_finish_pink_y = 0;
tiles.clear();
orbs.clear();
spikes.clear();
springs.clear();
buttons.clear();
doors.clear();
TMX* level_tmx = (TMX*)level_data;
uint16_t tile_id, x, y;
for (uint16_t i = 0; i < level_tmx->height * level_tmx->width; i++) {
tile_id = level_tmx->data[i];
x = (i % level_tmx->width) * sprite_size;
y = (i / level_tmx->width) * sprite_size;
if (tile_id != level_tmx->empty_tile) {
if (tile_id == TILE_ID::PLAYER::BLUE) {
// Blue spawn
level_spawn_blue_x = x;
level_spawn_blue_y = y;
}
else if (tile_id == TILE_ID::PLAYER::PINK) {
// Pink spawn
level_spawn_pink_x = x;
level_spawn_pink_y = y;
}
else if (tile_id == TILE_ID::FINISH::BLUE) {
// Finish spawn for blue
level_finish_blue_x = x;
level_finish_blue_y = y;
}
else if (tile_id == TILE_ID::FINISH::PINK) {
// Finish spawn for pink
level_finish_pink_x = x;
level_finish_pink_y = y;
}
else if (tile_id == TILE_ID::ORB::BLUE) {
// Blue orb
orbs.push_back(Orb(tile_id, 0, x, y));
}
else if (tile_id == TILE_ID::ORB::PINK) {
// Pink Orb
orbs.push_back(Orb(tile_id, 1, x, y));
}
else if (
tile_id == TILE_ID::SPIKE::DOUBLE_BOTTOM ||
tile_id == TILE_ID::SPIKE::DOUBLE_LEFT ||
tile_id == TILE_ID::SPIKE::DOUBLE_TOP ||
tile_id == TILE_ID::SPIKE::DOUBLE_RIGHT ||
tile_id == TILE_ID::SPIKE::SINGLE_BOTTOM_LEFT ||
tile_id == TILE_ID::SPIKE::SINGLE_BOTTOM_RIGHT ||
tile_id == TILE_ID::SPIKE::SINGLE_LEFT_TOP ||
tile_id == TILE_ID::SPIKE::SINGLE_LEFT_BOTTOM ||
tile_id == TILE_ID::SPIKE::SINGLE_TOP_LEFT ||
tile_id == TILE_ID::SPIKE::SINGLE_TOP_RIGHT ||
tile_id == TILE_ID::SPIKE::SINGLE_RIGHT_TOP ||
tile_id == TILE_ID::SPIKE::SINGLE_RIGHT_BOTTOM) {
// Spike
spikes.push_back(Spike(tile_id, parse_spike_id(tile_id), x, y));
}
else if (tile_id == TILE_ID::SPRING::BASE_FRAME) {
// Spring
springs.push_back(Spring(tile_id, x, y));
}
else if (tile_id == TILE_ID::BUTTON::BLUE_RELEASED) {
// Button
buttons.push_back(Button(tile_id, x, y, 0));
}
else if (tile_id == TILE_ID::BUTTON::PINK_RELEASED) {
// Button
buttons.push_back(Button(tile_id, x, y, 1));
}
else if (tile_id == TILE_ID::DOOR::VERTICAL::BLUE_MIDDLE) {
// Door
doors.push_back(Door(tile_id, x, y, 0));
}
else if (tile_id == TILE_ID::DOOR::VERTICAL::PINK_MIDDLE) {
// Door
doors.push_back(Door(tile_id, x, y, 1));
}
else if (
tile_id == TILE_ID::DOOR::VERTICAL::BLUE_TOP ||
tile_id == TILE_ID::DOOR::VERTICAL::BLUE_BOTTOM ||
tile_id == TILE_ID::DOOR::VERTICAL::PINK_TOP ||
tile_id == TILE_ID::DOOR::VERTICAL::PINK_BOTTOM) {
// Blank tile (don't render and don't collide)
// Don't do anything
}
else {
// Normal tile
tiles.push_back(Tile(tile_id, x, y));
}
}
}
}
void LevelHandler::update(AudioHandler audio_handler, float dt) { //Player& player,
for (Orb& orb : orbs) {
orb.update(dt);
}
for (Spring& spring : springs) {
spring.update(dt);
}
bool blue_pressed = false;
bool pink_pressed = false;
for (Button& button : buttons) {
button.update(dt);
if (button.get_pressed()) {
if (button.get_type() == 0) {
blue_pressed = true;
}
else {
pink_pressed = true;
}
}
}
for (Door& door : doors) {
if ((blue_pressed && door.get_type() == 0) ||
(pink_pressed && door.get_type() == 1)) {
if (!door.is_open() && !door.is_opening()) {
door.open();
// Play door sfx
audio_handler.play_sound(audio_handler.sound_samples.at(AUDIO::SFX::DOOR));
}
}
else {
if (door.is_open()) {
// Play door sfx
audio_handler.play_sound(audio_handler.sound_samples.at(AUDIO::SFX::DOOR));
}
door.close();
}
door.update(dt);
}
}
void LevelHandler::render(Spritesheet& spritesheet) {
for (Door& door : doors) {
door.render(spritesheet);
}
for (Tile& tile : tiles) {
tile.render(spritesheet);
}
for (Orb& orb : orbs) {
orb.render(spritesheet);
}
for (Spike& spike : spikes) {
spike.render(spritesheet);
}
for (Spring& spring : springs) {
spring.render(spritesheet);
}
for (Button& button : buttons) {
button.render(spritesheet);
}
// Render finish
spritesheet.sprite_scaled(TILE_ID::FINISH::BLUE, level_finish_blue_x, level_finish_blue_y);
spritesheet.sprite_scaled(TILE_ID::FINISH::PINK, level_finish_pink_x, level_finish_pink_y);
}
//void LevelHandler::render(Spritesheet& spritesheet, Camera& camera) {
// for (Tile& tile : tiles) {
// tile.render(spritesheet, camera);
// }
//
// // Render finish
// spritesheet.sprite_scaled(TILE_ID::FINISH::BLUE, camera.get_view_x(level_finish_blue_x), camera.get_view_y(level_finish_blue_y));
// spritesheet.sprite_scaled(TILE_ID::FINISH::PINK, camera.get_view_x(level_finish_pink_x), camera.get_view_y(level_finish_pink_y));
//}
uint8_t LevelHandler::handle_orb_collisions(float x, float y, uint8_t type) {
// Handle star collisions
uint8_t count = 0;
for (Orb& orb : orbs) {
// Orb hasn't been collected and is same colour as player
if (!orb.get_collected() && type == orb.get_type()) {
// Orb is colliding
if (is_colliding(orb.get_x() + GAME::ORB::BORDER, orb.get_y() + GAME::ORB::BORDER, x, y, GAME::ORB::WIDTH, GAME::ORB::WIDTH, sprite_size, sprite_size)) {
orb.set_collected();
count++; // Note: sometimes (rarely) player gets awarded many orbs when there aren't that many in the level
//printf("orb %u, %u, collected: %u\n", type, orb.get_type(), orb.get_collected());
//TODO: create particle or something with same image which rotates, grows larger and fades at location of orb just collected
}
}
}
return count;
}
bool LevelHandler::handle_spike_collisions(float x, float y) {
for (Spike& spike : spikes) {
// Spike is colliding
if (spike.check_collision(x, y)) {
return true;
//TODO: create something to show player was killed
}
}
return false;
}
std::vector<Tile>& LevelHandler::get_tiles() {
return tiles;
}
//std::vector<Orb> LevelHandler::get_orbs() {
// return orbs;
//}
std::vector<Spring>& LevelHandler::get_springs() {
return springs;
}
std::vector<Button>& LevelHandler::get_buttons() {
return buttons;
}
std::vector<Door>& LevelHandler::get_doors() {
return doors;
}
uint8_t LevelHandler::get_sprite_size() {
return sprite_size;
}
bool is_colliding(float tile_x, float tile_y, float x, float y, uint8_t tile_w, uint8_t tile_h, uint8_t w, uint8_t h) {
return tile_x + tile_w > x && tile_x < x + w && tile_y + tile_h > y && tile_y < y + h;
}
bool is_colliding(Tile& tile, float x, float y, uint8_t sprite_size) {
return (tile.get_x() + sprite_size > x && tile.get_x() < x + sprite_size && tile.get_y() + sprite_size > y && tile.get_y() < y + sprite_size);
}
bool is_colliding(float tile_x, float tile_y, float x, float y, uint8_t sprite_size) {
return (tile_x + sprite_size > x && tile_x < x + sprite_size && tile_y + sprite_size > y && tile_y < y + sprite_size);
}
bool check_on_top(float tile_x, float tile_y, float x, float y, uint8_t tile_w, uint8_t tile_h, uint8_t w, uint8_t h) {
// Checks if on top of tile
return tile_x + tile_w > x && tile_x < x + w && tile_y == y + h;
}
bool check_on_top(Tile& tile, float x, float y, uint8_t sprite_size) {
return (tile.get_x() + sprite_size > x && tile.get_x() < x + sprite_size && tile.get_y() == y + sprite_size);
}
Spike::SpikeDirection parse_spike_id(uint16_t spike_id) {
if (spike_id == TILE_ID::SPIKE::DOUBLE_BOTTOM) {
return Spike::SpikeDirection::DOUBLE_BOTTOM;
}
else if (spike_id == TILE_ID::SPIKE::DOUBLE_LEFT) {
return Spike::SpikeDirection::DOUBLE_LEFT;
}
else if (spike_id == TILE_ID::SPIKE::DOUBLE_TOP) {
return Spike::SpikeDirection::DOUBLE_TOP;
}
else if (spike_id == TILE_ID::SPIKE::DOUBLE_RIGHT) {
return Spike::SpikeDirection::DOUBLE_RIGHT;
}
else if (spike_id == TILE_ID::SPIKE::SINGLE_BOTTOM_LEFT) {
return Spike::SpikeDirection::SINGLE_BOTTOM_LEFT;
}
else if (spike_id == TILE_ID::SPIKE::SINGLE_BOTTOM_RIGHT) {
return Spike::SpikeDirection::SINGLE_BOTTOM_RIGHT;
}
else if (spike_id == TILE_ID::SPIKE::SINGLE_LEFT_TOP) {
return Spike::SpikeDirection::SINGLE_LEFT_TOP;
}
else if (spike_id == TILE_ID::SPIKE::SINGLE_LEFT_BOTTOM) {
return Spike::SpikeDirection::SINGLE_LEFT_BOTTOM;
}
else if (spike_id == TILE_ID::SPIKE::SINGLE_TOP_LEFT) {
return Spike::SpikeDirection::SINGLE_TOP_LEFT;
}
else if (spike_id == TILE_ID::SPIKE::SINGLE_TOP_RIGHT) {
return Spike::SpikeDirection::SINGLE_TOP_RIGHT;
}
else if (spike_id == TILE_ID::SPIKE::SINGLE_RIGHT_TOP) {
return Spike::SpikeDirection::SINGLE_RIGHT_TOP;
}
else if (spike_id == TILE_ID::SPIKE::SINGLE_RIGHT_BOTTOM) {
return Spike::SpikeDirection::SINGLE_RIGHT_BOTTOM;
}
else {
// Pick one
return Spike::SpikeDirection::DOUBLE_BOTTOM;
}
}