-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaintest.cpp
More file actions
439 lines (363 loc) · 16.3 KB
/
Copy pathmaintest.cpp
File metadata and controls
439 lines (363 loc) · 16.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
430
431
432
433
434
435
436
437
438
439
#include "maintest.h"
// Escape sequences for cursor positioning and movement
// \033[<L>;<C>H -> move cursor to line L, column C
int main() {
// Enum for game states
typedef enum {
STATE_MENU,
STATE_HTP,
STATE_GAME,
STATE_CONTIN,
STATE_EXIT,
} game_states;
// Get the dimensions of the terminal window
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
int rows = w.ws_row;
int cols = w.ws_col;
// Calculate the coordinates for centering the text
int startRow = rows / 2;
int startCol = cols / 2; // Adjust for the length of the longest string
// current state of the game
game_states current_state;
current_state = STATE_MENU;
// Progress, keyboard, window, player declaration
Progress progress;
Keyboard keyboard;
Window window;
Player player;
// load progress
if (progress.checking_loading()) {
progress.load_progress(player);
}
// if ending is 7, which is the true ending, don allow to play
if (progress.ending_num == 7) {
system("clear");
while (progress.ending_num == 7) {
cout << font_blue << dim;
draw_logo(rows, cols);
cout << reset_format;
string contents = "Determine your own destiny! We are living a happy life here. ----- By Hero";
cout << default_format << startRow - logo_h_size + 9 << ";" << startCol - (contents.length())/2 << "H" << contents << endl;
keyboard.get_userInput();
if (keyboard.key == KEY_EXIT || keyboard.key == KEY_ENTER || keyboard.key == KEY_SPACE) {
system("clear");
cout << show_cursor << endl; // Show cursor
return 0;
}
}
}
// Buttons
const string button_start_game[2] = {
"> NEW GAME <",
" New Game ",
};
const string button_continue[2] = {
"> CONTINUE <",
" Continue ",
};
const string button_HTP[2] = {
"> HOW TO PLAY <",
" How to Play ",
};
const string button_exit[2] = {
"> EXIT <",
" Exit ",
};
// content for how to play
const string how_to_play[10] = {
"How to Play",
"W - Move Up E - Action ",
"A - Move Left Q - Exit to menu ",
"S - Move Down SPACE - Next page",
"D - Move Right ENTER - Confirm ",
"Walk away to cancel chat with NPCs ",
"Exit to menu and save your progress by pressing Q ",
"Unlock different endings with different choices ",
"Enjoy the movie... ",
"Good luck"
};
static int menu_button = 0;
// Main loop
while (current_state != STATE_EXIT) {
// Clear screen
system("clear");
std::cout << save_cursor_position; // Save cursor position
std::cout << hide_cursor;
// Predine the variables
int select_start_game = 1, select_HTP = 1, select_exit = 1, select_continue = 1;
int choice_button = 0;
// In menu state
switch(current_state) {
// Menu
case STATE_MENU:
system("clear");
keyboard.key = 0;
while (keyboard.key != KEY_ENTER) {
// Draw the logo
draw_logo(rows, cols);
// button
if (keyboard.key == KEY_UP) {
menu_button--;
if (menu_button <= 0) {
menu_button = 0;
}
}
if (keyboard.key == KEY_DOWN) {
menu_button++;
}
// if the player saved, will have continue button
if (progress.checking_loading()) {
if (menu_button >= 3) {
menu_button = 3;
}
// check the button
if (menu_button == 0) {
select_start_game = 0;
select_continue = 1;
select_HTP = 1;
select_exit = 1;
}
else if (menu_button == 1) {
select_start_game = 1;
select_continue = 0;
select_HTP = 1;
select_exit = 1;
}
else if (menu_button == 2) {
select_start_game = 1;
select_continue = 1;
select_HTP = 0;
select_exit = 1;
}
else if (menu_button == 3){
select_start_game = 1;
select_continue = 1;
select_HTP = 1;
select_exit = 0;
}
// Position the cursor and print the text at the center
std::cout << default_format << startRow - logo_h_size + 9 << ";" << startCol - (button_start_game[select_start_game].length())/2 << "H" << button_start_game[select_start_game] << endl;
std::cout << default_format << startRow - logo_h_size + 11 << ";" << startCol - (button_continue[select_continue].length())/2 << "H" << button_continue[select_continue] << endl;
std::cout << default_format << startRow - logo_h_size + 13 << ";" << startCol - (button_HTP[select_HTP].length())/2 << "H" << button_HTP[select_HTP] << endl;
std::cout << default_format << startRow - logo_h_size + 15 << ";" << startCol - (button_exit[select_exit].length())/2 << "H" << button_exit[select_exit] << endl;
// Click handler
switch(menu_button) {
case 0:
current_state = STATE_GAME;
break;
case 1:
current_state = STATE_CONTIN;
break;
case 2:
current_state = STATE_HTP;
break;
case 3:
current_state = STATE_EXIT;
break;
}
}
else {
if (menu_button >= 2) {
menu_button = 2;
}
// check the button
if (menu_button == 0) {
select_start_game = 0;
select_HTP = 1;
select_exit = 1;
}
else if (menu_button == 1) {
select_start_game = 1;
select_HTP = 0;
select_exit = 1;
}
else if (menu_button == 2) {
select_start_game = 1;
select_HTP = 1;
select_exit = 0;
}
// Position the cursor and print the text at the center, without continue button
std::cout << default_format << startRow - logo_h_size + 9 << ";" << startCol - (button_start_game[select_start_game].length())/2 << "H" << button_start_game[select_start_game] << endl;
std::cout << default_format << startRow - logo_h_size + 11 << ";" << startCol - (button_HTP[select_HTP].length())/2 << "H" << button_HTP[select_HTP] << endl;
std::cout << default_format << startRow - logo_h_size + 13 << ";" << startCol - (button_exit[select_exit].length())/2 << "H" << button_exit[select_exit] << endl;
// Click handler
switch(menu_button) {
case 0:
current_state = STATE_GAME;
break;
case 1:
current_state = STATE_HTP;
break;
case 2:
current_state = STATE_EXIT;
break;
}
}
keyboard.get_userInput();
}
break;
// how to play
case STATE_HTP:
// How to play
for (int i = 0; i < 9; i++) {
std::cout << default_format << 20 + i * 3 << ";" << startCol - (how_to_play[i].length())/2 << "H" << how_to_play[i] << endl;
}
keyboard.get_userInput();
// Exit to menu
if (keyboard.key == KEY_EXIT)
current_state = STATE_MENU;
break;
// Game
case STATE_GAME:
// Clear previous progress
progress.delete_progress();
window.is_Hero = false;
window.is_Girl = false;
player.reset_player();
window.intro_character_choice(player);
if (window.is_Hero) {
// Set player as hero
player.x = 9;
player.y = 9;
player.symbol = "|@|";
player.color = font_blue;
progress.map_code = 0;
progress.event_num = 1;
progress.ending_num = 0;
// Start game
Hero_run(progress, player);
}
else if (window.is_Girl) {
// set player as girl
player.x = 9;
player.y = 9;
player.symbol = "|%|";
player.color = font_purple;
progress.map_code = 1;
progress.event_num = 0;
progress.ending_num = 0;
// Start game
Girl_run(progress, player);
}
// Ending 7 is the true ending, then the game will end
if (progress.ending_num == 7) {
system("clear");
progress.save_progress(player);
while (progress.ending_num == 7) {
cout << font_blue << dim;
draw_logo(rows, cols);
cout << reset_format;
string contents = "Determine your own destiny! We are living a happy life here. ----- By Hero";
cout << default_format << startRow - logo_h_size + 9 << ";" << startCol - (contents.length())/2 << "H" << contents << endl;
keyboard.get_userInput();
if (keyboard.key == KEY_EXIT || keyboard.key == KEY_ENTER || keyboard.key == KEY_SPACE) {
system("clear");
cout << show_cursor << endl; // Show cursor
return 0;
}
}
}
// player can choose to save or not if not yet reach the ending
if (!player.reach_ending) {
window.handle_save_choice(choice_button, progress.map_code, player);
}
else if (progress.ending_num != 7){
// reset progress after reaching the ending, return to menu directly without choice
progress.reset_progress();
if (player.color == font_blue) {
player.x = 9;
player.y = 9;
progress.event_num = 1;
}
else if (player.color == font_purple) {
player.x = 9;
player.y = 9;
progress.event_num = 0;
progress.map_code = 1;
}
progress.save_progress(player);
current_state = STATE_MENU;
}
if (choice_button == 0)
// save progress
progress.save_progress(player);
// reset progress
current_state = STATE_MENU;
break;
case STATE_CONTIN:
// Load progress
progress.load_progress(player);
// Check if the player has reached the ending
if (player.reach_ending) {
progress.reset_progress();
window.is_Girl = false;
window.is_Hero = false;
window.intro_character_choice(player);
if (player.color == font_blue) {
player.x = 9;
player.y = 9;
progress.event_num = 1;
}
else if (player.color == font_purple) {
player.x = 9;
player.y = 9;
progress.event_num = 0;
progress.map_code = 1;
}
player.reset_player();
}
if (player.color == font_blue) {
window.is_Hero = true;
player.symbol = "|@|";
// Continue game
Hero_run(progress, player);
}
else if(player.color == font_purple) {
window.is_Girl = true;
player.symbol = "|%|";
// Continue game
Girl_run(progress, player);
}
// Ending 7 is the true ending, then the game will end
if (progress.ending_num == 7) {
system("clear");
progress.save_progress(player);
while (progress.ending_num == 7) {
cout << font_blue << dim;
draw_logo(rows, cols);
cout << reset_format;
string contents = "Determine your own destiny! We are living a happy life here. ----- By Hero";
cout << default_format << startRow - logo_h_size + 9 << ";" << startCol - (contents.length())/2 << "H" << contents << endl;
keyboard.get_userInput();
if (keyboard.key == KEY_EXIT || keyboard.key == KEY_ENTER || keyboard.key == KEY_SPACE) {
system("clear");
cout << show_cursor << endl; // Show cursor
return 0;
}
}
}
// player can choose to save or not if not yet reach the ending
if (!player.reach_ending) {
window.handle_save_choice(choice_button, progress.map_code, player);
}
else if (progress.ending_num != 7) {
// reset progress after reaching the ending and return to menu directly without choice
progress.reset_progress();
progress.save_progress(player);
current_state = STATE_MENU;
}
if (choice_button == 0)
// save progress
progress.save_progress(player);
// reset progress
current_state = STATE_MENU;
break;
case STATE_EXIT:
break;
}
}
system("clear");
std::cout << show_cursor << endl; // Show cursor
return 0;
}