/
StudentWorld.cpp
563 lines (516 loc) · 14.6 KB
/
StudentWorld.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
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
#include "StudentWorld.h"
#include "GameConstants.h"
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
// TODO: is_____BlockedAt requires a common function
double euclidianDistance(Actor* A, Actor* B)
{
// Note that distance between centers is equal to distance between corners
double deltaX = (A->getX()) - (B->getX());
double deltaY = (A->getY()) - (B->getY());
return ((deltaX)*(deltaX)) + ((deltaY)*(deltaY));
}
double euclidianDistance(double x, double y, Actor* B)
{
// Note that distance between centers is equal to distance between corners
double deltaX = (x) - (B->getX());
double deltaY = (y) - (B->getY());
return ((deltaX)*(deltaX)) + ((deltaY)*(deltaY));
}
GameWorld* createStudentWorld(string assetPath)
{
return new StudentWorld(assetPath);
}
StudentWorld::StudentWorld(string assetPath)
: GameWorld(assetPath)
{
m_pen = nullptr;
}
StudentWorld::~StudentWorld()
{
cleanUp();
}
int StudentWorld::init()
{
m_numCitizens = 0;
m_canExit = false;
m_levelFinished = false;
Level lev(assetPath());
ostringstream oss;
if (getLevel() == 100)
return GWSTATUS_PLAYER_WON;
oss << "level0" << getLevel() << ".txt";
string levelFile = oss.str();
Level::LoadResult result = lev.loadLevel(levelFile);
if (result == Level::load_fail_file_not_found)
cerr << "Cannot find " << "level0" << getLevel() << ".txt data file" << endl;
else if (result == Level::load_fail_bad_format)
{
cerr << "Your level was improperly formatted" << endl;
return GWSTATUS_LEVEL_ERROR;
}
else if (result == Level::load_success)
{
cerr << "Successfully loaded " << "level0" << getLevel() << ".txt" << endl;
int cnt = 0;
for (int level_x = 0; level_x < SPRITE_WIDTH; level_x++)
{
for (int level_y = 0; level_y < SPRITE_HEIGHT; level_y++)
{
Level::MazeEntry ge = lev.getContentsOf(level_x, level_y);
double x = level_x * SPRITE_WIDTH;
double y = level_y * SPRITE_HEIGHT;
switch (ge)
{
case Level::empty:
break;
case Level::citizen:
cnt++;
m_numCitizens++;
addActor(new Citizen(this, x, y));
break;
case Level::vaccine_goodie:
cnt++;
addActor(new VaccineGoodie(this, x, y));
break;
case Level::gas_can_goodie:
cnt++;
addActor(new GasCanGoodie(this, x, y));
break;
case Level::landmine_goodie:
cnt++;
addActor(new LandmineGoodie(this, x, y));
break;
case Level::smart_zombie:
cnt++;
addActor(new SmartZombie(this, x, y));
break;
case Level::dumb_zombie:
cnt++;
addActor(new DumbZombie(this, x, y));
break;
case Level::player:
m_pen = new Penelope(this, x, y);
break;
case Level::exit:
cnt++;
addActor(new Exit(this, x, y));
break;
case Level::wall:
cnt++;
addActor(new Wall(this, x, y));
break;
case Level::pit:
cnt++;
addActor(new Pit(this, x, y));
break;
}
}
}
recordLevelFinishedIfAllCitizensGone();
return GWSTATUS_CONTINUE_GAME;
}
return GWSTATUS_PLAYER_WON;
}
void StudentWorld::addActor(Actor * a)
{
actors.push_back(a);
}
int StudentWorld::move()
{
m_pen->doSomething();
list<Actor*>::iterator p = actors.begin();
while (p != actors.end())
{
// Delete actors from the vector if they are dead
// or ask them to do something if they are alive
// This is slightly different from the suggestion in the spec.
// I didn't feel that two loops were necessary.
if ((*p)->isDead())
{
delete *p;
list<Actor*>::iterator q = actors.erase(p);
p = q;
}
else
{
(*p)->doSomething();
p++;
}
}
// Check if Penelope died or won the game
if (m_pen->isDead())
{
decLives();
return GWSTATUS_PLAYER_DIED;
}
else if (getLevelFinished())
return GWSTATUS_FINISHED_LEVEL;
// Set Status Text
ostringstream oss;
oss.fill('0');
oss << "Score: " << setw(5) << getScore();
oss << " Level: " << getLevel();
oss << " Lives : " << getLives();
oss << " Vaccines : " << m_pen->getNumVaccines();
oss << " Flames : " << m_pen->getNumFlameCharges();
oss << " Mines : " << m_pen->getNumLandmines();
oss << " Infected : " << m_pen->getInfectionDuration();
setGameStatText(oss.str());
return GWSTATUS_CONTINUE_GAME;
}
void StudentWorld::cleanUp()
{
if (m_pen != nullptr)
{
delete m_pen;
m_pen = nullptr;
}
int cnt = 0;
list<Actor*>::iterator p = actors.begin();
while (p != actors.end())
{
cnt++;
if (*p != nullptr)
delete *p;
list<Actor*>::iterator q = actors.erase(p);
p = q;
}
}
bool StudentWorld::isAgentMovementBlockedAt(Actor* curActor, double x, double y)
{
// Create bounding box for destination
double dest_x_start = x;
double dest_x_end = (x + SPRITE_WIDTH - 1);
double dest_y_start = y;
double dest_y_end = (y + SPRITE_HEIGHT - 1);
// Check if the destination x and y coordinates are in Penelope's bounding box
if (curActor != m_pen)
{
// Create bounding box for current actor
double pen_x_start = m_pen->getX();
double pen_x_end = (m_pen->getX() + SPRITE_WIDTH - 1);
double pen_y_start = m_pen->getY();
double pen_y_end = (m_pen->getY() + SPRITE_HEIGHT - 1);
// Check if the bouding boxes overlap
if ((pen_x_start <= dest_x_start && dest_x_start <= pen_x_end &&
pen_y_start <= dest_y_start && dest_y_start <= pen_y_end)
||
(pen_x_start <= dest_x_end && dest_x_end <= pen_x_end &&
pen_y_start <= dest_y_end && dest_y_end <= pen_y_end)
||
(pen_x_start <= dest_x_start && dest_x_start <= pen_x_end &&
pen_y_start <= dest_y_end && dest_y_end <= pen_y_end)
||
(pen_x_start <= dest_x_end && dest_x_end <= pen_x_end &&
pen_y_start <= dest_y_start && dest_y_start <= pen_y_end)
)
{
return true;
}
}
// Check if the destination x and y coordinates are in any other actor's bounding box
for (list<Actor*>::iterator p = actors.begin();
p != actors.end(); p++)
{
if (*p == curActor)
continue;
// Actors cannot walk through solid objects
if ((*p)->blocksMovement())
{
// Create bounding box for current actor
double actor_x_start = (*p)->getX();
double actor_x_end = ((*p)->getX() + SPRITE_WIDTH - 1);
double actor_y_start = (*p)->getY();
double actor_y_end = ((*p)->getY() + SPRITE_HEIGHT - 1);
// Check if the bouding boxes overlap
if ((actor_x_start <= dest_x_start && dest_x_start <= actor_x_end &&
actor_y_start <= dest_y_start && dest_y_start <= actor_y_end)
||
(actor_x_start <= dest_x_end && dest_x_end <= actor_x_end &&
actor_y_start <= dest_y_end && dest_y_end <= actor_y_end)
||
(actor_x_start <= dest_x_start && dest_x_start <= actor_x_end &&
actor_y_start <= dest_y_end && dest_y_end <= actor_y_end)
||
(actor_x_start <= dest_x_end && dest_x_end <= actor_x_end &&
actor_y_start <= dest_y_start && dest_y_start <= actor_y_end)
)
{
return true;
}
}
}
return false;
}
bool StudentWorld::isFlameBlockedAt(Actor* curActor, double x, double y)
{
// Create bounding box for destination
double dest_x_start = x;
double dest_x_end = (x + SPRITE_WIDTH - 1);
double dest_y_start = y;
double dest_y_end = (y + SPRITE_HEIGHT - 1);
// Check if the destination x and y coordinates are in any other actor's bounding box
for (list<Actor*>::iterator p = actors.begin();
p != actors.end(); p++)
{
if (*p == curActor)
continue;
// Flames cannot go through certain objects
if ((*p)->blocksFlame())
{
// Create bounding box for current actor
double actor_x_start = (*p)->getX();
double actor_x_end = ((*p)->getX() + SPRITE_WIDTH - 1);
double actor_y_start = (*p)->getY();
double actor_y_end = ((*p)->getY() + SPRITE_HEIGHT - 1);
// Check if the bouding boxes overlap
if ((actor_x_start <= dest_x_start && dest_x_start <= actor_x_end &&
actor_y_start <= dest_y_start && dest_y_start <= actor_y_end)
||
(actor_x_start <= dest_x_end && dest_x_end <= actor_x_end &&
actor_y_start <= dest_y_end && dest_y_end <= actor_y_end)
||
(actor_x_start <= dest_x_start && dest_x_start <= actor_x_end &&
actor_y_start <= dest_y_end && dest_y_end <= actor_y_end)
||
(actor_x_start <= dest_x_end && dest_x_end <= actor_x_end &&
actor_y_start <= dest_y_start && dest_y_start <= actor_y_end)
)
{
return true;
}
}
}
return false;
}
bool StudentWorld::isVaccineBlockedAt(Actor* curActor, double x, double y)
{
// Create bounding box for destination
double dest_x_start = x;
double dest_x_end = (x + SPRITE_WIDTH - 1);
double dest_y_start = y;
double dest_y_end = (y + SPRITE_HEIGHT - 1);
// Check if the destination x and y coordinates are in Penelope's bounding box
if (curActor != m_pen)
{
// Create bounding box for current actor
double pen_x_start = m_pen->getX();
double pen_x_end = (m_pen->getX() + SPRITE_WIDTH - 1);
double pen_y_start = m_pen->getY();
double pen_y_end = (m_pen->getY() + SPRITE_HEIGHT - 1);
// Check if the bouding boxes overlap
if ((pen_x_start <= dest_x_start && dest_x_start <= pen_x_end &&
pen_y_start <= dest_y_start && dest_y_start <= pen_y_end)
||
(pen_x_start <= dest_x_end && dest_x_end <= pen_x_end &&
pen_y_start <= dest_y_end && dest_y_end <= pen_y_end)
||
(pen_x_start <= dest_x_start && dest_x_start <= pen_x_end &&
pen_y_start <= dest_y_end && dest_y_end <= pen_y_end)
||
(pen_x_start <= dest_x_end && dest_x_end <= pen_x_end &&
pen_y_start <= dest_y_start && dest_y_start <= pen_y_end)
)
{
return true;
}
}
// Check if the destination x and y coordinates are in any other actor's bounding box
for (list<Actor*>::iterator p = actors.begin();
p != actors.end(); p++)
{
if (*p == curActor)
continue;
// Vaccines are blocked by all other actors
// Create bounding box for current actor
double actor_x_start = (*p)->getX();
double actor_x_end = ((*p)->getX() + SPRITE_WIDTH - 1);
double actor_y_start = (*p)->getY();
double actor_y_end = ((*p)->getY() + SPRITE_HEIGHT - 1);
// Check if the bouding boxes overlap
if ((actor_x_start <= dest_x_start && dest_x_start <= actor_x_end &&
actor_y_start <= dest_y_start && dest_y_start <= actor_y_end)
||
(actor_x_start <= dest_x_end && dest_x_end <= actor_x_end &&
actor_y_start <= dest_y_end && dest_y_end <= actor_y_end)
||
(actor_x_start <= dest_x_start && dest_x_start <= actor_x_end &&
actor_y_start <= dest_y_end && dest_y_end <= actor_y_end)
||
(actor_x_start <= dest_x_end && dest_x_end <= actor_x_end &&
actor_y_start <= dest_y_start && dest_y_start <= actor_y_end)
)
{
return true;
}
}
return false;
}
bool StudentWorld::isZombieVomitTriggerAt(Actor* curActor, double x, double y)
{
// Check if the distance between Penelope and the x and y coordinates
// are less than a Euclidian distance of 100 away
if (euclidianDistance(x, y, m_pen) <= 100)
{
return true;
}
// Check if the distance between the current actor and the x and y coordinates
// are less than a Euclidian distance of 100 away
for (list<Actor*>::iterator p = actors.begin();
p != actors.end(); p++)
{
if ((*p)->triggersZombieVomit() && euclidianDistance(x, y, (*p)) <= 100)
{
return true;
}
}
return false;
}
void StudentWorld::activateOnAppropriateActors(Actor* a)
{
if (objectOverlap(m_pen, a))
{
a->activateIfAppropriate(m_pen);
}
for (list<Actor*>::iterator p = actors.begin();
p != actors.end(); p++)
{
if (objectOverlap(*p, a))
{
a->activateIfAppropriate(*p);
}
}
}
bool StudentWorld::locateNearestVomitTrigger(Actor* curActor, double& otherX, double& otherY, double& distance)
{
// Select closest person
// Assume Penelope is the closest
if (!m_pen->isDead())
{
double minDistance = euclidianDistance(curActor, m_pen);
Actor* closestActor = m_pen;
// Check if other actors are closer
for (list<Actor*>::iterator p = actors.begin();
p != actors.end(); p++)
{
// The zombie doesn't pick itself
// Check if the character can be infected and is alive
if ((*p)->triggersZombieVomit() && !((*p)->isDead()))
{
double tempDistance = euclidianDistance(curActor, *p);
if (tempDistance < minDistance)
{
minDistance = tempDistance;
closestActor = *p;
}
}
}
otherX = closestActor->getX();
otherY = closestActor->getY();
distance = minDistance;
return true;
}
else
// If Penelope is dead, the level ends and this function will not be called
return false;
}
bool StudentWorld::locateNearestCitizenTrigger(Actor* curActor, double& otherX, double& otherY, double& distance, bool& isThreat)
{
// Select closest person
// Assume Penelope is the closest
if (!(m_pen->isDead()))
{
double minDistance = euclidianDistance(curActor, m_pen);
Actor* closestActor = m_pen;
// Check if other actors are closer
for (list<Actor*>::iterator p = actors.begin();
p != actors.end(); p++)
{
// Check if the character triggers citizens and is alive
if ((*p)->triggersCitizens() && !((*p)->isDead()))
{
double tempDistance = euclidianDistance(curActor, (*p));
if (tempDistance < minDistance)
{
minDistance = tempDistance;
closestActor = *p;
}
}
}
otherX = closestActor->getX();
otherY = closestActor->getY();
distance = minDistance;
isThreat = closestActor->threatensCitizens();
return true;
}
else
// If Penelope is dead, the level ends and this function will not be called
return false;
}
bool StudentWorld::locateNearestCitizenThreat(double x, double y, double& otherX, double& otherY, double& distance)
{
// Select closest person
double minDistance;
Actor* closestActor = nullptr;
// Check if other actors are closer
for (list<Actor*>::iterator p = actors.begin();
p != actors.end(); p++)
{
// Check if the character triggers citizens and is alive
if (closestActor == nullptr && (*p)->threatensCitizens() && !((*p)->isDead()))
{
closestActor = *p;
minDistance = euclidianDistance(x, y, *p);
}
else if ((*p)->threatensCitizens() && !((*p)->isDead()))
{
double tempDistance = euclidianDistance(x, y, *p);
if (tempDistance < minDistance)
{
minDistance = tempDistance;
closestActor = *p;
}
}
}
if (closestActor != nullptr)
{
otherX = closestActor->getX();
otherY = closestActor->getY();
distance = minDistance;
return true;
}
else
return false;
}
bool StudentWorld::objectOverlap(Actor* A, Actor* B)
{
if (euclidianDistance(A, B) <= 100)
return true;
else
return false;
}
void StudentWorld::setLevelFinished()
{
m_levelFinished = true;
}
bool StudentWorld::getLevelFinished()
{
return m_levelFinished;
}
void StudentWorld::recordLevelFinishedIfAllCitizensGone()
{
if (m_numCitizens <= 0)
m_canExit = true;
}
void StudentWorld::recordCitizenGone()
{
m_numCitizens--;
recordLevelFinishedIfAllCitizensGone();
}
bool StudentWorld::canExit()
{
return m_canExit;
}