-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayScene.cpp
More file actions
641 lines (628 loc) · 21.5 KB
/
PlayScene.cpp
File metadata and controls
641 lines (628 loc) · 21.5 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
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
#include <allegro5/allegro.h>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <functional>
#include <vector>
#include <queue>
#include <string>
#include <memory>
#include "AudioHelper.hpp"
#include "DirtyEffect.hpp"
#include "Enemy.hpp"
#include "GameEngine.hpp"
#include "Group.hpp"
#include "IObject.hpp"
#include "Image.hpp"
#include "Label.hpp"
// Turret
#include "PlugGunTurret.hpp"
#include "Plane.hpp"
#include "MachineGunTurret.hpp"
#include "DoubleMachineGunTurret.hpp"
#include "MeteorHammerTurret.hpp"
#include "AmplifierTurret.hpp"
// Enemy
#include "RedNormalEnemy.hpp"
#include "PlayScene.hpp"
#include "Resources.hpp"
#include "Sprite.hpp"
#include "Turret.hpp"
#include "TurretButton.hpp"
#include "LOG.hpp"
#include "DiceEnemy.hpp"
// Tool
#include "Shovel.hpp"
#include "Shifter.hpp"
bool PlayScene::DebugMode = false;
const std::vector<Engine::Point> PlayScene::directions = { Engine::Point(-1, 0), Engine::Point(0, -1), Engine::Point(1, 0), Engine::Point(0, 1) };
const int PlayScene::MapWidth = 20, PlayScene::MapHeight = 13;
const int PlayScene::BlockSize = 64;
const float PlayScene::DangerTime = 7.61;
const Engine::Point PlayScene::SpawnGridPoint = Engine::Point(-1, 0);
const Engine::Point PlayScene::EndGridPoint = Engine::Point(MapWidth, MapHeight - 1);
// TODO 5 (2/3): Set the cheat code correctly.
const std::vector<int> PlayScene::code = { ALLEGRO_KEY_UP , ALLEGRO_KEY_DOWN, ALLEGRO_KEY_LEFT, ALLEGRO_KEY_RIGHT, ALLEGRO_KEY_LEFT, ALLEGRO_KEY_RIGHT, ALLEGRO_KEY_ENTER};
Engine::Point PlayScene::GetClientSize() {
return Engine::Point(MapWidth * BlockSize, MapHeight * BlockSize);
}
void PlayScene::Initialize() {
// TODO 6 (1/2): There's a bug in this file, which crashes the game when you win. Try to find it.
// TODO 6 (2/2): There's a bug in this file, which doesn't update the player's life correctly when getting the first attack. Try to find it.
mapState.clear();
keyStrokes.clear();
ticks = 0;
deathCountDown = -1;
lives = 10;
money = 150;
SpeedMult = 1;
// Add groups from bottom to top.
AddNewObject(TileMapGroup = new Group());
AddNewObject(GroundEffectGroup = new Group());
AddNewObject(DebugIndicatorGroup = new Group());
AddNewObject(TowerGroup = new Group());
AddNewObject(EnemyGroup = new Group());
AddNewObject(BulletGroup = new Group());
AddNewObject(EffectGroup = new Group());
// Should support buttons.
AddNewControlObject(UIGroup = new Group());
ReadMap();
ReadEnemyWave();
mapDistance = CalculateBFSDistance();
ConstructUI();
imgTarget = new Engine::Image("play/target.png", 0, 0);
imgTarget->Visible = false;
preview = nullptr;
shifting = false;
UIGroup->AddNewObject(imgTarget);
// Preload Lose Scene
deathBGMInstance = Engine::Resources::GetInstance().GetSampleInstance("astronomia.ogg");
Engine::Resources::GetInstance().GetBitmap("lose/benjamin-happy.png");
// Preload Win Scene
Engine::Resources::GetInstance().GetBitmap("win/benjamin-sad.png");
// Start BGM.
// bgmId = AudioHelper::PlayBGM("play.ogg");
if (!mute)
bgmInstance = AudioHelper::PlaySample("play.ogg", true, AudioHelper::BGMVolume);
else
bgmInstance = AudioHelper::PlaySample("play.ogg", true, 0.0);
}
void PlayScene::Terminate() {
AudioHelper::StopBGM(bgmId);
AudioHelper::StopSample(bgmInstance);
AudioHelper::StopSample(deathBGMInstance);
deathBGMInstance = std::shared_ptr<ALLEGRO_SAMPLE_INSTANCE>();
IScene::Terminate();
}
void PlayScene::Update(float deltaTime) {
// If we use deltaTime directly, then we might have Bullet-through-paper problem.
// Reference: Bullet-Through-Paper
if (SpeedMult == 0)
deathCountDown = -1;
else if (deathCountDown != -1)
SpeedMult = 1;
// Calculate danger zone.
std::vector<float> reachEndTimes;
for (auto& it : EnemyGroup->GetObjects()) {
reachEndTimes.push_back(dynamic_cast<Enemy*>(it)->reachEndTime);
}
// Can use Heap / Priority-Queue instead. But since we won't have too many enemies, sorting is fast enough.
std::sort(reachEndTimes.begin(), reachEndTimes.end());
float newDeathCountDown = -1;
int danger = lives;
for (auto& it : reachEndTimes) {
if (it <= DangerTime) {
danger--;
if (danger <= 0) {
// Death Countdown
float pos = DangerTime - it;
if (it > deathCountDown) {
// Restart Death Count Down BGM.
AudioHelper::StopSample(deathBGMInstance);
if (SpeedMult != 0)
deathBGMInstance = AudioHelper::PlaySample("astronomia.ogg", false, AudioHelper::BGMVolume, pos);
}
float alpha = pos / DangerTime;
alpha = std::max(0, std::min(255, static_cast<int>(alpha * alpha * 255)));
dangerIndicator->Tint = al_map_rgba(255, 255, 255, alpha);
newDeathCountDown = it;
break;
}
}
}
deathCountDown = newDeathCountDown;
if (SpeedMult == 0)
AudioHelper::StopSample(deathBGMInstance);
if (deathCountDown == -1 && lives > 0) {
AudioHelper::StopSample(deathBGMInstance);
dangerIndicator->Tint.a = 0;
}
if (SpeedMult == 0)
deathCountDown = -1;
for (int i = 0; i < SpeedMult; i++) {
IScene::Update(deltaTime);
// Check if we should create new enemy.
ticks += deltaTime;
if (enemyWaveData.empty()) {
if (EnemyGroup->GetObjects().empty()) {
// Free resources.
/*
delete TileMapGroup;
delete GroundEffectGroup;
delete DebugIndicatorGroup;
delete TowerGroup;
delete EnemyGroup;
delete BulletGroup;
delete EffectGroup;
delete UIGroup;
delete imgTarget;
*/
Engine::GameEngine::GetInstance().ChangeScene("win");
}
continue;
}
auto current = enemyWaveData.front();
if (ticks < current.second)
continue;
ticks -= current.second;
enemyWaveData.pop_front();
const Engine::Point SpawnCoordinate = Engine::Point(SpawnGridPoint.x * BlockSize + BlockSize / 2, SpawnGridPoint.y * BlockSize + BlockSize / 2);
Enemy* enemy;
switch (current.first) {
case 1:
EnemyGroup->AddNewObject(enemy = new RedNormalEnemy(SpawnCoordinate.x, SpawnCoordinate.y));
break;
// TODO 2 (2/3): You need to modify 'resources/enemy1.txt', or 'resources/enemy2.txt' to spawn the new enemy.
// The format is "[EnemyId] [TimeDelay] [Repeat]".
// TODO 2 (3/3): Enable the creation of the new enemy.
case 2:
EnemyGroup->AddNewObject(enemy = new DiceEnemy(SpawnCoordinate.x, SpawnCoordinate.y));
break;
case 3:
EnemyGroup->AddNewObject(enemy = new DiceEnemy(SpawnCoordinate.x, SpawnCoordinate.y));
break;
default:
continue;
}
enemy->UpdatePath(mapDistance);
// Compensate the time lost.
enemy->Update(ticks);
}
if (preview) {
preview->Position = Engine::GameEngine::GetInstance().GetMousePosition();
// To keep responding when paused.
preview->Update(deltaTime);
}
}
void PlayScene::Draw() const {
IScene::Draw();
if (DebugMode) {
// Draw reverse BFS distance on all reachable blocks.
for (int i = 0; i < MapHeight; i++) {
for (int j = 0; j < MapWidth; j++) {
if (mapDistance[i][j] != -1) {
// Not elegant nor efficient, but it's quite enough for debugging.
Engine::Label label(std::to_string(mapDistance[i][j]), "pirulen.ttf", 32, (j + 0.5) * BlockSize, (i + 0.5) * BlockSize);
label.Anchor = Engine::Point(0.5, 0.5);
label.Draw();
}
}
}
}
}
void PlayScene::OnMouseDown(int button, int mx, int my) {
if ((button & 1) && !imgTarget->Visible && preview) {
// Cancel turret construct.
UIGroup->RemoveObject(preview->GetObjectIterator());
preview = nullptr;
}
IScene::OnMouseDown(button, mx, my);
}
void PlayScene::OnMouseMove(int mx, int my) {
IScene::OnMouseMove(mx, my);
const int x = mx / BlockSize;
const int y = my / BlockSize;
//|| dynamic_cast<Shovel*>(preview))
if (!preview || x < 0 || x >= MapWidth || y < 0 || y >= MapHeight) {
imgTarget->Visible = false;
return;
}
imgTarget->Visible = true;
imgTarget->Position.x = x * BlockSize;
imgTarget->Position.y = y * BlockSize;
}
void PlayScene::OnMouseUp(int button, int mx, int my) {
IScene::OnMouseUp(button, mx, my);
if (!imgTarget->Visible)
return;
const int x = mx / BlockSize;
const int y = my / BlockSize;
if (button & 1) {
if (mapState[y][x] != TILE_OCCUPIED) {
if (!preview)
return;
// Check if valid.
if (dynamic_cast<Shovel*> (preview) || dynamic_cast<Shifter*> (preview)) return;
if (!CheckSpaceValid(x, y)) {
Engine::Sprite* sprite;
GroundEffectGroup->AddNewObject(sprite = new DirtyEffect("play/target-invalid.png", 1, x * BlockSize + BlockSize / 2, y * BlockSize + BlockSize / 2));
sprite->Rotation = 0;
return;
}
// Purchase.
if (!shifting) {
EarnMoney(-preview->GetPrice());
}
else {
shifting = false;
}
// Remove Preview.
preview->GetObjectIterator()->first = false;
UIGroup->RemoveObject(preview->GetObjectIterator());
// Construct real turret.
preview->Enabled = true;
preview->Preview = false;
preview->Position.x = x * BlockSize + BlockSize / 2;
preview->Position.y = y * BlockSize + BlockSize / 2;
preview->Tint = al_map_rgba(255, 255, 255, 255);
TowerGroup->AddNewObject(preview);
// To keep responding when paused.
preview->Update(0);
// Remove Preview.
preview = nullptr;
mapState[y][x] = TILE_OCCUPIED;
OnMouseMove(mx, my);
} else if (mapState[y][x] == TILE_OCCUPIED && dynamic_cast<MachineGunTurret*>(preview)) {
if (!CheckSpaceValid(x, y)) {
Engine::Sprite* sprite;
GroundEffectGroup->AddNewObject(sprite = new DirtyEffect("play/target-invalid.png", 1, x * BlockSize + BlockSize / 2, y * BlockSize + BlockSize / 2));
sprite->Rotation = 0;
return;
}
auto turrets = TowerGroup->GetObjects();
bool matched = false;
for (auto it : turrets) {
if (dynamic_cast<MachineGunTurret*>(it) && it->Position.x == x * BlockSize + BlockSize / 2 && it->Position.y == y * BlockSize + BlockSize / 2) {
matched = true;
TowerGroup->RemoveObject(it->GetObjectIterator());
break;
}
}
if (!matched) return;
// Purchase.
EarnMoney(-preview->GetPrice());
// Remove Preview.
preview->GetObjectIterator()->first = false;
UIGroup->RemoveObject(preview->GetObjectIterator());
// Construct Double Machine Gun turret.
preview->Position.x = x * BlockSize + BlockSize / 2;
preview->Position.y = y * BlockSize + BlockSize / 2;
preview->Enabled = true;
preview->Preview = false;
preview->Tint = al_map_rgba(255, 255, 255, 255);
TowerGroup->AddNewObject(new DoubleMachineGunTurret(preview->Position.x, preview->Position.y));
// To keep responding when paused.
preview->Update(0);
// Remove Preview.
preview = nullptr;
mapState[y][x] = TILE_OCCUPIED;
OnMouseMove(mx, my);
}
else if (dynamic_cast<Shovel*>(preview)) {
dynamic_cast<Shovel*>(preview)->RemoveTurret(mx, my);
// Purchase.
// Remove Preview.
preview->GetObjectIterator()->first = false;
UIGroup->RemoveObject(preview->GetObjectIterator());
// To keep responding when paused.
preview->Update(0);
// Remove Preview.
preview = nullptr;
mapState[y][x] = TILE_FLOOR;
OnMouseMove(mx, my);
}
else if (dynamic_cast<Shifter*>(preview)) {
dynamic_cast<Shifter*>(preview)->ShiftTurret(x, y);
mapState[y][x] = TILE_FLOOR;
// Purchase.
// Remove Preview.
/*
preview->GetObjectIterator()->first = false;
UIGroup->RemoveObject(preview->GetObjectIterator());
preview->Update(0);
// Remove Preview.
preview = nullptr;
OnMouseMove(mx, my);
*/
// To keep responding when paused.
}
}
}
void PlayScene::OnKeyDown(int keyCode) {
IScene::OnKeyDown(keyCode);
if (keyCode == ALLEGRO_KEY_TAB) {
// TODO 5 (1/3): Set Tab as a code to active / de-active the debug mode.
if (DebugMode) DebugMode = false;
else DebugMode = true;
}
else {
keyStrokes.push_back(keyCode);
if (keyStrokes.size() > code.size())
keyStrokes.pop_front();
// TODO 5 (3/3): Check whether the input sequence corresponds to the code. If so, active a plane and earn 10000 money.
//Active a plane : EffectGroup->AddNewObject(new Plane());
//Earn money : money += 10000;
bool MatchTheCode = true;
int idx = 0;
if (keyStrokes.size() >= 7) {
for (int n : keyStrokes) {
if (n != code[idx]) {
MatchTheCode = false;
break;
}
idx++;
}
if (MatchTheCode) {
EffectGroup->AddNewObject(new Plane());
money += 10000;
}
}
}
if (keyCode == ALLEGRO_KEY_Q) {
// Hotkey for PlugGunTurret.
UIBtnClicked(0);
}
// TODO 3 (5/5): Make the W key to create the new turret.
else if (keyCode == ALLEGRO_KEY_W) {
// Hotkey for new turret.
UIBtnClicked(1);
}
else if (keyCode == ALLEGRO_KEY_E) {
UIBtnClicked(2);
}
else if (keyCode == ALLEGRO_KEY_S) {
UIBtnClicked(3);
}
else if (keyCode == ALLEGRO_KEY_M) {
UIBtnClicked(4);
}
else if (keyCode == ALLEGRO_KEY_R) {
UIBtnClicked(5);
}
else if (keyCode >= ALLEGRO_KEY_0 && keyCode <= ALLEGRO_KEY_9) {
// Hotkey for Speed up.
SpeedMult = keyCode - ALLEGRO_KEY_0;
}
else if (keyCode == ALLEGRO_KEY_M) {
// Hotkey for mute / unmute.
if (mute)
AudioHelper::ChangeSampleVolume(bgmInstance, AudioHelper::BGMVolume);
else
AudioHelper::ChangeSampleVolume(bgmInstance, 0.0);
mute = !mute;
}
}
void PlayScene::Hit() {
UILives->Text = std::string("Life ") + std::to_string(--lives);
if (lives <= 0) {
Engine::GameEngine::GetInstance().ChangeScene("lose");
}
}
int PlayScene::GetMoney() const {
return money;
}
void PlayScene::EarnMoney(int money) {
this->money += money;
UIMoney->Text = std::string("$") + std::to_string(this->money);
}
void PlayScene::ReadMap() {
std::string filename = std::string("resources/map") + std::to_string(MapId) + ".txt";
// Read map file.
char c;
std::vector<bool> mapData;
std::ifstream fin(filename);
while (fin >> c) {
switch (c) {
case '0': mapData.push_back(false); break;
case '1': mapData.push_back(true); break;
case '\n':
case '\r':
if (static_cast<int>(mapData.size()) / MapWidth != 0)
throw std::ios_base::failure("Map data is corrupted.");
break;
default: throw std::ios_base::failure("Map data is corrupted.");
}
}
fin.close();
// Validate map data.
if (static_cast<int>(mapData.size()) != MapWidth * MapHeight)
throw std::ios_base::failure("Map data is corrupted.");
// Store map in 2d array.
mapState = std::vector<std::vector<TileType>>(MapHeight, std::vector<TileType>(MapWidth));
for (int i = 0; i < MapHeight; i++) {
for (int j = 0; j < MapWidth; j++) {
const int num = mapData[i * MapWidth + j];
mapState[i][j] = num ? TILE_FLOOR : TILE_DIRT;
if (num)
TileMapGroup->AddNewObject(new Engine::Image("play/floor.png", j * BlockSize, i * BlockSize, BlockSize, BlockSize));
else
TileMapGroup->AddNewObject(new Engine::Image("play/dirt.png", j * BlockSize, i * BlockSize, BlockSize, BlockSize));
}
}
}
void PlayScene::ReadEnemyWave() {
std::string filename = std::string("resources/enemy") + std::to_string(MapId) + ".txt";
// Read enemy file.
float type, wait, repeat;
enemyWaveData.clear();
std::ifstream fin(filename);
while (fin >> type && fin >> wait && fin >> repeat) {
for (int i = 0; i < repeat; i++)
enemyWaveData.emplace_back(type, wait);
}
fin.close();
}
void PlayScene::ConstructUI() {
// Background
UIGroup->AddNewObject(new Engine::Image("play/sand.png", 1280, 0, 320, 832));
// Text
UIGroup->AddNewObject(new Engine::Label(std::string("Stage ") + std::to_string(MapId), "pirulen.ttf", 32, 1294, 0));
UIGroup->AddNewObject(UIMoney = new Engine::Label(std::string("$") + std::to_string(money), "pirulen.ttf", 24, 1294, 48));
UIGroup->AddNewObject(UILives = new Engine::Label(std::string("Life ") + std::to_string(lives), "pirulen.ttf", 24, 1294, 88));
// Buttons
ConstructButton(0, "play/turret-6.png", PlugGunTurret::Price);
// TODO 3 (3/5): Create a button to support constructing the new turret.
ConstructButton(1, "play/turret-1.png", MachineGunTurret::Price);
ConstructButton(2, "play/turret-8.png", MeteorHammerTurret::Price);
ConstructButton(3, "play/shovel.png", Shovel::Price);
ConstructButton(4, "play/shifter.png", Shifter::Price);
ConstructButton(5, "play/amplifier.png", AmplifierTurret::Price);
int w = Engine::GameEngine::GetInstance().GetScreenSize().x;
int h = Engine::GameEngine::GetInstance().GetScreenSize().y;
int shift = 135 + 25;
dangerIndicator = new Engine::Sprite("play/benjamin.png", w - shift, h - shift);
dangerIndicator->Tint.a = 0;
UIGroup->AddNewObject(dangerIndicator);
}
void PlayScene::ConstructButton(int id, std::string sprite, int price) {
TurretButton* btn;
// to determine the button's floor level
int level = id / 4;
// To determine the button's cell room;
int cell = id % 4;
if (id == 3) {
btn = new TurretButton("play/floor.png", "play/dirt.png",
Engine::Sprite(sprite, 1300 + cell * 76, 140 + level * 76, 0, 0, 0, 0),
Engine::Sprite(sprite, 1300 + cell * 76, 140 + level * 76, 0, 0, 0, 0)
, 1294 + cell * 76, 136 + level * 76, price);
}
else if (id == 4) {
btn = new TurretButton("play/floor.png", "play/dirt.png",
Engine::Sprite(sprite, 1297 + cell * 76, 140 + level * 76, 0, 0, 0, 0),
Engine::Sprite(sprite, 1297 + cell * 76, 140 + level * 76, 0, 0, 0, 0)
, 1294 + cell * 76, 136 + level * 76, price);
}
else {
btn = new TurretButton("play/floor.png", "play/dirt.png",
Engine::Sprite("play/tower-base.png", 1294 + cell * 76, 136 + level * 76, 0, 0, 0, 0),
Engine::Sprite(sprite, 1294 + cell * 76, 136 - 8 + level * 76, 0, 0, 0, 0)
, 1294 + cell * 76, 136 + level * 76, price);
}
// Reference: Class Member Function Pointer and std::bind.
btn->SetOnClickCallback(std::bind(&PlayScene::UIBtnClicked, this, id));
UIGroup->AddNewControlObject(btn);
}
void PlayScene::UIBtnClicked(int id) {
if (preview) {
UIGroup->RemoveObject(preview->GetObjectIterator());
preview = nullptr;
}
if (shifting) {
if (id == 0)
preview = new PlugGunTurret(0, 0);
// TODO 3 (4/5): On the new turret button callback, create the new turret.
else if (id == 1) {
preview = new MachineGunTurret(0, 0);
}
else if (id == 2) {
preview = new MeteorHammerTurret(0, 0);
}
else if (id == 3) {
preview = new Shovel(0, 0);
}
else if (id == 4) {
preview = new Shifter(0, 0);
}
else if (id == -1) {
preview = new DoubleMachineGunTurret(0, 0);
}
else if (id == 5) {
preview = new AmplifierTurret(0, 0);
}
}
else {
if (id == 0 && money >= PlugGunTurret::Price)
preview = new PlugGunTurret(0, 0);
// TODO 3 (4/5): On the new turret button callback, create the new turret.
else if (id == 1 && money >= MachineGunTurret::Price) {
preview = new MachineGunTurret(0, 0);
}
else if (id == 2 && money >= MeteorHammerTurret::Price) {
preview = new MeteorHammerTurret(0, 0);
}
else if (id == 3 && money >= Shovel::Price) {
preview = new Shovel(0, 0);
}
else if (id == 4 && money >= Shifter::Price) {
preview = new Shifter(0, 0);
}
else if (id == -1 && money >= DoubleMachineGunTurret::Price) {
preview = new DoubleMachineGunTurret(0, 0);
}
else if (id == 5 && money >= AmplifierTurret::Price) {
preview = new AmplifierTurret(0, 0);
}
}
if (!preview)
return;
preview->Position = Engine::GameEngine::GetInstance().GetMousePosition();
preview->Tint = al_map_rgba(255, 255, 255, 200);
preview->Enabled = false;
preview->Preview = true;
UIGroup->AddNewObject(preview);
OnMouseMove(Engine::GameEngine::GetInstance().GetMousePosition().x, Engine::GameEngine::GetInstance().GetMousePosition().y);
}
bool PlayScene::CheckSpaceValid(int x, int y) {
if (x < 0 || x >= MapWidth || y < 0 || y >= MapHeight)
return false;
auto map00 = mapState[y][x];
mapState[y][x] = TILE_OCCUPIED;
std::vector<std::vector<int>> map = CalculateBFSDistance();
mapState[y][x] = map00;
if (map[0][0] == -1)
return false;
for (auto& it : EnemyGroup->GetObjects()) {
Engine::Point pnt;
pnt.x = floor(it->Position.x / BlockSize);
pnt.y = floor(it->Position.y / BlockSize);
if (pnt.x < 0) pnt.x = 0;
if (pnt.x >= MapWidth) pnt.x = MapWidth - 1;
if (pnt.y < 0) pnt.y = 0;
if (pnt.y >= MapHeight) pnt.y = MapHeight - 1;
if (map[pnt.y][pnt.x] == -1)
return false;
}
// All enemy have path to exit.
mapState[y][x] = TILE_OCCUPIED;
mapDistance = map;
for (auto& it : EnemyGroup->GetObjects())
dynamic_cast<Enemy*>(it)->UpdatePath(mapDistance);
return true;
}
std::vector<std::vector<int>> PlayScene::CalculateBFSDistance() {
// Reverse BFS to find path.
std::vector<std::vector<int>> map(MapHeight, std::vector<int>(std::vector<int>(MapWidth, -1)));
std::queue<Engine::Point> que;
// Push end point.
// BFS from end point.
if (mapState[MapHeight - 1][MapWidth - 1] != TILE_DIRT)
return map;
que.push(Engine::Point(MapWidth - 1, MapHeight - 1));
map[MapHeight - 1][MapWidth - 1] = 0;
while (!que.empty()) {
Engine::Point p = que.front();
que.pop();
for (auto &c : directions) {
int x = p.x + c.x;
int y = p.y + c.y;
if (x < 0 || x >= MapWidth || y < 0 || y >= MapHeight ||
map[y][x] != -1 || mapState[y][x] != TILE_DIRT) {
continue;
} else {
map[y][x] = map[p.y][p.x] + 1;
que.push(Engine::Point(x, y));
}
}
}
return map;
}