Repository navigation
Expand file tree
/
Copy pathdash_manager.cpp
More file actions
484 lines (427 loc) · 13.4 KB
/
Copy pathdash_manager.cpp
File metadata and controls
484 lines (427 loc) · 13.4 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include "dash_manager.h"
/**
* Default Constructor that initializes all game objects and
* sets starting state
*/
dash_manager::dash_manager()
{
dash = nullptr;
score = 0;
current_state = MENU;
selected_style = CLASSIC;
player_name = "";
current_difficulty = "";
selected_leaderboard = "Easy";
load_style_bitmaps();
create_player();
}
/**
* Destructor used to delete the pointer
*/
dash_manager::~dash_manager()
{
delete dash;
}
/**
* Method: create_player
* Purpose: Deletes the current player and creates a new one matching the selected block style
*/
void dash_manager::create_player()
{
delete dash;
switch (selected_style)
{
case CLASSIC:
dash = new classic_dash();
break;
case GHOST:
dash = new ghost_dash();
break;
case ROBOT:
dash = new robot_dash();
break;
case UFO:
dash = new ufo_dash();
break;
case PUZZLE:
dash = new puzzle_dash();
break;
}
}
/**
* Method: run
* Purpose: Opens the window and runs the main game loop
*/
void dash_manager::run()
{
open_window("Dash - By Khubaib Ejaz", SCREEN_WIDTH, SCREEN_HEIGHT);
while(!quit_requested() && current_state != QUIT)
{
process_events();
handle_input();
update();
draw();
}
}
/**
* Method: handle_input
* Purpose: Reads input based on current game state
*/
void dash_manager::handle_input()
{
switch (current_state)
{
case MENU:
if (key_typed(NUM_1_KEY))
{
load_level(1);
current_state = PLAYING;
}
else if (key_typed(NUM_2_KEY))
{
load_level(2);
current_state = PLAYING;
}
else if (key_typed(NUM_3_KEY))
{
load_level(3);
current_state = PLAYING;
}
else if (key_typed(S_KEY))
{
current_state = PLAYER_SELECT;
}
else if (key_typed(L_KEY))
{
current_state = LEADERBOARD;
}
else if (key_typed(Q_KEY))
{
current_state = QUIT;
}
break;
case PLAYER_SELECT:
if (key_typed(Q_KEY))
{
current_state = MENU;
}
break;
case PLAYING:
dash->handle_input();
break;
case WIN:
if (key_typed(RETURN_KEY) && !player_name.empty())
{
board.save(current_difficulty, player_name, score);
selected_leaderboard = current_difficulty;
current_state = LEADERBOARD;
player_name = "";
}
break;
case LEADERBOARD:
if (key_typed(Q_KEY))
{
current_state = MENU;
}
break;
default:
if (key_typed(RETURN_KEY))
{
current_state = MENU;
}
else if (key_typed(Q_KEY))
{
current_state = QUIT;
}
break;
}
}
/**
* Method: update
* Purpose: Updates game logic based on current state
*/
void dash_manager::update()
{
if (current_state == PLAYING)
{
dash->update();
current_level.update();
score += (int)current_level.get_scroll_speed();
if(current_level.check_collision(*dash))
{
current_state = DEAD;
}
if (current_level.is_complete())
{
player_name = "";
current_state = WIN;
}
}
}
/**
* Method: draw
* Purpose: Draws the correct screen based on current state
*/
void dash_manager::draw()
{
if(current_state == MENU) draw_menu();
if (current_state == PLAYER_SELECT) draw_player_select();
if (current_state == PLAYING) draw_game();
if (current_state == LEADERBOARD) draw_leaderboard();
if(current_state == DEAD || current_state == WIN) draw_end_screen();
refresh_screen(60);
}
/**
* Method: draw_datetime
* Purpose: Displays the current local date and time on the menu screen
* using the <ctime> library
*/
void dash_manager::draw_date_time()
{
time_t now = time(0);
tm* local_time = localtime(&now);
string minute = (local_time->tm_min < 10) ? "0" + to_string(local_time->tm_min) : to_string(local_time->tm_min);
string time = to_string(local_time->tm_hour) + ":" + minute;
string date = to_string(local_time->tm_mday) + "/" + to_string(local_time->tm_mon + 1) + "/" + to_string(local_time->tm_year + 1900);
draw_text(date + " " + time, COLOR_GRAY, 20, 20);
}
/**
* Method: draw_player_select
* Purpose: Draws the player select dash style window to the user
*/
void dash_manager::draw_player_select()
{
int block_size = 120;
int gap = 40;
int total_row_width = (block_size * 5) + (gap * 4);
int start_x = (SCREEN_WIDTH - total_row_width) / 2;
int block_y = 260;
int button_y = block_y + block_size + 15;
int footer_y = button_y + 60;
int classic_x = start_x;
int ghost_x = start_x + (block_size + gap);
int robot_x = start_x + (block_size + gap) * 2;
int ufo_x = start_x + (block_size + gap) * 3;
int puzzle_x = start_x + (block_size + gap) * 4;
int divider_margin = start_x;
int center_x = SCREEN_WIDTH / 2;
clear_screen(COLOR_DARK_SLATE_BLUE);
draw_text("= SELECT YOUR BLOCK =", COLOR_WHITE, center_x - 90, 90);
draw_line(COLOR_DARK_GRAY, divider_margin, 115, SCREEN_WIDTH - divider_margin, 115);
draw_text("Click the button below your chosen style.", COLOR_GRAY, center_x - 140, 143);
draw_line(COLOR_DARK_GRAY, divider_margin, 178, SCREEN_WIDTH - divider_margin, 178);
set_interface_style(SHADED_DARK_STYLE);
// Draw bitmaps
double scale_x = (double)block_size / bitmap_width(classic_bmp);
double scale_y = (double)block_size / bitmap_height(classic_bmp);
draw_bitmap(classic_bmp, classic_x + 15, block_y, option_scale_bmp(scale_x, scale_y));
draw_bitmap(ghost_bmp, ghost_x + 15, block_y, option_scale_bmp(scale_x, scale_y));
draw_bitmap(robot_bmp, robot_x + 15, block_y, option_scale_bmp(scale_x, scale_y));
draw_bitmap(ufo_bmp, ufo_x + 15, block_y, option_scale_bmp(scale_x, scale_y));
draw_bitmap(puzzle_bmp, puzzle_x + 15, block_y, option_scale_bmp(scale_x, scale_y));
// Buttons
if (button("Classic", rectangle_from(classic_x, button_y, block_size, 30)))
{
selected_style = CLASSIC;
create_player();
current_state = MENU;
}
if (button("Ghost", rectangle_from(ghost_x, button_y, block_size, 30)))
{
selected_style = GHOST;
create_player();
current_state = MENU;
}
if (button("Robot", rectangle_from(robot_x, button_y, block_size, 30)))
{
selected_style = ROBOT;
create_player();
current_state = MENU;
}
if (button("UFO", rectangle_from(ufo_x, button_y, block_size, 30)))
{
selected_style = UFO;
create_player();
current_state = MENU;
}
if (button("Puzzle", rectangle_from(puzzle_x, button_y, block_size, 30)))
{
selected_style = PUZZLE;
create_player();
current_state = MENU;
}
draw_interface();
draw_line(COLOR_DARK_GRAY, divider_margin, footer_y, SCREEN_WIDTH - divider_margin, footer_y);
draw_text("Q - Return to Menu", COLOR_GRAY, center_x - 85, footer_y + 28);
draw_line(COLOR_DARK_GRAY, divider_margin, footer_y + 55, SCREEN_WIDTH - divider_margin, footer_y + 55);
}
/**
* Method: draw_progress_bar
* Purpose: Draws a progress bar on top of the screen to guide the user where they stand in the level
*/
void dash_manager::draw_progress_bar()
{
double progress = 1.0 - ((double)current_level.get_obstacle_count() / total_obstacles);
double bar_x = 200;
double bar_y = 20;
double bar_width = 880;
double bar_height = 15;
// Background
draw_rectangle(COLOR_DARK_GRAY, bar_x, bar_y, bar_width, bar_height);
// Filled Progress
double filled_width = bar_width * progress;
fill_rectangle(COLOR_LIME_GREEN, bar_x + 2, bar_y + 2, filled_width - 4, bar_height - 4);
// Progress percentage text
int percent = (int)(progress * 100);
draw_text(to_string(percent) + "%", COLOR_WHITE, bar_x + bar_width + 10, bar_y);
}
/**
* Method: draw_leaderboard
* Purpose: Draws the leaderboard screen showing top 3 scores
* for the selected difficulty using buttons to switch difficulty
*/
void dash_manager::draw_leaderboard()
{
clear_screen(COLOR_BLACK);
draw_text("= LEADERBOARD =", COLOR_WHITE, 570, 60);
draw_line(COLOR_DARK_GRAY, 300, 85, 980, 85);
// Difficulty buttons using SplashKit Interface
set_interface_style(SHADED_DARK_STYLE);
if (button("Easy", rectangle_from(390, 110, 140, 35))) selected_leaderboard = "Easy";
if (button("Medium", rectangle_from(550, 110, 140, 35))) selected_leaderboard = "Medium";
if (button("Hard", rectangle_from(710, 110, 140, 35))) selected_leaderboard = "Hard";
draw_interface();
draw_text(selected_leaderboard + " Scores", COLOR_BLUE, 585, 165);
draw_line(COLOR_DARK_GRAY, 300, 185, 980, 185);
// Draw top 3 using leaderboard class
board.draw(selected_leaderboard);
// Footer
draw_line(COLOR_DARK_GRAY, 300, 490, 980, 490);
draw_text("Q - Back to Menu", COLOR_GRAY, 575, 510);
}
/**
* Method: load_level
* Purpose: Uses level_loader to load the correct file based on choice
*
* @param choice the difficulty selected by the user (1, 2 or 3)
*/
void dash_manager::load_level(int choice)
{
string filename;
create_player(); // reset player using current selected style
score = 0;
switch (choice)
{
case 1:
filename = "Resources/levels/easy.txt";
current_difficulty = "Easy";
current_level = loader.load(filename);
total_obstacles = current_level.get_obstacle_count();
break;
case 2:
filename = "Resources/levels/medium.txt";
current_difficulty = "Medium";
current_level = loader.load(filename);
total_obstacles = current_level.get_obstacle_count();
break;
case 3:
filename = "Resources/levels/hard.txt";
current_difficulty = "Hard";
current_level = loader.load(filename);
total_obstacles = current_level.get_obstacle_count();
break;
default:
break;
}
}
/**
* Method: load_style_bitmaps
* Purpose: Loads all the bitmaps being used
*/
void dash_manager::load_style_bitmaps()
{
classic_bmp = load_bitmap("classic", "Resources/dash_styles/classic.png");
ghost_bmp = load_bitmap("ghost", "Resources/dash_styles/ghost.png");
robot_bmp = load_bitmap("robot", "Resources/dash_styles/robot.png");
ufo_bmp = load_bitmap("ufo", "Resources/dash_styles/ufo.png");
puzzle_bmp = load_bitmap("puzzle", "Resources/dash_styles/puzzle.png");
}
/**
* Method: draw_menu
* Purpose: Draws the difficulty selection screen
*/
void dash_manager::draw_menu()
{
clear_screen(COLOR_BLACK);
draw_date_time();
// Title
draw_text("DASH", COLOR_WHITE, 620, 220);
draw_text("Select Difficulty", COLOR_GRAY, 570, 255);
// Divider line
draw_line(COLOR_DARK_GRAY, 440, 275, 840, 275);
// Options
draw_text("1 - Easy", COLOR_GREEN, 590, 295);
draw_text("2 - Medium", COLOR_YELLOW, 590, 325);
draw_text("3 - Hard", COLOR_RED, 590, 355);
// Divider line
draw_line(COLOR_DARK_GRAY, 440, 375, 840, 375);
// Instructions
draw_text("Press 1, 2 or 3 to start", COLOR_GRAY, 545, 395);
draw_text("Press S to select block style", COLOR_LIGHT_GRAY, 520, 420);
draw_text("Press L to view Leaderboard", COLOR_LIGHT_GRAY, 525, 445);
draw_text("Press Q to quit", COLOR_GRAY, 580, 465);
}
/**
* Method: draw_game
* Purpose: Draws the active gameplay screen
*/
void dash_manager::draw_game()
{
current_level.draw();
draw_ground();
dash->draw();
draw_progress_bar();
// HUD
draw_text("DASH", COLOR_WHITE, 20, 20);
draw_text("Score: " + to_string(score), COLOR_WHITE, 20, 45);
draw_text("Speed: " + to_string((int)current_level.get_scroll_speed()), COLOR_GRAY, 20, 65);
draw_text("SPACE to jump", COLOR_GRAY, 20, 85);
}
/**
* Method: draw_end_screen
* Purpose: Draws the win or game over screen
*/
void dash_manager::draw_end_screen()
{
clear_screen(COLOR_BLACK);
if (current_state == WIN)
{
draw_text("YOU WIN!", COLOR_GREEN, 610, 200);
draw_text("Score: " + to_string(score), COLOR_WHITE, 600, 235);
draw_line(COLOR_DARK_GRAY, 440, 260, 840, 260);
draw_text("Enter your name:", COLOR_WHITE, 580, 285);
draw_text("(no spaces in name)", COLOR_GRAY, 570, 310);
set_interface_style(SHADED_DARK_STYLE);
player_name = text_box(player_name, rectangle_from(490, 320, 300, 35));
draw_interface();
draw_line(COLOR_DARK_GRAY, 440, 375, 840, 375);
draw_text("Press ENTER to save", COLOR_GRAY, 570, 395);
}
else
{
draw_text("GAME OVER", COLOR_RED, 605, 290);
draw_text("Score: " + to_string(score), COLOR_WHITE, 605, 325);
draw_line(COLOR_DARK_GRAY, 440, 345, 840, 345);
draw_text("Press ENTER to play again", COLOR_GRAY, 545, 365);
draw_text("Press Q to quit", COLOR_GRAY, 583, 390);
}
}
/**
* Method: draw_ground
* Purpose: Draws the ground line and fills below it
*/
void dash_manager::draw_ground()
{
fill_rectangle(COLOR_FOREST_GREEN, 0, GROUND_Y, SCREEN_WIDTH, SCREEN_HEIGHT - GROUND_Y);
draw_line(COLOR_WHITE, 0, GROUND_Y, SCREEN_WIDTH, GROUND_Y);
}