-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.h
408 lines (347 loc) · 8.41 KB
/
header.h
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
#ifndef _HEADER_H_
#define _HEADER_H_
#include "Library.h"
class OXY
{
private:
public:
int x, y;
OXY(int x, int y);
OXY(pair<int, int> A);
bool invalid() const;
bool operator==(OXY b);
OXY operator+(OXY b);
};
class Pixel
{
public:
OXY coord;
char c;
string color;
Pixel(OXY _coord, char _c, const char *_color);
Pixel(OXY _coord, char _c, string _color);
Pixel operator+(OXY &other);
friend ostream &operator<<(ostream &os, const Pixel &pixel);
};
class TrafficLight
{
private:
OXY anchor;
int state; // 0 green, 1 yellow, 2 red
int cooldown;
const int defCooldown;
void resetCooldown();
void switchLight();
void draw();
public:
TrafficLight(OXY _anchor);
~TrafficLight();
bool isRed();
void run();
};
class Character
{
protected:
OXY anchor;
vector<Pixel> *pixels;
int cooldown;
const int defCooldown;
void resetCooldown();
public:
Character(OXY _anchor, vector<Pixel> *_pixels, int _defCooldown);
virtual ~Character();
bool checkCollision(Character *other);
OXY getAnchor();
};
class Player : public Character
{
private:
OXY prevAnchor;
public:
Player(OXY _anchor);
~Player();
vector<Pixel> *getPixels();
bool run();
void draw();
void erase(bool curPos);
void setDefPos(OXY _anchor);
};
class Obstacle : public Character
{
protected:
bool isMoving;
bool goRight;
const int power;
public:
Obstacle(OXY _anchor, vector<Pixel> *_pixels, int _power, int _defCooldown, bool _goRight);
virtual ~Obstacle();
virtual void setMove(bool move);
virtual void speak() = 0;
int getPower();
bool isOffScreen();
bool move();
void draw();
void erase(bool curPos);
};
class Pigeon : public Obstacle
{
private:
public:
Pigeon(OXY _anchor, int level, bool _goRight, int laneWidth);
~Pigeon();
void speak();
};
class Pterodactyl : public Obstacle
{
private:
public:
Pterodactyl(OXY _anchor, int level, bool _goRight, int laneWidth);
~Pterodactyl();
void speak();
};
class Plane : public Obstacle
{
private:
public:
Plane(OXY _anchor, int level, bool _goRight, int laneWidth);
~Plane();
void speak();
};
class Cougar : public Obstacle
{
private:
public:
Cougar(OXY _anchor, int level, bool _goRight, int laneWidth);
~Cougar();
void speak();
};
class Car : public Obstacle
{
private:
public:
Car(OXY _anchor, int level, bool _goRight, int laneWidth);
~Car();
void speak();
void setMove(bool move);
};
class Ceratosaurus : public Obstacle
{
private:
public:
Ceratosaurus(OXY _anchor, int level, bool _goRight, int laneWidth);
~Ceratosaurus();
void speak();
};
class Lane
{
protected:
OXY anchor;
int laneWidth;
int cooldown;
const int defCooldown;
TrafficLight *light;
void drawLane(bool lastLane);
public:
Lane(OXY _anchor, int _laneWidth, int _defCooldown, bool hasLight, bool lastLane);
virtual ~Lane();
virtual void run(int level) = 0;
virtual bool checkCollision(Player &player) = 0;
virtual void resumeLane(bool lastLane) = 0;
};
template <class T>
class SimpleLane : public Lane
{
private:
list<T> obstacles;
bool goRight;
bool spawnObstacle(int level);
void resetCooldown();
void changeLight();
public:
SimpleLane(OXY _anchor, int _laneWidth, int level, bool hasLight, bool lastLane);
~SimpleLane();
void run(int level);
bool checkCollision(Player &player);
void resumeLane(bool lastLane);
};
class ChaoticLane : public Lane
{
private:
list<Obstacle *> obstacles;
void spawnObstacle(int level, bool &plane);
void resetCooldown();
void changeLight();
public:
ChaoticLane(OXY _anchor, int _laneWidth, int level, bool hasLight, bool lastLane);
~ChaoticLane();
void run(int level);
bool checkCollision(Player &player);
void resumeLane(bool lastLane);
};
class InGame;
class JurassicRoad
{
private:
int difficulty;
int level;
Player player;
list<Lane *> lanes;
InGame *ingame;
public:
JurassicRoad(int _level, int _difficulty, InGame *_ingame);
~JurassicRoad();
int run();
bool checkWin();
bool checkDeath();
void resume();
};
class InGame {
private:
string username;
int32_t score, maxlevel, level, difficulty, endless_lvl;
JurassicRoad *game;
void gameTitle(int x, int y);
bool startMenu(int x, int y);
bool playMenu(int x, int y);
bool gameMenu(int x, int y);
bool loadMenu(int x, int y);
bool saveMenu(int x, int y);
bool instructionMenu(int x, int y);
bool difficultyMenu(int x, int y);
int menu(int x, int y, vector<string> &options, string menu_name);
bool settingsMenu(int x, int y);
void readTitle(string dir, int x, int y);
string linkBoard(int x, int y);
void saveFile(int x = 5, int y = 15);
bool loadGame(string filePath = "", bool music = 0);
void CantLoadFile(int x = 0, int y = 0);
bool Dead(int x = 0, int y = 0);
bool Win(int x = 0, int y = 0);
void Score(int num, int x = 0, int y = 0);
public:
InGame();
~InGame();
void run();
void gameplay(int x, int y);
int pauseMenu(int x, int y);
bool hasSound;
};
void gotoxy(int x, int y);
void ShowConsoleCursor(bool showFlag);
void gotoxy(OXY coord);
void FixConsoleWindow();
void SetWindowSize(SHORT width, SHORT height);
void playSound(const wchar_t *file);
void changeFont(int W, int H);
// void hidecursor();
void loadCharacters();
void loadTexture(const char *path, vector<Pixel> *g_pixels, const char *color);
template <class T>
SimpleLane<T>::SimpleLane(OXY _anchor, int _laneWidth, int level, bool hasLight, bool lastLane)
: Lane(_anchor, _laneWidth, 6 * (SPAWN_DEF_SPEED - level * SPAWN_LVL_SPEED), hasLight, lastLane), goRight(rand() % 2)
{
}
template <class T>
SimpleLane<T>::~SimpleLane()
{
for (auto &obstacle : obstacles)
{
obstacle.erase(true);
}
delete light;
}
template <class T>
void SimpleLane<T>::run(int level)
{
if (light)
{
light->run();
changeLight();
}
if (cooldown <= 0 && (!light || (light && !light->isRed())))
{
if (spawnObstacle(level)) resetCooldown();
}
else
--cooldown;
for (auto itr = obstacles.begin(); itr != obstacles.end();)
{
if (itr->move())
{
if (itr->isOffScreen())
{
itr->erase(false);
auto tmp = itr++;
obstacles.erase(tmp);
}
else
{
itr->erase(false);
itr->draw();
++itr;
}
}
else
++itr;
}
}
template <class T>
void SimpleLane<T>::changeLight()
{
for (T &obstacle : obstacles)
{
obstacle.setMove(!light->isRed());
}
}
template <class T>
void SimpleLane<T>::resetCooldown()
{
cooldown = defCooldown;
}
template <class T>
void SimpleLane<T>::resumeLane(bool lastLane)
{
drawLane(lastLane);
for (auto &obstacle : obstacles)
{
obstacle.draw();
}
}
template <class T>
bool SimpleLane<T>::spawnObstacle(int level)
{
T obstacle(anchor, level, goRight, laneWidth);
if (obstacles.empty()) {
obstacles.push_back(obstacle);
return true;
}
else {
Character* thisObstacle = &obstacle;
Character* thatObstacle = &obstacles.back();
if (thisObstacle->checkCollision(thatObstacle)) return false;
obstacles.push_back(obstacle);
return true;
}
}
template <class T>
bool SimpleLane<T>::checkCollision(Player &player)
{
bool outside = true;
for (auto &pixel : *player.getPixels())
{
if ((pixel.coord + player.getAnchor()).y > anchor.y && (pixel.coord + player.getAnchor()).y < anchor.y + laneWidth + 2)
{
outside = false;
break;
}
}
if (outside)
return false;
for (auto &obstacle : obstacles)
{
if (obstacle.checkCollision(&player))
return true;
}
return false;
}
#endif