-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelManager.cpp
More file actions
361 lines (333 loc) · 9.96 KB
/
LevelManager.cpp
File metadata and controls
361 lines (333 loc) · 9.96 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
#include "LevelManager.h"
#include "surface.h"
#include "Player.h"
#include "Finish.h"
#include <iostream>
namespace Tmpl8
{
// Constructor //
LevelManager::LevelManager()
:arrowSign(new Surface("assets/Objects/sign_arrow.png"), 1)
,tiles("assets/Tilesets/tileset_forest.png")
{
finish = new Finish; // Creating a 'real' finish object.
state[0] = LevelState::Open; // Open first level.
}
// Main Functions //
// Function that resets everything and then loads the requested level.
void LevelManager::LoadLevel(const int level, Player& player, Timer& timer)
{
if (level > numLevels || level <= 0) { std::cout << "Level cannot be found or does not exist." << std::endl; }
else
{
currentLevel = level;
Reset(player);
switch (level)
{
case 1:
player.SetLoc(startLoc1);
timer.SetTime(time1);
break;
case 2:
player.SetLoc(startLoc2);
timer.SetTime(time2);
break;
case 3:
player.SetLoc(startLoc3);
timer.SetTime(time3);
break;
default:
break;
}
}
}
// Function that calls the required draw functions.
void LevelManager::DrawLevel(Surface* screen, const Location& drawOffset)
{
int loopY = 0, loopX = 0;
// Set loop values for each level.
switch (currentLevel)
{
case 1:
loopY = 11;
loopX = 50;
break;
case 2:
loopY = 11;
loopX = 55;
break;
case 3:
loopY = 18;
loopX = 55;
break;
default:
break;
}
for (int y = 0; y < loopY; y++)
{
for (int x = 0; x < loopX; x++)
{
// Collision map drawing
if (!(GetContents(x, y) == TileContents::Empty))
{
if (GetContents(x, y) == TileContents::ArrowSign)
{
arrowSign.Draw(screen, x * tileSize - static_cast<int>(drawOffset.x),
y * tileSize - static_cast<int>(drawOffset.y));
}
if (GetContents(x, y) == TileContents::Water || GetContents(x, y) == TileContents::FakeWater)
{ water.Draw(screen, x, y, drawOffset); }
if (GetContents(x, y) == TileContents::WoodStakes) { woodstakes.Draw(screen, x, y, drawOffset); }
if (GetContents(x, y) == TileContents::SpikesBig) { spikes.Draw(screen, x, y, Spikes::Type::Big, drawOffset); }
if (GetContents(x, y) == TileContents::SpikesMedium) { spikes.Draw(screen, x, y, Spikes::Type::Medium, drawOffset); }
if (GetContents(x, y) == TileContents::SpikesSmall) { spikes.Draw(screen, x, y, Spikes::Type::Small, drawOffset); }
if (GetContents(x, y) == TileContents::SpikesFloor) { spikes.Draw(screen, x, y, Spikes::Type::Floor, drawOffset); }
if (GetContents(x, y) == TileContents::Finish) { finish->Draw(screen, x, y, drawOffset); }
}
// Tilemap drawing
switch (currentLevel)
{
case 1:
if (!(levelOne[y][x * 2] == '-') && !(levelOne[y][x * 2 + 1] == '-'))
{
float tx = static_cast<float>(levelOne[y][x * 2] - 'a');
float ty = static_cast<float>(levelOne[y][x * 2 + 1] - 'a');
DrawTile(screen, { static_cast<float>(x * tileSize - drawOffset.x),
static_cast<float>(y * tileSize - drawOffset.y) }, { tx, ty });
}
break;
case 2:
if (!(levelTwo[y][x * 2] == '-') && !(levelTwo[y][x * 2 + 1] == '-'))
{
float tx = static_cast<float>(levelTwo[y][x * 2] - 'a');
float ty = static_cast<float>(levelTwo[y][x * 2 + 1] - 'a');
DrawTile(screen, { static_cast<float>(x * tileSize - drawOffset.x),
static_cast<float>(y * tileSize - drawOffset.y) }, { tx, ty });
}
break;
case 3:
if (!(levelThree[y][x * 2] == '-') && !(levelThree[y][x * 2 + 1] == '-'))
{
float tx = static_cast<float>(levelThree[y][x * 2] - 'a');
float ty = static_cast<float>(levelThree[y][x * 2 + 1] - 'a');
DrawTile(screen, { static_cast<float>(x * tileSize - drawOffset.x),
static_cast<float>(y * tileSize - drawOffset.y) }, { tx, ty });
}
break;
default:
break;
}
}
}
}
// Function that updates all tile animations.
void LevelManager::UpdateAnimations(const float& deltaTime)
{
water.UpdateFrame(deltaTime);
finish->UpdateFrame(deltaTime);
}
// Function that calls tile triggers when a player touches that tile.
void LevelManager::CallTrigger(const TileContents& content, const Location& tile, Player& player, MenuManager& menu)
{
switch (content)
{
case TileContents::Empty:
break;
case TileContents::FakeWater:
case TileContents::SpikesFloor:
case TileContents::Obstacle:
obstacle.Trigger(tile, player);
break;
case TileContents::Finish:
finish->Trigger(menu, *this, player, tile);
break;
case TileContents::Water:
water.Trigger(*this, player, menu, tile);
break;
case TileContents::WoodStakes:
woodstakes.Trigger(*this, player, menu, tile);
break;
case TileContents::SpikesBig:
spikes.Trigger(*this, player, menu, Spikes::Type::Big, tile);
break;
case TileContents::SpikesMedium:
spikes.Trigger(*this, player, menu, Spikes::Type::Medium, tile);
break;
case TileContents::SpikesSmall:
spikes.Trigger(*this, player, menu, Spikes::Type::Small, tile);
break;
default:
break;
}
}
// Function that runs everything necessary on death.
void LevelManager::Death(Player& player, MenuManager& menu)
{
isDead = true;
menu.SetMenuState(MenuManager::MenuState::LevelFailed);
}
// Returns contents from the called cell.
LevelManager::TileContents LevelManager::GetContents(const Location& loc) const
{
char content = 'o';
switch (currentLevel)
{
case 1:
if ((static_cast<int>(loc.x) > 50 * tileSize) || (static_cast<int>(loc.y) > 11 * tileSize)) { return TileContents::Empty; }
content = levelOneColl[static_cast<int>(loc.y) / tileSize][static_cast<int>(loc.x) / tileSize];
break;
case 2:
if ((static_cast<int>(loc.x) > 55 * tileSize) || (static_cast<int>(loc.y) > 11 * tileSize)) { return TileContents::Empty; }
content = levelTwoColl[static_cast<int>(loc.y) / tileSize][static_cast<int>(loc.x) / tileSize];
break;
case 3:
if ((static_cast<int>(loc.x) > 55 * tileSize) || (static_cast<int>(loc.y) > 18 * tileSize)) { return TileContents::Empty; }
content = levelThreeColl[static_cast<int>(loc.y) / tileSize][static_cast<int>(loc.x) / tileSize];
break;
default:
break;
}
/* ======================== DEBUG ======================== */
// std::cout << "TileContent: " << content << std::endl;
/* ======================================================= */
switch (content)
{
case 'o':
return TileContents::Empty;
break;
case 'A':
return TileContents::ArrowSign;
break;
case '-':
return TileContents::Obstacle;
break;
case 'F':
return TileContents::Finish;
break;
case 'W':
return TileContents::Water;
break;
case 'w':
return TileContents::FakeWater;
break;
case 'X':
return TileContents::WoodStakes;
break;
case 'S':
return TileContents::SpikesBig;
break;
case 's':
return TileContents::SpikesMedium;
break;
case 'c':
return TileContents::SpikesSmall;
break;
case 'C':
return TileContents::SpikesFloor;
break;
default:
std::cout << "Unknown content declaration, returned empty." << std::endl;
return TileContents::Empty;
break;
}
}
// Function overload //
// Returns content from the called cell.
LevelManager::TileContents LevelManager::GetContents(const int x, const int y) const
{
char content = 'o';
switch (currentLevel)
{
case 1:
if (x > 50 || x < 0 || y > 10 || y < 0) { return TileContents::Empty; }
else { content = levelOneColl[y][x]; }
break;
case 2:
if (x > 55 || x < 0 || y > 10 || y < 0) { return TileContents::Empty; }
else { content = levelTwoColl[y][x]; }
break;
case 3:
if (x > 55 || x < 0 || y > 17 || y < 0) { return TileContents::Empty; }
else { content = levelThreeColl[y][x]; }
break;
default:
break;
}
/* ======================== DEBUG ======================== */
// std::cout << "TileContent: " << content << std::endl;
/* ======================================================= */
switch (content)
{
case 'o':
return TileContents::Empty;
break;
case 'A':
return TileContents::ArrowSign;
break;
case '-':
return TileContents::Obstacle;
break;
case 'F':
return TileContents::Finish;
break;
case 'W':
return TileContents::Water;
break;
case 'w':
return TileContents::FakeWater;
break;
case 'X':
return TileContents::WoodStakes;
break;
case 'S':
return TileContents::SpikesBig;
break;
case 's':
return TileContents::SpikesMedium;
break;
case 'c':
return TileContents::SpikesSmall;
break;
case 'C':
return TileContents::SpikesFloor;
break;
default:
std::cout << "Unknown content declaration, returned empty." << std::endl;
return TileContents::Empty;
break;
}
}
// Function that draws the requested tile to the requested location.
void LevelManager::DrawTile(Surface* screen, const Location& loc, const Location& tile)
{
// Checks if location is on the screen.
if (loc.x > screen->GetWidth()) { return; }
if (loc.y > screen->GetHeight()) { return; }
// Get locations on buffers.
Pixel* src = tiles.GetBuffer() + static_cast<int>(tile.x) * tileSize + (static_cast<int>(tile.y) * tileSize) * tiles.GetWidth();
Pixel* dst = screen->GetBuffer() + static_cast<int>(loc.x) + static_cast<int>(loc.y) * screen->GetWidth();
for (int i = 0; i < tileSize; i++, src += tiles.GetWidth(), dst += screen->GetWidth()) // y
{
if (loc.y + i + 1 <= screen->GetHeight() && loc.y + i >= 0) // Stop drawing offscreen.
{
for (int j = 0; j < tileSize; j++) // x
{
if ((loc.x + j + 1) <= screen->GetWidth() && loc.x + j >= 0) // Stop drawing offscreen.
{
// Check for black parts & remove these from drawing.
// Found in Sprite::Draw() code in Tmpl8.
const Pixel c1 = *(src + j);
if (c1 & 0xffffff) { dst[j] = src[j]; }
//////////////////////////////////////////////
}
}
}
}
}
// Function that calls everything that must be reset upon (re)loading a level.
void LevelManager::Reset(Player& player)
{
player.Reset();
isDead = false;
}
};