-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
360 lines (332 loc) · 10.7 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
#include "Game.h"
#include "Map.h"
#include "Display.h"
#include "Input.h"
#include "Player.h"
#include "Enemy.h"
#include "EnemyFactory.h"
#include <sstream>
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
Game *Game::instance = 0;
Game *Game::GetInstance()
{
if (!instance)
instance = new Game();
return instance;
}
void Game::RemoveInstance()
{
cerr << "Removing game instance\n";
delete instance;
instance = 0;
}
void Game::SetInstance(Game * game)
{
if (instance)
delete instance;
instance = game;
}
Game::Game():player(0)
{
ifstream in;
in.open("valid_games.txt");
string name;
while(!in.fail())
{
getline(in, name);
if (!in.fail())
gameNames.push_back(name);
}
}
Game::~Game()
{
for (unsigned int i = 0; i < enemies.size(); i++)
delete enemies[i];
delete player;
}
Player *Game::GetPlayer()
{
return player;
}
std::vector<Enemy *> &Game::GetEnemies()
{
return enemies;
}
bool Game::GameEnd()
{
if (player->Dead())
return true;
pair<int,int> end = Map::GetInstance()->GetEndLoc();
if (end.first == player->GetX()/BOX_PIXEL_WIDTH && end.second == player->GetY()/BOX_PIXEL_WIDTH)
return true;
return false;
}
void Game::Play(std::string gameName, int diff)
{
Input *input = Input::GetInstance();
player = new Player(0,0);
string t;
for (int i = 0; i < 20; i++)
{
stringstream t2;
t2 << i+1;
t = gameName + "/Level_";
t += t2.str();
t+= ".txt";
if (player->GetHealth() > 0)
{
InitLevel(i+1,diff,t);
player->AddMoney(500*i);
PlayLevel();
ResetLevel();
}
if (input->IsClosed() || input->IsPressed(ALLEGRO_KEY_ESCAPE))
break;
}
if (player->GetHealth() > 0 && !input->IsClosed() && !input->IsPressed(ALLEGRO_KEY_ESCAPE))
cerr << "WINNER\n";
}
void Game::Create(std::string gameName, int size)
{
cerr << "Creating game \"" << gameName << "\"...\n";
if (gameName == "")
return;
if (!Valid(gameName))
gameNames.push_back(gameName);
Map *map = Map::GetInstance();
string t;
string temp2 = "mkdir \"";
system((temp2 + gameName + "\"").c_str());
for (int i = 0; i < 20; i++)
{
stringstream t2;
t2 << i+1;
t = gameName + "/Level_";
t += t2.str();
t+= ".txt";
map->CreateAuto(size);
map->Save(t);
}
ofstream out;
out.open("valid_games.txt");
string name;
for (vector<string>::iterator it = gameNames.begin(); it < gameNames.end(); it++)
{
cerr << (*it) << endl;
out << (*it) << endl;
}
}
void Game::InitLevel(int level, int difficulty, string fileName)
{
srand(time(0));
EnemyFactory enemyFactory = EnemyFactory(difficulty % NUM_ENEMY_DIFFICULTIES);
Map * mapInst = Map::GetInstance();
if (fileName == "")
mapInst->CreateAuto(GRID_SIZE);
else
mapInst->Load(fileName);
pair<int,int> startLoc = mapInst->GetStartLoc();
player->SetX(startLoc.first*BOX_PIXEL_WIDTH+BOX_PIXEL_WIDTH/2);
player->SetY(startLoc.second*BOX_PIXEL_WIDTH+BOX_PIXEL_WIDTH/2);
int numEnemies = level * (difficulty/NUM_ENEMY_DIFFICULTIES + 1) * (mapInst->GetGridSize()/5);
for (int i = 0; i < numEnemies; i++)
{
int x, y, health = (rand() % (MAX_ENEMY_HEALTH - 10)) + 10;
do
{
x = rand() % mapInst->GetGridSize();
y = rand() % mapInst->GetGridSize();
} while (mapInst->GetGrid()[x][y] == 1 || (x == startLoc.first && y == startLoc.second));
enemies.push_back(enemyFactory.Generate(health, x*BOX_PIXEL_WIDTH+BOX_PIXEL_WIDTH/2, y*BOX_PIXEL_WIDTH+BOX_PIXEL_WIDTH/2));
}
Display *display = display->GetInstance();
display->SetBackground();
display->UpdateMiniMap (player->GetX()/BOX_PIXEL_WIDTH,player->GetY()/BOX_PIXEL_WIDTH);
display->Zoom(player->GetX()/BOX_PIXEL_WIDTH,player->GetY()/BOX_PIXEL_WIDTH);
display->UpdateScreen();
}
void Game::ResetLevel()
{
for (unsigned int i = 0; i < enemies.size(); i++)
delete enemies[i];
enemies.clear();
}
void Game::PlayLevel()
{
Input * input = Input::GetInstance();
Display * display = Display::GetInstance();
display->UpdateScreen();
while (!(input->IsClosed() || input->IsPressed(ALLEGRO_KEY_ESCAPE)))
{
input->ReadInput();
while (input->Timer())
{
vector<Weapon *> playerWeapons;
vector<Weapon *> enemyWeapons;
input->ReadInput();
Map::GetInstance()->UpdateDistFromPlayer(player->GetX()/BOX_PIXEL_WIDTH,player->GetY()/BOX_PIXEL_WIDTH, 30);
if (input->GetMovement() >= 10000) // Upgrade
{
Upgrade();
while (input->IsPressed(ALLEGRO_KEY_U)) input->ReadInput();
input->Timer();
}
for (unsigned int i = 0; i < enemies.size(); i++)
{
if (enemies[i]->Dead())
{
player->AddMoney(enemies[i]->GetInitialHealth());
delete enemies[i];
enemies.erase(enemies.begin()+i);
i--;
}
else
{
enemies[i]->Move();
enemies[i]->Attack();
vector<Weapon *> enemyWeapon = enemies[i]->GetWeapons();
enemyWeapons.insert(enemyWeapons.end(),enemyWeapon.begin(), enemyWeapon.end());
}
}
player->Move();
player->Attack();
playerWeapons = player->GetWeapons();
for(vector<Weapon *>::iterator weaponIt = enemyWeapons.begin(); weaponIt != enemyWeapons.end(); ++weaponIt)
{
if ((*weaponIt)->GetProperties().GetType() != _Gun)
{
(*weaponIt)->Action(player);
if ((*weaponIt)->WillDestroy())
{
(*weaponIt)->Notify();
}
(*weaponIt)->Update();
}
else if ((*weaponIt)->WillDestroy())
{
(*weaponIt)->Notify();
(*weaponIt)->Update();
}
else
{
while (!(*weaponIt)->WillDestroy())
{
(*weaponIt)->Action(player);
if ((*weaponIt)->WillDestroy())
break;
(*weaponIt)->Update();
}
}
}
for(vector<Weapon *>::iterator weaponIt = playerWeapons.begin(); weaponIt != playerWeapons.end(); ++weaponIt)
{
if ((*weaponIt)->GetProperties().GetType() != _Gun)
{
(*weaponIt)->Action(player);
for (unsigned int i = 0; i < enemies.size(); i++)
(*weaponIt)->Action(enemies[i]);
if ((*weaponIt)->WillDestroy())
{
(*weaponIt)->Notify();
}
(*weaponIt)->Update();
}
else if ((*weaponIt)->WillDestroy())
{
(*weaponIt)->Notify();
(*weaponIt)->Update();
}
else
{
while (!(*weaponIt)->WillDestroy())
{
for (unsigned int i = 0; i < enemies.size(); i++)
(*weaponIt)->Action(enemies[i]);
if ((*weaponIt)->WillDestroy())
break;
(*weaponIt)->Update();
}
}
}
Map::GetInstance()->UpdateFog(player->GetX()/60, player->GetY()/60);
pair<int, int> mouse_pos = input->GetMouse();
if (mouse_pos.first >= SCREEN_X && mouse_pos.first < SCREEN_X+MAX_GRID_SIZE*2 && mouse_pos.second < MAX_GRID_SIZE*2 && mouse_pos.second >= 0)
display->Zoom ((mouse_pos.first-(SCREEN_X))/(2*MAX_GRID_SIZE/Map::GetInstance()->GetGridSize()),mouse_pos.second/(2*MAX_GRID_SIZE/Map::GetInstance()->GetGridSize()));
else
display->Zoom (player->GetX()/60,player->GetY()/60);
display->UpdateMiniMap (player->GetX()/60,player->GetY()/60);
display->UpdateScreen();
if (GameEnd())
break;
}
if (GameEnd())
break;
}
}
void Game::Upgrade()
{
bool finished = false;
int prevVals = 1;
while (!finished && !Input::GetInstance()->IsClosed())
{
Display::GetInstance()->DrawUpgrade();
Input::GetInstance()->ReadUpgrade();
int upgradeVals = Input::GetInstance()->GetUpgrade();
if (upgradeVals != prevVals && !Input::GetInstance()->IsClosed())
{
prevVals = upgradeVals;
switch (upgradeVals)
{
case 1:
finished = true;
break;
case 2:
player->UpgradeWeapon(_Gun, "rate");
break;
case 4:
player->UpgradeWeapon(_Gun, "clip");
break;
case 8:
player->UpgradeWeapon(_Gun, "range");
break;
case 16:
player->UpgradeWeapon(_None, "health");
break;
case 32:
player->UpgradeWeapon(_Nuke, "quantity");
break;
case 64:
player->UpgradeWeapon(_WideShot, "quantity");
break;
case 128:
player->UpgradeWeapon(_Grenade, "quantity");
break;
case 256:
player->UpgradeWeapon(_ExplodingShot, "quantity");
break;
case 512:
player->UpgradeWeapon(_Mine, "quantity");
break;
case 1024:
player->UpgradeWeapon(_WallBreaker, "quantity");
break;
default:
break;
}
}
}
}
bool Game::Valid(std::string gameName)
{
for (vector<string>::iterator it = gameNames.begin(); it < gameNames.end(); it++)
if (*it == gameName)
return true;
return false;
}
vector <string> Game::GetGameNames() const
{
return gameNames;
}