This repository has been archived by the owner on Mar 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameScene.cpp
184 lines (137 loc) · 3.47 KB
/
GameScene.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
#include "GameScene.h"
#include "BuildEnemy.h"
#include "Music.h"
GameScene::GameScene(Stage* stage)
: stage(stage)
{
Path path;
engine->startTimers();
player = new Player(path.PLAYER, 10, 10, 16, 2, CONTROL_SOURCE::ARROW_CONTROL);
heart = new Heart(path.OBJECT_HEART);
if (engine->difficultyLevel == DifficultyLevel::EASY)
this->HOW_MUTCH_KILL = 10;
else if (engine->difficultyLevel == DifficultyLevel::MEDIUM)
this->HOW_MUTCH_KILL = 20;
else
this->HOW_MUTCH_KILL = 25;
enemies.push_back(BuildEnemy::getEnemy(player));
}
GameScene::~GameScene()
{
engine->stopTimers();
resetCamera();
delete player;
delete heart;
for (size_t i = 0; i < enemies.size(); i++) {
delete enemies[i];
}
enemies.clear();
enemies.shrink_to_fit();
cout << "DESTRUKTOR Z GAME_SCENE" << endl;
}
void GameScene::showWindow()
{
Path path;
Music* music = new Music(path.MUSIC_GAME, ChooseMusic::MUSIC);
music->playMusic();
bool move = false;
while (!this->done) {
ALLEGRO_EVENT events;
al_wait_for_event(engine->event_queue, &events);
if (events.type == ALLEGRO_EVENT_TIMER) {
cameraTransform(player);
player->move(events, this->backgroundXPosition, this->backgroundWidth, this->backgroundHeight);
player->getHealthBar()->setXposition(this->cameraPosition);
heart->calculateCoordinates(cameraPosition, screen_width, screen_height);
for (size_t i = 0; i < enemies.size(); i++) {
enemies[i]->move(events, this->backgroundXPosition, this->backgroundWidth, this->backgroundHeight);
enemies[i]->makeShot(events);
}
if (events.timer.source == engine->timmerVector[3]) {
randEnemy();
}
move = true;
}
if (events.type == ALLEGRO_EVENT_KEY_DOWN) {
switch (events.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done = true;
default:
player->makeShot(events);
}
}
if (move == true) {
move = false;
drawBackground(player);
heart->show();
player->show();
player->checkHit(enemies);//When players get hits by enemies
for (size_t i = 0; i < enemies.size(); i++) {
enemies[i]->show();
enemies[i]->checkHit(player);//When enemies get hits by player
}
heart->checkHit(player);//To check that player touch the heart
if (player->checkHp()) {//When player is dead
done = true;
}
allocateMemory();//To delete death enemies
al_flip_display();
}
}
delete music;
resetCamera();
if (winGame) {
this->stage->showWin();
}else
this->stage->showLose();
}
void GameScene::allocateMemory()
{
vector<Hero *>::iterator it;
for (it = enemies.begin(); it != enemies.end(); it++) {
if ((*it)->checkHp()) {
if (*it != NULL) {
Hero* temp = *it;
enemies.erase(it);
delete temp;
temp = NULL;
countKilledEnemies++;
break;
}
}
}
if (this->checkEnd) {
if (enemyEnd != NULL) {
if (enemyEnd->checkHp()) {
enemyEnd = NULL;
this->winGame = true;
this->done = true;
}
}
}
}
void GameScene::randEnemy()
{
if (this->countKilledEnemies == HOW_MUTCH_KILL) {
this->enemyEnd = BuildEnemy::getBoss(player);
enemies.push_back(enemyEnd);
this->checkEnd = true;
countKilledEnemies = 99;
}
else if(this->countKilledEnemies <= HOW_MUTCH_KILL){
srand(time(NULL));
if (rand() % (int)Engine::difficultyLevel == 1) {
Enemy* temp = BuildEnemy::getEnemy(player);
do {
if (temp) {
enemies.push_back(temp);
break;
}
else {
Enemy* temp = BuildEnemy::getEnemy(player);
}
} while (temp == NULL);
}
}
}