-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.cpp
749 lines (687 loc) · 17 KB
/
App.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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <string>
#include <sstream>
#include <conio.h>
#include <stdio.h>
#include <Windows.h>
#include "Player.h"
#include "Spike.h"
#include "Block.h"
#include "Console.h"
#include "Portal.h"
#include "rapidxml.hpp"
// remove stupid warnings
#pragma warning (disable : 6262)
#pragma warning (disable : 26812)
#define DEBUG_MODE 0
Player player;
bool quit, paused, debug, mainMenu, editor, levelSelect;
screensize_t screenSize;
int groundY, camera, finish;
std::vector<Spike> spikes, spikesEditor;
std::vector<Block> blocks, blocksEditor;
std::vector<Portal> portals, portalsEditor;
int editorX, editorY, editorSelectedID;
int levelSelectCursor, levelSelectLevels;
std::vector<std::string> levels;
const char* SelectedIDToStr(void)
{
if (editorSelectedID == 1)
{
return "Spike";
}
else if (editorSelectedID == 2)
{
return "Block";
}
else if (editorSelectedID == 3)
{
return "Slab";
}
else if (editorSelectedID == 4)
{
return "Cube Portal";
}
else if (editorSelectedID == 5)
{
return "Ship Portal";
}
return "Error";
}
void LoadLevel(const char* path)
{
blocks.clear();
spikes.clear();
portals.clear();
std::ifstream levelFile(path);
if (!levelFile.is_open())
{
std::cout << "Failed to open level file (" << path << ")" << std::endl;
std::cin.get();
return;
}
std::string levelData((std::istreambuf_iterator<char>(levelFile)),
std::istreambuf_iterator<char>());
rapidxml::xml_document<> doc;
doc.parse<0>(const_cast<char*>(levelData.c_str()));
rapidxml::xml_node<>* root = doc.first_node();
ClearScreen();
// iterate through all nodes in root
for (rapidxml::xml_node<>* node = root->first_node(); node; node = node->next_sibling())
{
if (node->name() == std::string("spike"))
{
int x = atoi(node->first_attribute("x")->value()) + player.GetStartX();
int y = groundY - atoi(node->first_attribute("y")->value());
spikes.push_back(Spike(x, y));
std::cout << "Spike (" << x << ", " << y << ")\n";
}
else if (node->name() == std::string("block"))
{
int x = atoi(node->first_attribute("x")->value()) + player.GetStartX();
int y = groundY - atoi(node->first_attribute("y")->value());
bool slab = false;
if (node->first_attribute("slab") != NULL && node->first_attribute("slab")->value() == std::string("true"))
{
slab = true;
}
blocks.push_back(Block(x, y, slab));
std::cout << "Block (" << x << ", " << y << ")\n";
}
else if (node->name() == std::string("portal"))
{
int x = atoi(node->first_attribute("x")->value()) + player.GetStartX();
int y = groundY - atoi(node->first_attribute("y")->value());
GameMode mode = StringToGameMode(std::string(node->first_attribute("mode")->value()));
portals.push_back(Portal(x, y, mode));
std::cout << "Portal (" << x << ", " << y << ")\n";
}
else
{
std::cout << "Unknown node: " << node->name() << std::endl;
}
}
int maxX = 0;
for (auto block : blocks)
{
if (block.GetX() > maxX)
{
maxX = block.GetX();
}
}
for (auto spike : spikes)
{
if (spike.GetX() > maxX)
{
maxX = spike.GetX();
}
}
for (auto portal : portals)
{
if (portal.GetX() > maxX)
{
maxX = portal.GetX();
}
}
finish = maxX + 12;
}
void LoadLevelEditor(const char *path)
{
LoadLevel(path);
// copy blocks and spikes to editor
spikesEditor = spikes;
blocksEditor = blocks;
portalsEditor = portals;
spikes.clear();
blocks.clear();
portals.clear();
}
void Restart(void)
{
camera = 0;
player.Reset();
paused = false;
}
void ResetEditorCursor(void)
{
editorY = groundY - 1;
editorX = player.GetStartX();
}
void Input(void)
{
if (_kbhit())
{
if (!mainMenu && !editor && !levelSelect)
{
if (paused)
{
// pause menu keys
switch (_getch())
{
case 27:
paused = false;
break;
case 'm':
case 'M':
mainMenu = true;
paused = false;
player.SetLevelComplete(false);
blocks.clear();
spikes.clear();
break;
case 'r':
case 'R':
Restart();
break;
}
}
else
{
// gameplay keys
switch (_getch())
{
case 27:
paused = player.IsLevelComplete() ? false : true;
break;
case 'd':
case 'D':
debug = !debug;
break;
case ' ':
player.Jump();
if (player.IsLevelComplete())
{
mainMenu = true;
player.SetLevelComplete(false);
blocks.clear();
spikes.clear();
}
break;
case 'i':
case 'I':
if (debug)
player.ToggleInvincibility();
break;
case 'r':
case 'R':
if (player.IsLevelComplete())
{
Restart();
}
break;
}
}
}
else
{
if (editor)
{
// editor keys
switch (_getch())
{
case 27: // quit
editor = false;
mainMenu = true;
break;
// wasd movement
case 'w':
case 'W':
editorY--;
break;
case 'a':
case 'A':
editorX--;
break;
case 's':
case 'S':
editorY++;
break;
case 'd':
case 'D':
editorX++;
break;
case 'i':
case 'I': // prev obj
editorSelectedID--;
if (editorSelectedID < 1)
{
editorSelectedID = 1;
}
break;
case 'o':
case 'O': // next obj
editorSelectedID++;
if (editorSelectedID > 5)
{
editorSelectedID = 5;
}
break;
case ' ': // place obj
if (editorSelectedID == 1) // spike
{
spikesEditor.push_back(Spike(editorX, editorY));
}
else if (editorSelectedID == 2) // block
{
blocksEditor.push_back(Block(editorX, editorY, false));
}
else if (editorSelectedID == 3) // slab
{
blocksEditor.push_back(Block(editorX, editorY, true));
}
break;
case 'p':
case 'P': // erase obj
for (size_t i = 0; i < spikesEditor.size(); i++)
{
Spike spike = spikesEditor[i];
if (spike.GetX() == editorX && spike.GetY() == editorY)
{
spikesEditor.erase(spikesEditor.begin() + i);
}
}
for (size_t i = 0; i < blocksEditor.size(); i++)
{
Block block = blocksEditor[i];
if (block.GetX() == editorX && block.GetY() == editorY)
{
blocksEditor.erase(blocksEditor.begin() + i);
}
}
break;
case 'x':
case 'X': // save to file
CreateDirectoryA("levels", NULL);
std::string name = "";
ClearScreen();
GotoXY(0, 0);
printf("Enter level name: ");
std::cin >> name;
name += ".xml";
printf("Creating level data...\n");
std::string levelData = "";
levelData += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<level>\n";
for (auto spike : spikesEditor)
{
std::ostringstream ss;
ss << "\t<spike x=\"" << spike.GetGridX(player.GetStartX()) << "\" y=\"" << spike.GetGridY(groundY) << "\" />\n";
levelData += ss.str();
}
for (auto block : blocksEditor)
{
std::string slabStr = "";
if (block.IsSlab())
{
slabStr = "slab=\"true\" "; // extra space is important
}
std::ostringstream ss;
ss << "\t<block x=\"" << block.GetGridX(player.GetStartX()) << "\" y=\"" << block.GetGridY(groundY) << "\" " << slabStr << "/>\n";
levelData += ss.str();
}
levelData += "</level>\n";
printf(levelData.c_str());
printf("Writing to file...\n");
std::ofstream os;
os.open(std::string("levels\\") + name);
os << levelData;
os.close();
}
}
else if (levelSelect)
{
// level select menu keys
switch (_getch())
{
case 27:
levelSelect = false;
paused = false;
debug = false;
mainMenu = true;
editor = false;
break;
case 'w':
case 'W':
levelSelectCursor--;
if (levelSelectCursor < 0)
{
levelSelectCursor = levelSelectLevels - 1;
}
break;
case 's':
case 'S':
levelSelectCursor++;
if (levelSelectCursor > levelSelectLevels - 1)
{
levelSelectCursor = 0;
}
break;
case ' ': {
std::string levelPath = "levels\\" + levels[levelSelectCursor];
LoadLevel(levelPath.c_str());
mainMenu = false;
levelSelect = false;
levelSelectCursor = 0;
editor = false;
paused = false;
debug = false;
break;
}
case 'e': {
std::string levelPathh = "levels\\" + levels[levelSelectCursor];
LoadLevelEditor(levelPathh.c_str());
mainMenu = false;
levelSelect = false;
levelSelectCursor = 0;
editor = true;
paused = false;
debug = false;
break;
}
}
}
else
{
// main menu keys
switch (_getch())
{
case 27:
quit = true;
break;
case ' ':
mainMenu = false;
paused = false;
debug = false;
editor = false;
levelSelect = true;
break;
case 'e':
case 'E':
mainMenu = false;
paused = false;
debug = false;
editor = true;
camera = 0;
break;
}
}
}
}
}
void Update(void)
{
if (editor || mainMenu || levelSelect)
{
camera = 0;
}
if (paused || mainMenu || editor || levelSelect)
{
return;
}
if (!player.IsLevelComplete())
{
camera++;
}
player.Update(blocks, &camera, finish);
for (auto spike : spikes)
{
// when player touch spike player die
if (player.GetX() == spike.GetX() && player.GetY() == spike.GetY())
{
player.Die(&camera);
}
}
#if DEBUG_MODE
player.SetInvincibility(true);
#endif
}
void Draw(void)
{
SetConsoleColor(ConsoleColor::White, ConsoleColor::Black);
ClearScreen();
if (mainMenu) // main menu
{
const char *title = "Terminal Dash";
const char *press1 = "Press Space to play";
const char *press2 = "Press E to open level editor";
const char *press3 = "Press Escape to quit the game";
const char *gameBy = "Game by lispy2010 - lispy2010.github.io";
const char *version = "Version 0.1";
const int lenTitle = strlen(title);
const int lenPress1 = strlen(press1);
const int lenPress2 = strlen(press2);
const int lenPress3 = strlen(press3);
const int lenVersion = strlen(version);
GotoXY(screenSize.width / 2 - lenTitle / 2, 3);
printf(title);
GotoXY(screenSize.width / 2 - lenPress1 / 2, 6);
printf(press1);
GotoXY(screenSize.width / 2 - lenPress2 / 2, 7);
printf(press2);
GotoXY(screenSize.width / 2 - lenPress3 / 2, 8);
printf(press3);
GotoXY(2, screenSize.height - 2);
printf(gameBy);
GotoXY(screenSize.width - 2 - lenVersion, screenSize.height - 2);
printf(version);
return;
}
if (editor) // editor
{
// draw ground
for (int i = 0; i < screenSize.width; i++)
{
GotoXY(i, groundY);
SetConsoleColor(ConsoleColor::Green, ConsoleColor::Black);
printf("\"");
SetConsoleColor(ConsoleColor::White, ConsoleColor::Black);
}
// draw blocks
for (auto block : blocksEditor)
{
block.Draw(camera);
}
// draw spikes
for (auto spike : spikesEditor)
{
spike.Draw(camera);
}
// draw cursor
GotoXY(editorX, editorY);
SetConsoleColor(ConsoleColor::Black, ConsoleColor::Blue);
printf(" ");
SetConsoleColor(ConsoleColor::White, ConsoleColor::Black);
// draw instructions
const char *wasdKeys = "Use WASD to move cursor";
const char *escape = "Press Escape to quit";
const char *selectID = "Press I and O to select object";
const char *space = "Press Space to place object";
const char *erase = "Press P to erase object";
const char *save = "Press X to save level";
const int lenWasdKeys = strlen(wasdKeys);
const int lenEscape = strlen(escape);
const int lenSelectID = strlen(selectID);
const int lenSpace = strlen(space);
const int lenErase = strlen(erase);
const int lenSave = strlen(save);
// 2 in the x pos calculation is offset from right side
// 2 in the x pos calculation is offset from right side
GotoXY(screenSize.width - 2 - lenWasdKeys, 1);
printf(wasdKeys);
GotoXY(screenSize.width - 2 - lenEscape, 2);
printf(escape);
GotoXY(screenSize.width - 2 - lenSelectID, 3);
printf(selectID);
GotoXY(screenSize.width - 2 - lenSpace, 4);
printf(space);
GotoXY(screenSize.width - 2 - lenErase, 5);
printf(erase);
GotoXY(screenSize.width - 2 - lenSave, 6);
printf(save);
// editor info
GotoXY(1, screenSize.height - 2);
printf("%s selected", SelectedIDToStr());
return;
}
if (levelSelect) // level select screen
{
GotoXY(10, 5);
printf("Level select");
GotoXY(10, 6);
printf("Press Space to play selected level");
GotoXY(10, 7);
printf("Press Escape to quit this menu");
GotoXY(10, 8);
printf("Press W and S to select levels");
GotoXY(10, 9);
printf("Press E to edit level");
CreateDirectoryA("levels", NULL); // just in case (if it doesn't exist)
std::vector<std::string> files;
// get all xml files in levels folder
// https://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c
// answer by herohuyongtao (modified to actually work)
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(L"levels\\*.xml", &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
char filename[260] = {0};
WCHAR *wfn = fd.cFileName;
sprintf_s(filename, "%ws", wfn);
files.push_back(filename);
}
} while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
else
{
SetConsoleColor(ConsoleColor::Black, ConsoleColor::White);
printf("No levels found.");
SetConsoleColor(ConsoleColor::White, ConsoleColor::Black);
}
levels = files;
levelSelectLevels = files.size();
for (size_t i = 0; i < files.size(); i++)
{
GotoXY(10, 12 + (short)i);
if (levelSelectCursor == i)
{
SetConsoleColor(ConsoleColor::Black, ConsoleColor::White);
}
printf("%d. %s", i + 1, files[i].c_str());
SetConsoleColor(ConsoleColor::White, ConsoleColor::Black);
}
return;
}
// gameplay drawing
// draw ground
for (int i = 0; i < screenSize.width; i++)
{
GotoXY(i, groundY);
SetConsoleColor(ConsoleColor::Green, ConsoleColor::Black);
printf("\"");
SetConsoleColor(ConsoleColor::White, ConsoleColor::Black);
}
// draw spikes
for (auto spike : spikes)
{
spike.Draw(camera);
}
// draw blocks
for (auto block : blocks)
{
block.Draw(camera);
}
// draw second ground
if (!player.IsGameMode(GameMode::Cube))
{
for (int i = 0; i < screenSize.width; i++)
{
GotoXY(i, 4);
SetConsoleColor(ConsoleColor::Green, ConsoleColor::Black);
printf("\"");
SetConsoleColor(ConsoleColor::White, ConsoleColor::Black);
}
}
SetConsoleColor(ConsoleColor::LightGreen, ConsoleColor::Black);
// draw finish line
for (int i = 0; i < groundY; i++)
{
GotoXY(finish - camera, i);
printf("|");
}
// draw player
if (!player.IsLevelComplete())
{
player.Draw();
}
SetConsoleColor(ConsoleColor::White, ConsoleColor::Black);
// draw attempt count
GotoXY(1, screenSize.height - 2);
printf("Attempt %d", player.GetAttempt());
if (paused)
{
const char *text1 = "Press M to go to menu";
const char *text2 = "Press R to restart";
const int len1 = strlen(text1);
const int len2 = strlen(text2);
GotoXY(screenSize.width / 2 - 3, screenSize.height / 2);
printf("-PAUSED-");
GotoXY(screenSize.width / 2 - len1 / 2, screenSize.height / 2 + 1);
printf(text1);
GotoXY(screenSize.width / 2 - len2 / 2, screenSize.height / 2 + 2);
printf(text2);
}
if (player.IsLevelComplete())
{
const char *levelComplete = " Level complete! ";
const char *press1 = " Press Space to go to menu ";
const int lenLevelComplete = strlen(levelComplete);
const int lenPress1 = strlen(press1);
SetConsoleColor(ConsoleColor::Black, ConsoleColor::LightGreen);
GotoXY(screenSize.width / 2 - lenLevelComplete / 2, screenSize.height / 2 - 1);
printf(levelComplete);
GotoXY(screenSize.width / 2 - lenPress1 / 2, screenSize.height / 2);
printf(press1);
SetConsoleColor(ConsoleColor::White, ConsoleColor::Black);
}
if (debug)
{
GotoXY(1, 1);
printf("Debug Info");
GotoXY(1, 2);
printf("CWD: %s", _getcwd(NULL, 0));
GotoXY(1, 3);
printf("Player X: %d Y: %d", player.GetX(), player.GetY());
}
}
int main(void)
{
// init
player = Player(12, 19);
quit = paused = debug = editor = false;
mainMenu = true;
screenSize = GetScreenSize();
groundY = 20;
camera = 0;
editorSelectedID = 1;
levelSelectCursor = 0;
blocks.clear();
spikes.clear();
ResetEditorCursor();
HideCursor();
DisableConsoleSelection();
while (!quit)
{
Input();
Update();
Draw();
Sleep(100);
}
return 0;
}