-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentWorld.cpp
More file actions
410 lines (350 loc) · 11.6 KB
/
StudentWorld.cpp
File metadata and controls
410 lines (350 loc) · 11.6 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
#include "StudentWorld.h"
#include "Actor.h"
using namespace std;
Protestor* m_Protestor;
GameWorld* createStudentWorld(string assetDir) {
return new StudentWorld(assetDir);
}
#pragma region Utilities
std::mt19937 generatorXBoulder{ std::random_device{}() };
std::uniform_int_distribution<> distributionXBoulder { 0, ICE_WIDTH - 4 };
auto get_randomXBoulder = std::bind(distributionXBoulder, generatorXBoulder);
std::mt19937 generatorYBoulder{ std::random_device{}() };
std::uniform_int_distribution<> distributionYBoulder{ 20, ICE_HEIGHT - 4 }; //changed it to 20, 56 as Boulders need to be within (0,20) - (60,56)
auto get_randomYBoulder = std::bind(distributionYBoulder, generatorYBoulder);
std::mt19937 generatorXNuggetBarrel{ std::random_device{}() };
std::uniform_int_distribution<> distributionXNuggetBarrel{ 0, ICE_WIDTH - 4 };
auto get_randomXNuggetBarrel = std::bind(distributionXNuggetBarrel, generatorXNuggetBarrel);
std::mt19937 generatorYNuggetBarrel{ std::random_device{}() };
std::uniform_int_distribution<> distributionYNuggetBarrel{ 0, ICE_HEIGHT - 4 };
auto get_randomYNuggetBarrel = std::bind(distributionYNuggetBarrel, generatorYNuggetBarrel);
std::pair<int, int> getRandomPositionBoulder() {
int x = get_randomXBoulder();
int y = get_randomYBoulder();
return std::make_pair(x, y);
}
std::pair<int, int> getRandomPositionNuggetBarrel() {
int x = get_randomXNuggetBarrel();
int y = get_randomYNuggetBarrel();
return std::make_pair(x, y);
}
#pragma endregion Utilities
#pragma region StudentWorld
StudentWorld::~StudentWorld() {
delete m_iceman;
m_iceman = nullptr;
m_oilField.cleanUp();
m_stage.cleanUp();
}
int StudentWorld::init() {
//initialize stats
m_stats.init();
//initialize actors
m_stage.init();
//construct oil field
m_oilField.init();
//allocate and insert iceman
m_iceman = new Iceman(this);
m_squirt = nullptr;
m_Protestor = new Protestor(this, m_iceman);
//HardcoreProtestor* m_HardcoreProtestor = new HardcoreProtestor();
return GWSTATUS_CONTINUE_GAME;
}
int StudentWorld::move() {
// Display Stats
setGameStatText(m_stats.toString());
// Give each actor a chance to do something
m_stage.move();
//Give player a chance to do something
m_iceman->doSomething(m_squirt);
if (m_squirt != nullptr && !m_squirt->isAlive()) {
delete m_squirt;
m_squirt = nullptr;
}
m_Protestor->doSomething();
if (m_stats.getHealth() <= 0) {
m_stats.m_lifeCount--;
m_stats.m_healthCount = 100;
return GWSTATUS_PLAYER_DIED;
} else if (m_stats.getBarrels() == 0) {
return GWSTATUS_FINISHED_LEVEL;
} else if (m_stats.getLives() == 0) {
return GWSTATUS_PLAYER_DIED;
}
return GWSTATUS_CONTINUE_GAME;
}
void StudentWorld::cleanUp() noexcept {
delete m_iceman;
m_iceman = nullptr;
m_oilField.cleanUp();
m_stage.cleanUp();
}
void StudentWorld::removeIce() noexcept {
auto iceRemoved = [this]() -> bool {
int deletedSomething = false;
int endX = max(0, min(m_iceman->getX() + 3, ICE_WIDTH - 1)); //no lower than 0, no higher than ICE_WIDTH - 1
int endY = max(0, min(m_iceman->getY() + 3, ICE_HEIGHT - 1)); //no lower than 0, no higher than ICE_HEIGHT - 1
for (int i = m_iceman->getX(); i <= endX; i++) {
for (int j = m_iceman->getY(); j <= endY; j++) {
i = max(0, min(i, ICE_WIDTH - 1)); //prevents rebounding
j = max(0, min(j, ICE_HEIGHT - 1)); //prevents rebounding
if (m_oilField.isIce(i, j)) {
deletedSomething = true;
}
m_oilField.removeIce(i, j);
}
}
return deletedSomething;
};
if (iceRemoved()) {
playSound(SOUND_DIG);
}
}
bool StudentWorld::isIce(int x, int y) const noexcept {
return m_oilField.isIce(x, y);
}
int StudentWorld::playerX() const {
return m_iceman->getX();
}
int StudentWorld::playerY() const {
return m_iceman->getY();
}
void StudentWorld::takeDamage(int dmg) {
playSound(SOUND_PROTESTER_YELL);
m_stats.m_healthCount -= dmg * 10;
}
void StudentWorld::protestorGiveUp() {
playSound(SOUND_PROTESTER_GIVE_UP);
}
void StudentWorld::createSquirt(int x, int y) noexcept {
playSound(SOUND_PLAYER_SQUIRT);
m_squirt = new Squirt(x, y);
m_stats.m_squirtCount--;
}
Squirt* StudentWorld::getSquirt() {
return m_squirt;
}
#pragma endregion StudentWorld
#pragma region GameStats
void StudentWorld::GameStats::init() noexcept {
m_boulderCount = min(m_levelCount / 2 + 2, 9);
m_goldCount = max(5 - m_levelCount / 2, 2);
m_barrelCount = min(2 + m_levelCount, 21);
m_goodieCount = m_levelCount * 25 + 300;
m_sonarKitTimer = max(100, 300 - 10 * m_levelCount);
}
string StudentWorld::GameStats::toString() const noexcept {
string lvlTxt, hlthTxt, wtrTxt, gldTxt, oilTxt, snrTxt, scrTxt;
if (m_levelCount < 10) { lvlTxt = " " + std::to_string(m_levelCount); } else { lvlTxt = std::to_string(m_levelCount); }
if (m_healthCount < 10) { hlthTxt = " " + std::to_string(m_healthCount) + "%"; } else if (m_healthCount < 100) { hlthTxt = " " + std::to_string(m_healthCount) + "%"; } else { hlthTxt = std::to_string(m_healthCount) + "%"; }
if (m_squirtCount < 10) { wtrTxt = " " + std::to_string(m_squirtCount); } else { wtrTxt = std::to_string(m_squirtCount); }
if (mm_goldCount < 10) { gldTxt = " " + std::to_string(mm_goldCount); } else { gldTxt = std::to_string(mm_goldCount); }
if (m_barrelCount < 10) { oilTxt = " " + std::to_string(m_barrelCount); } else { oilTxt = std::to_string(m_barrelCount); }
if (mm_sonarCount < 10) { snrTxt = " " + std::to_string(mm_sonarCount); } else { snrTxt = std::to_string(mm_sonarCount); }
if (m_scoreCount < 10) { scrTxt = "00000" + std::to_string(m_scoreCount); } else if (m_scoreCount < 100) { scrTxt = "0000" + std::to_string(m_scoreCount); } else if (m_scoreCount < 1000) { scrTxt = "000" + std::to_string(m_scoreCount); } else if (m_scoreCount < 10000) { scrTxt = "00" + std::to_string(m_scoreCount); } else if (m_scoreCount < 100000) { scrTxt = "0" + std::to_string(m_scoreCount); } else { scrTxt = std::to_string(m_scoreCount); }
return "Lvl: " +lvlTxt+ " Lives: " +std::to_string(m_lifeCount)+ " Hlth: "+hlthTxt+" Wtr: "+wtrTxt+" Gld: "+gldTxt+" Oil Left: "+oilTxt+" Sonar: "+snrTxt+" Scr: "+scrTxt;
}
// Getters
int StudentWorld::GameStats::getLevel() const noexcept {
return m_levelCount;
}
int StudentWorld::GameStats::getLives() const noexcept {
return m_lifeCount;
}
int StudentWorld::GameStats::getScore() const noexcept {
return m_scoreCount;
}
int StudentWorld::GameStats::getHealth() const noexcept {
return m_healthCount;
}
int StudentWorld::GameStats::getSquirts() const noexcept {
return m_squirtCount;
}
int StudentWorld::GameStats::getGold() const noexcept {
return m_goldCount;
}
int StudentWorld::GameStats::getBarrels() const noexcept {
return m_barrelCount;
}
int StudentWorld::GameStats::getSonar() const noexcept {
return m_sonarCount;
}
int StudentWorld::GameStats::getBoulders() const noexcept {
return m_boulderCount;
}
int StudentWorld::GameStats::getGoodies() const noexcept {
return m_goodieCount;
}
int StudentWorld::GameStats::getSonarKitTimer() const noexcept {
return m_sonarKitTimer;
}
#pragma endregion GameStats
#pragma region OilField
StudentWorld::OilField::~OilField() {
for (auto& i : self) {
for (auto& j : i)
delete j;
}
}
void StudentWorld::OilField::cleanUp() noexcept {
for (auto& i : self) {
for (auto& j : i) {
delete j;
j = nullptr;
}
}
}
void StudentWorld::OilField::removeIce(int x, int y) noexcept {
if ((x >= 0 && x <= ICE_WIDTH - 1) && (y >= 0 && y <= ICE_HEIGHT - 1)) {
if (self[x][y] != nullptr) {
self[x][y]->setVisible(false);
delete self[x][y];
self[x][y] = nullptr;
}
}
}
bool StudentWorld::OilField::isIce(int x, int y) const noexcept {
if ((x >= 0 && x <= ICE_WIDTH - 1) && (y >= 0 && y <= ICE_HEIGHT - 1)) {
if (self[x][y] != nullptr) {
return true;
}
}
return false;
}
void StudentWorld::OilField::init() {
for (int x = 0; x < ICE_WIDTH; x++) {
for (int y = 0; y < ICE_HEIGHT; y++) {
if ((x >= 30 && x <= 33) && (y >= 4 && y <= ICE_HEIGHT - 1)) {
continue;
}
self[x][y] = new Ice(x, y);
}
}
}
#pragma endregion OilField
#pragma region Stage
StudentWorld::Stage::~Stage() {
for (auto i : self) {
if (i != nullptr) {
delete i;
i = nullptr;
}
}
}
void StudentWorld::Stage::cleanUp() noexcept {
for (auto i : self) {
if (i != nullptr) {
delete i;
i = nullptr;
}
}
}
template <typename T>
T* StudentWorld::Stage::spawnActor() {
T* newActor = new T();
self.insert(newActor);
return newActor;
}
template <>
Boulder* StudentWorld::Stage::spawnActor<Boulder>() {
static Boulder* newBoulder = nullptr;
pair<int, int> randomPosition = getRandomPositionBoulder();
if (!m_boulderBlackList.isListed(randomPosition)) {
m_boulderBlackList.add(randomPosition);
newBoulder = new Boulder(randomPosition.first, randomPosition.second);
self.insert(newBoulder);
return newBoulder;
}
spawnActor<Boulder>();
return newBoulder;
}
template <>
OilBarrel* StudentWorld::Stage::spawnActor<OilBarrel>() {
static OilBarrel* newOilBarrel = nullptr;
pair<int, int> randomPosition = getRandomPositionNuggetBarrel();
newOilBarrel = new OilBarrel(randomPosition.first, randomPosition.second, m_studentWorldPointer);
self.insert(newOilBarrel);
return newOilBarrel;
//FIX ME
//not sure how to seperate barrels by 6 blocks
}
template <>
GoldNugget* StudentWorld::Stage::spawnActor<GoldNugget>() {
static GoldNugget* newGoldNugget = nullptr;
pair<int, int> randomPosition = getRandomPositionNuggetBarrel();
newGoldNugget = new GoldNugget(randomPosition.first, randomPosition.second, m_studentWorldPointer);
self.insert(newGoldNugget);
return newGoldNugget;
}
template <>
SonarKit* StudentWorld::Stage::spawnActor<SonarKit>() {
static SonarKit* newSonarKit = new SonarKit(m_studentWorldPointer, m_studentWorldPointer->m_stats.getSonarKitTimer());
self.insert(newSonarKit);
return newSonarKit;
}
template <>
WaterPool* StudentWorld::Stage::spawnActor<WaterPool>() {
static WaterPool* newWaterPool = new WaterPool(1, 1, m_studentWorldPointer);
self.insert(newWaterPool);
return newWaterPool;
}
void StudentWorld::Stage::removeActor(Actor* actor) noexcept {
self.erase(actor);
}
void StudentWorld::Stage::init() {
for (int i = 0; i < m_studentWorldPointer->m_stats.getBoulders(); i++) {
spawnActor<Boulder>();
}
for (int spawnBarrel = 0; spawnBarrel < m_studentWorldPointer->m_stats.getBarrels(); spawnBarrel++) {
spawnActor<OilBarrel>();
}
for (int spawnGold = 0; spawnGold < m_studentWorldPointer->m_stats.getGold(); spawnGold++) {
spawnActor<GoldNugget>();
}
//spawnActor<SonarKit>();
}
/*
void StudentWorld::Stage::init(OilField myField) {
auto m_oilField = myField.getField();
const int numBoulders = 6;
std::array<Boulder*, numBoulders> m_boulderField;
Ice* testIce = new Ice(0, 0);
int temp = numBoulders;
while (temp > 0) {
int rand_x = rand() % 60;
int rand_y = rand() % 36 + 20;
m_oilField[rand_x][rand_y] = testIce;
m_oilField[rand_x+1][rand_y] = testIce;
m_oilField[rand_x+2][rand_y] = testIce;
m_oilField[rand_x+3][rand_y] = testIce;
m_boulderField[numBoulders-temp] = new Boulder(rand_x, rand_y);
temp--;
}
delete testIce;
}
*/
void StudentWorld::Stage::move() {
//if (rand() % m_studentWorldPointer->m_stats.getGoodies() == 0) {
// if (rand() % 5 == 0) {
// spawnActor<SonarKit>();
// }
// else {
// spawnActor<WaterPool>();
// }
//}
//spawning SonarKit in move causes error, whereas spawning in init does not when deleting it
auto it = self.begin();
while (it != self.end()) {
if (*it != nullptr) {
(*it)->doSomething();
if (!((*it)->isAlive())) {
m_studentWorldPointer->playSound((*it)->getDeathSound());
delete (*it);
it = self.erase(it);
continue;
}
}
it++;
}
}
#pragma endregion Stage