-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
513 lines (438 loc) · 13.5 KB
/
game.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
#define OLC_PGE_APPLICATION
#include <cassert>
#include <olcPixelGameEngine.h>
#include <player.h>
#include <terrain.h>
class LiaGame : public olc::PixelGameEngine
{
public:
LiaGame()
{
sAppName = "Lia";
}
private:
olc::Sprite *livesSprite;
bool isDraggingShovel;
bool isDraggingAnderson;
bool renderShovel;
// Scores
bool didFindAnderson;
bool didBuryAnderson;
bool didInteractWithDog;
// UI Controls
bool didGameStart;
bool isGameOver;
bool isDialogOpen;
public:
character::player *player;
character::player *anderson;
character::character *shovel;
character::player *tree;
character::player *dog;
terrain::terrain *terrain;
olc::ResourcePack *pack;
public:
bool OnUserCreate() override
{
player = new character::player("Lia");
player->SetPack(pack);
player->EnableControls();
player->SetPosition({10.0f, 10.0f});
player->SetState("idle", 1, true);
anderson = new character::player("Anderson", andersonDefinition);
anderson->SetPack(pack);
anderson->SetPosition({100.0f, 100.0f});
anderson->SetState("idle", 1, true);
anderson->coins = rand() % 10 + 1;
shovel = new character::player("Shovel", shovelDefinition);
shovel->SetPack(pack);
shovel->SetPosition({200.0f, 200.0f});
shovel->SetState("idle", 1, true);
dog = new character::player("Dog", dogDefinition);
dog->SetPack(pack);
dog->SetPosition({200.0f, 250.0f});
dog->SetState("idle", 2, true);
tree = new character::player("Tree", treeDefinition);
tree->SetPack(pack);
tree->SetPosition({200.0f, 80.0f});
tree->SetState("idle", 2, true);
livesSprite = new olc::Sprite("./gfx/lives.png", this->pack);
terrain = new terrain::terrain();
terrain->Create(ScreenWidth(), ScreenHeight(), 100, 1);
didGameStart = false;
isGameOver = false;
isDraggingShovel = false;
isDialogOpen = false;
renderShovel = false;
didFindAnderson = false;
isDraggingAnderson = false;
didInteractWithDog = false;
didBuryAnderson = false;
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
Clear(olc::BLACK);
if (isGameOver || player->IsDead())
{
DrawGameOverScreen();
return true;
}
if (!didGameStart)
{
DrawStartScreen();
return true;
}
terrain->Update(this, fElapsedTime);
anderson->Draw(this, fElapsedTime);
dog->Draw(this, fElapsedTime);
player->Draw(this, fElapsedTime);
DrawShovel();
InteractWithAnderson();
InteractWithShovel();
InteractWithDog();
tree->Draw(this, fElapsedTime, 3);
DrawUI();
return true;
}
private:
void InteractWithAnderson()
{
bool collidesWithHole = terrain->CollidesWithHole(anderson->box);
bool collidesWithShovel = shovel->box->Intersects(anderson->box);
if (collidesWithHole)
{
KillAnderson();
}
if (player->box->Intersects(anderson->box))
{
if (anderson->IsDead())
{
if (anderson->coins > 0)
{
ShowDialog("Holy shit! You killed him!!\nPress SPACE to steal his coins!", 0.0f);
}
else if (!collidesWithHole)
{
if (!terrain->HasHoles())
{
ShowDialog("You need to bury him. Find a shovel", 0.0f);
renderShovel = true;
}
else if (isDraggingAnderson)
ShowDialog("Go to the hole", 0.0f);
else
ShowDialog("Press SPACE to drag the body", 0.0f);
}
else
{
if (isDraggingAnderson)
ShowDialog("Press SPACE to drop the body", 0.0f);
else
{
if (!collidesWithShovel)
{
ShowDialog("Go get the shovel", 0.0f);
}
else
{
if (isDraggingShovel)
ShowDialog("Press SPACE to bury the body", 0.0f);
}
}
}
}
else
{
ShowDialog("You found Anderson!!\nPress SPACE to interact", 0.0f);
FindAnderson();
}
if (GetKey(olc::Key::SPACE).bPressed)
{
if (anderson->IsDead())
{
if (anderson->coins > 0)
{
StealAnderson();
}
else if (collidesWithHole && collidesWithShovel)
{
didBuryAnderson = true;
}
else
{
isDraggingAnderson = !isDraggingAnderson;
}
}
else
{
KillAnderson();
}
}
}
if (isDraggingAnderson)
{
DragAndersonBody();
}
}
void InteractWithShovel()
{
if (!renderShovel)
{
return;
}
bool collidesWithHole = terrain->CollidesWithHole(shovel->box);
bool collidesWithAnderson = anderson->box->Intersects(shovel->box);
if (player->box->Intersects(shovel->box))
{
if (GetKey(olc::Key::SPACE).bPressed)
{
if (collidesWithHole)
{
isDraggingShovel = true;
ShowDialog("Can't drop the shovel here", 0.0f);
}
else
{
isDraggingShovel = !isDraggingShovel;
}
}
if (GetKey(olc::Key::R).bPressed)
{
AddHoleAtPlayerPosition();
}
if (!terrain->HasHoles())
{
if (isDraggingShovel)
ShowDialog("Press R to dig a hole", 0.0f);
else
ShowDialog("Press SPACE to pick up the shovel", 0.0f);
}
else if (terrain->CollidesWithHole(shovel->box))
{
if (collidesWithAnderson)
{
if (didBuryAnderson)
isGameOver = true;
else
ShowDialog("Press SPACE to bury the body", 0.0f);
}
else
{
ShowDialog("Go to Anderson", 0.0f);
}
}
else
{
if (!collidesWithAnderson)
{
if (isDraggingShovel)
ShowDialog("Go to Anderson", 0.0f);
else
ShowDialog("Press SPACE to pick up the shovel", 0.0f);
}
}
}
}
void InteractWithDog()
{
if (player->box->Intersects(dog->box))
{
if (didInteractWithDog)
{
ShowDialog("The dog seems happy!", 0.0f);
return;
}
if (GetKey(olc::Key::SPACE).bPressed)
{
didInteractWithDog = true;
player->XP += 10;
player->coins += 2;
player->TakeLife();
}
else
{
ShowDialog("Press SPACE to interact with the dog", 0.0f);
}
}
}
private:
void KillAnderson()
{
if (anderson->IsDead())
{
return;
}
player->XP += 10;
anderson->SetState("dead");
anderson->Kill();
}
void FindAnderson()
{
if (didFindAnderson)
{
return;
}
didFindAnderson = true;
player->XP += 10;
}
void ShowDialog(std::string text, float fElapsedTime, float timeout = 3.0f)
{
isDialogOpen = true;
DrawDialog(text);
}
void DrawDialog(std::string text)
{
if (!isDialogOpen)
{
return;
}
int padding = 10;
int width = ScreenWidth() - padding * 2;
int height = 20 + padding * 2;
FillRect(olc::vi2d(padding, padding), olc::vi2d(width, height), olc::VERY_DARK_GREEN);
DrawString(olc::vi2d(padding + 5, padding + 5), text, olc::WHITE, 1);
}
void AddHoleAtPlayerPosition()
{
olc::vf2d pos = *player->GetPosition();
pos.x += 8;
pos.y += 8;
terrain->AddHole(pos);
}
void StealAnderson()
{
if (!anderson->IsDead() || anderson->coins <= 0)
{
return;
}
player->XP += 10;
player->coins += anderson->coins;
anderson->coins = 0;
}
void DragAndersonBody()
{
if (!anderson->IsDead())
{
return;
}
anderson->SetPosition(*player->GetPosition());
}
void DrawShovel()
{
if (!renderShovel)
{
return;
}
olc::vf2d *pos = shovel->GetPosition();
SetPixelMode(olc::Pixel::MASK);
bool flip = player->isFacingLeft | !isDraggingShovel;
DrawSprite(*pos, shovel->GetSprite(), 1, flip);
if (isDraggingShovel)
{
shovel->SetPosition(*player->GetPosition());
}
}
void DrawUI()
{
int y = ScreenHeight() - 20;
int spriteWidth = livesSprite->width;
// Lives
for (int i = 0; i < player->GetLives(); i++)
{
SetPixelMode(olc::Pixel::MASK);
DrawSprite(olc::vi2d(spriteWidth * i + 10, y), livesSprite);
SetPixelMode(olc::Pixel::NORMAL);
}
// XP
DrawString(olc::vi2d(70, y + 4), "XP: " + std::to_string(player->XP), olc::WHITE, 1);
// Draw Coins
int x = 130, radius = 5, coins = player->coins;
for (int i = 0; i < coins * 2; i++)
{
bool isOdd = i % 2 != 0;
olc::Pixel color = isOdd ? olc::YELLOW : olc::DARK_YELLOW;
FillCircle(olc::vi2d(x + i * 2, y + radius), radius, color);
}
}
void DrawStartScreen()
{
Clear(olc::VERY_DARK_BLUE);
if (GetKey(olc::Key::SPACE).bPressed)
{
didGameStart = true;
}
SetPixelMode(olc::Pixel::MASK);
DrawString(olc::vi2d(10, 10), "Welcome, " + player->GetName() + "! XD", olc::WHITE, 1);
DrawSprite(olc::vi2d(10, 60), anderson->GetSprite(), 3);
DrawSprite(olc::vi2d(50, 90), player->GetSprite(), 4, true);
// Start game
DrawString(olc::vi2d(10, ScreenHeight() - 20), "Press SPACE to start", olc::WHITE, 1);
DrawString(olc::vi2d(ScreenWidth() - 200, ScreenHeight() - 20), "Developed by Anderson!", olc::DARK_GREY, 1);
}
void DrawGameOverScreen()
{
Clear(olc::BLACK);
if (GetKey(olc::Key::R).bPressed)
{
OnUserCreate();
isGameOver = false;
}
int width = ScreenWidth() / 2;
int height = ScreenHeight() / 2;
DrawString(olc::vi2d(width - 75, height - 10), "Game Over", olc::WHITE, 2);
if (didBuryAnderson)
DrawString(olc::vi2d(width - 100, height + 10), "You buried Anderson ^-^", olc::WHITE, 1);
DrawString(olc::vi2d(width - 80, height + 30), "Thx for playing s2", olc::VERY_DARK_GREY, 1);
DrawString(olc::vi2d(width - 80, ScreenHeight() - 30), "Press R to restart", olc::VERY_DARK_GREY, 1);
}
};
void CompileResourcePack(std::string packName, std::string password)
{
olc::ResourcePack *pack = new olc::ResourcePack();
// add all files to the pack from gfx folder
std::string path = "./gfx/";
for (const auto &entry : std::filesystem::directory_iterator(path))
{
std::string filename = entry.path().string();
std::cout << "Adding file to pack: " << filename << std::endl;
pack->AddFile(filename);
}
pack->SavePack(packName, password);
}
int main(int argc, char const *argv[])
{
std::vector<std::string> all_args;
std::string compilePassPhrase = "I like cookies";
std::string executablePath = argv[0];
executablePath = executablePath.substr(0, executablePath.find_last_of("/\\"));
std::string packName = executablePath + "/game.dat";
if (argc > 1)
{
std::string first_arg = argv[1];
if (first_arg == "--pack")
{
std::cout << "Compiling resource pack..." << std::endl;
CompileResourcePack(packName, compilePassPhrase);
return 0;
}
}
olc::ResourcePack *pack = new olc::ResourcePack();
if (std::filesystem::exists(packName))
{
std::cout << "Loading resource pack..." << packName << std::endl;
pack->LoadPack(packName, compilePassPhrase);
pack->pubsync();
}
else
{
std::cout << "Resource pack not found." << packName << std::endl;
return 1;
}
srand(time(NULL));
LiaGame game;
game.pack = pack;
if (game.Construct(400, 300, 3, 3))
game.Start();
return 0;
}