-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActor.cpp
More file actions
605 lines (547 loc) · 14.9 KB
/
Actor.cpp
File metadata and controls
605 lines (547 loc) · 14.9 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
#include "Actor.h"
#include "StudentWorld.h"
//Actor Definitions
Actor::Actor(int imageID, double startX, double startY, int HP, StudentWorld* world)
: GraphObject(imageID, startX, startY), m_World(world), m_HP(HP) {}
void Actor::spaceInFrontofActor(int dir, int& x, int& y) {
switch (dir) {
case up:
y++;
break;
case left:
x--;
break;
case down:
y--;
break;
case right:
x++;
break;
default:
break;
}
return;
}
//Agent Definitions
Agent::Agent(int imageID, double startX, double startY, StudentWorld* world, int HP)
: Actor(imageID, startX, startY, HP, world) {}
bool Agent::moveIfPossible() {
int x, y;
switch (getDirection()) {
case up:
x = int(getX());
y = int(getY() + 1);
break;
case left:
x = int(getX() - 1);
y = int(getY());
break;
case down:
x = int(getX());
y = int(getY() - 1);
break;
case right:
x = int(getX() + 1);
y = int(getY());
break;
default:
return false;
}
for (int i = 0; i < getWorld()->getContainerSize(); i++) {
if (x == getWorld()->getContainer(i)->getX() &&
y == getWorld()->getContainer(i)->getY()) {
if (!getWorld()->getContainer(i)->allowsAgentColocation()) {
if (getWorld()->getContainer(i)->bePushedBy(this, x, y)) {
getWorld()->getContainer(i)->bePushedBy(this, x, y);
return true;
}
else {
return false;
}
}
}
}
return true;
}
//Avatar Definitions
Avatar::Avatar(double startX, double startY, StudentWorld* world)
: Agent(IID_PLAYER, startX, startY, world, 20) {
p = nullptr;
}
void Avatar::doSomething() {
if (!isAlive()) {
return;
}
int ch;
int curX, curY;
if (getWorld()->getKey(ch)) {
switch (ch) {
case KEY_PRESS_UP:
setDirection(up);
if (moveIfPossible()) {
moveTo(getX(), getY() + 1);
}
break;
case KEY_PRESS_LEFT:
setDirection(left);
if (moveIfPossible()) {
moveTo(getX() - 1, getY());
}
break;
case KEY_PRESS_DOWN:
setDirection(down);
if (moveIfPossible()) {
moveTo(getX(), getY() - 1);
}
break;
case KEY_PRESS_RIGHT:
setDirection(right);
if (moveIfPossible()) {
moveTo(getX() + 1, getY());
}
break;
case KEY_PRESS_SPACE:
if (m_ammo > 0) {
curX = int(getX()), curY = int(getY());
spaceInFrontofActor(getDirection(), curX, curY);
getWorld()->playSound(shootingSound());
p = new Pea(getWorld(), curX, curY, getDirection());
getWorld()->addActor(p);
p->setDirection(getDirection());
m_ammo--;
}
break;
case KEY_PRESS_ESCAPE:
getWorld()->decLives();
break;
default:
break;
}
}
}
//Robot Definitions
Robot::Robot(StudentWorld* world, int startX, int startY, int imageID,
int hitPoints, int score, int startDir)
: Agent(imageID, startX, startY, world, hitPoints), robot_score(score),
m_tick(1), robot_p(nullptr) {
setDirection(startDir);
}
void Robot::damage(int damageAmt) {
setHitPoints(getHP() - damageAmt);
if (!isAlive()) {
setDead();
getWorld()->playSound(SOUND_ROBOT_DIE);
getWorld()->increaseScore(getRobotScore());
}
}
bool Robot::actionTick() {
int ticks = (28 - getWorld()->getLevel()) / 4;
if (ticks < 3) {
ticks = 3;
}
if (m_tick == ticks) {
m_tick = 1;
return true;
}
m_tick++;
return false;
}
void Robot::doSomething() {
if (!isAlive()) {
return;
}
if (!actionTick()) { return; }
doDiffRobotSomething();
}
bool Robot::peaCanFire() {
int avatar_x = int(getWorld()->getAvatar()->getX()), avatar_y = int(getWorld()->getAvatar()->getY());
int robot_x = int(getX()), robot_y = int(getY());
switch (getDirection()) {
case up:
if (avatar_x == robot_x && avatar_y > robot_y) {
for (int i = 0; i < getWorld()->getContainerSize(); i++) {
int actor_x = int(getWorld()->getContainer(i)->getX()), actor_y = int(getWorld()->getContainer(i)->getX());
if (robot_y <= actor_y && avatar_y >= actor_y &&
getWorld()->getContainer(i)->stopsPea()) {
return false;
}
}
return true;
}
break;
case down:
if (avatar_x == robot_x && avatar_y < robot_y) {
for (int i = 0; i < getWorld()->getContainerSize(); i++) {
int actor_x = int(getWorld()->getContainer(i)->getX()), actor_y = int(getWorld()->getContainer(i)->getX());
if (robot_y >= actor_y && avatar_y <= actor_y &&
getWorld()->getContainer(i)->stopsPea()) {
return false;
}
}
return true;
}
break;
case left:
if (avatar_y == robot_y && avatar_x < robot_x) {
for (int i = 0; i < getWorld()->getContainerSize(); i++) {
int actor_x = int(getWorld()->getContainer(i)->getX()), actor_y = int(getWorld()->getContainer(i)->getX());
if (robot_x > actor_x && avatar_x < actor_x &&
getWorld()->getContainer(i)->stopsPea()) {
return false;
}
}
return true;
}
break;
case right:
if (avatar_y == robot_y && avatar_x > robot_x) {
for (int i = 0; i < getWorld()->getContainerSize(); i++) {
int actor_x = int(getWorld()->getContainer(i)->getX()), actor_y = int(getWorld()->getContainer(i)->getX());
if (robot_x < actor_x && avatar_x > actor_x &&
getWorld()->getContainer(i)->stopsPea()) {
return false;
}
}
return true;
}
break;
default:
break;;
}
return false;
}
void Robot::firePea() {
int curX, curY;
curX = int(getX()), curY = int(getY());
spaceInFrontofActor(getDirection(), curX, curY);
getWorld()->playSound(shootingSound());
robot_p = new Pea(getWorld(), curX, curY, getDirection());
getWorld()->addActor(robot_p);
robot_p->setDirection(getDirection());
}
//RageBot Definitions
RageBot::RageBot(StudentWorld* world, int startX, int startY, int startDir)
: Robot(world, startX, startY, IID_RAGEBOT, 10, 100, startDir) {}
void RageBot::moveRageBot() {
if (moveIfPossible()) {
int curX, curY;
curX = int(getX()), curY = int(getY());
spaceInFrontofActor(getDirection(), curX, curY);
moveTo(curX, curY);
}
else {
switch (getDirection()) {
case up:
setDirection(down);
break;
case down:
setDirection(up);
break;
case left:
setDirection(right);
break;
case right:
setDirection(left);
break;
default:
break;
}
}
}
void RageBot::doDiffRobotSomething() {
if (peaCanFire()) {
firePea();
}
else {
moveRageBot();
}
}
//VerticalRageBot Definitions
VerticalRageBot::VerticalRageBot(StudentWorld* world, int startX, int startY)
: RageBot(world, startX, startY, up) {}
//HorizontalRageBot Definitions
HorizontalRageBot::HorizontalRageBot(StudentWorld* world, int startX, int startY)
: RageBot(world, startX, startY, left) {}
//TheifBot Definition
ThiefBot::ThiefBot(StudentWorld* world, int startX, int startY, int imageID, int hitPoints, int score)
: Robot(world, startX, startY, imageID, hitPoints, score, right),
distanceBeforeTurning(rand() % 5 + 1), curDistance(0), goodieP(nullptr) {}
void ThiefBot::doDiffRobotSomething() {
//Shoot if meanthiefbot
doThiefBotSomething();
//if on a Pickupable Item
for (int i = 0; i < getWorld()->getContainerSize(); i++) {
if (getX() == getWorld()->getContainer(i)->getX() &&
getY() == getWorld()->getContainer(i)->getY() &&
getWorld()->getContainer(i)->isStealable()) {
//pick up item (remove from board, play munching sound)
goodieP = getWorld()->getContainer(i);
goodieP->setVisible(false);
getWorld()->playSound(SOUND_ROBOT_MUNCH);
return;
}
}
//else if distanceWithoutTurning is not met and no obstruction in next square
if (curDistance < distanceBeforeTurning && moveIfPossible()) {
//move to next square
int nextX = int(getX()), nextY = int(getY());
spaceInFrontofActor(getDirection(), nextX, nextY);
moveTo(nextX, nextY);
distanceBeforeTurning++;
}
else {
//set a new distanceWithoutTurning
distanceBeforeTurning = 0;
bool dirArr[4] = { false, false, false, false };
//pick a new random direction d
switch (rand() % 3 + 1) {
case 1:
setDirection(up);
dirArr[0] = true;
break;
case 2:
setDirection(left);
dirArr[1] = true;
break;
case 3:
setDirection(down);
dirArr[2] = true;
break;
case 4:
setDirection(right);
dirArr[3] = true;
break;
default:
break;
}
while ((dirArr[0] == false || dirArr[1] == false
|| dirArr[2] == false || dirArr[3] == false)) {
if (moveIfPossible()) {
//move to next square
int nextX = int(getX()), nextY = int(getY());
spaceInFrontofActor(getDirection(), nextX, nextY);
moveTo(nextX, nextY);
distanceBeforeTurning++;
}
switch (getDirection()) {
case up:
setDirection(left);
dirArr[1] = true;
break;
case left:
setDirection(down);
dirArr[2] = true;
break;
case down:
setDirection(right);
dirArr[3] = true;
break;
case right:
setDirection(up);
dirArr[0] = true;
break;
default:
break;
}
}
}
}
void ThiefBot::damage(int damageAmt) {
setHitPoints(getHP() - damageAmt);
if (!isAlive()) {
goodieP->moveTo(getX(), getY());
goodieP->setVisible(true);
setDead();
getWorld()->playSound(SOUND_ROBOT_DIE);
getWorld()->increaseScore(getRobotScore());
}
}
//RegularThiefBot Definition
RegularThiefBot::RegularThiefBot(StudentWorld* world, int startX, int startY)
:ThiefBot(world, startX, startY, IID_THIEFBOT, 5, 10) {}
//MeanThiefBot Definition
MeanThiefBot::MeanThiefBot(StudentWorld* world, int startX, int startY)
: ThiefBot(world, startX, startY, IID_MEAN_THIEFBOT, 8, 20) {}
void MeanThiefBot::doThiefBotSomething() {
if (peaCanFire()) {
firePea();
}
}
//ThiefBotFactory Definitions
ThiefBotFactory::ThiefBotFactory(StudentWorld* world, int startX, int startY, ProductType type)
: Actor(IID_ROBOT_FACTORY, startX, startY, 20, world),
m_Type(type), m_ThiefBot(nullptr) {}
void ThiefBotFactory::doSomething() {
int size = getWorld()->getContainerSize();
int countBots = 0;
for (int i = 0; i < getWorld()->getContainerSize(); i++) {
if (getWorld()->getContainer(i)->isThiefBot() &&
(getX() - 3) <= getWorld()->getContainer(i)->getX() &&
(getX() + 3) >= getWorld()->getContainer(i)->getX() &&
(getY() - 3) <= getWorld()->getContainer(i)->getY() &&
(getY() + 3) >= getWorld()->getContainer(i)->getY()){
if (getX() == getWorld()->getContainer(i)->getX() &&
getY() == getWorld()->getContainer(i)->getY()) {
return;
}
countBots++;
}
}
if (countBots < 3 && (rand() % 50 + 1) == 25) {
//Add 1:50 chance spawn thiefbot
switch (m_Type) {
case REGULAR:
m_ThiefBot = new RegularThiefBot(getWorld(), int(getX()), int(getY()));
getWorld()->addActor(m_ThiefBot);
break;
case MEAN:
m_ThiefBot = new MeanThiefBot(getWorld(), int(getX()), int(getY()));
getWorld()->addActor(m_ThiefBot);
break;
default:
break;
}
}
}
//Exit Definitions
Exit::Exit(StudentWorld* world, int startX, int startY)
:Actor(IID_EXIT, startX, startY, 20, world) {
setVisible(false);
}
void Exit::doSomething() {
for (int i = 0; i < getWorld()->getContainerSize(); i++) {
if (getWorld()->getContainer(i)->isCrystal()) {
return;
}
}
if (isVisible() == false) {
setVisible(true);
getWorld()->playSound(SOUND_REVEAL_EXIT);
}
if (getX() == getWorld()->getAvatar()->getX() &&
getY() == getWorld()->getAvatar()->getY() &&
isVisible()) {
getWorld()->playSound(SOUND_FINISHED_LEVEL);
getWorld()->increaseScore(2000 + getWorld()->getCurrentLevelBonus());
getWorld()->advanceToNextLevel();
getWorld()->setLevelCompleted(true);
}
}
//Wall Definitions
Wall::Wall(double startX, double startY, StudentWorld* world)
: Actor(IID_WALL, startX, startY, 20, world) {}
//Pea Definitions
Pea::Pea (StudentWorld* world, int startX, int startY, int startDir)
: Actor(IID_PEA, startX, startY, 20, world) {
setDirection(right);
}
void Pea::checkToDoDamage(int& x, int& y) {
if (x == getWorld()->getAvatar()->getX() &&
y == getWorld()->getAvatar()->getY()) {
getWorld()->getAvatar()->damage(2);
setDead();
return;
}
for (int i = 0; i < getWorld()->getContainerSize(); i++) {
if (!getWorld()->getContainer(i)->isAPea() &&
x == int(getWorld()->getContainer(i)->getX()) &&
y == int(getWorld()->getContainer(i)->getY()) &&
getWorld()->getContainer(i)->stopsPea()) {
if (getWorld()->getContainer(i)->isDamageable()) {
getWorld()->getContainer(i)->damage(2);
setDead();
return;
}
else {
setDead();
return;
}
}
}
}
void Pea::doSomething() {
if (!isAlive()) {
return;
}
int x = int(getX()), y = int(getY());
checkToDoDamage(x, y);
if (!isAlive()) {
return;
}
spaceInFrontofActor(getDirection(), x, y);
moveTo(x, y);
checkToDoDamage(x, y);
}
//Marble Definitions
Marble::Marble(StudentWorld* world, int startX, int startY)
: Actor(IID_MARBLE, startX, startY, 10, world) {}
void Marble::damage(int damageAmt) {
decHitPoints(damageAmt);
}
bool Marble::bePushedBy(Agent* a, int x, int y) {
if (a->canPushMarbles()) {
spaceInFrontofActor(a->getDirection(), x, y);
for (int i = 0; i < getWorld()->getContainerSize(); i++) {
if (x == getWorld()->getContainer(i)->getX() &&
y == getWorld()->getContainer(i)->getY() &&
!getWorld()->getContainer(i)->allowsMarbleColocation()) {
return false;
}
}
moveTo(x, y);
return true;
}
return false;
}
//Pit Definitions
Pit::Pit(StudentWorld* world, int startX, int startY)
: Actor(IID_PIT, startX, startY, 20, world) {}
void Pit::doSomething() {
if (!isAlive()) { return; }
for (int i = 0; i < getWorld()->getContainerSize(); i++) {
if (getX() == getWorld()->getContainer(i)->getX() &&
getY() == getWorld()->getContainer(i)->getY() &&
getWorld()->getContainer(i)->isSwallowable()) {
getWorld()->getContainer(i)->setDead();
setDead();
}
}
}
//PickupableItem Definitions
PickupableItem::PickupableItem(StudentWorld* world, int startX, int startY, int imageID, int score)
: Actor(imageID, startX, startY, 20, world), item_Score(score) {}
void PickupableItem::doSomething() {
if (!isAlive()) { return; }
if (getX() == getWorld()->getAvatar()->getX() &&
getY() == getWorld()->getAvatar()->getY() &&
isVisible()) {
getWorld()->increaseScore(getItemScore());
setDead();
getWorld()->playSound(SOUND_GOT_GOODIE);
doItemSomething();
}
}
//Goodie Definitions
Goodie::Goodie(StudentWorld* world, int startX, int startY, int imageID, int score)
: PickupableItem(world, startX, startY, imageID, score) {}
//ExtraLife Goodie Definitions
ExtraLifeGoodie::ExtraLifeGoodie(StudentWorld* world, int startX, int startY)
: Goodie(world, startX, startY, IID_EXTRA_LIFE, 1000) {}
void ExtraLifeGoodie::doItemSomething() {
getWorld()->incLives();
}
//RestoreHealthGoodie Definitions
RestoreHealthGoodie::RestoreHealthGoodie(StudentWorld* world, int startX, int startY)
: Goodie(world, startX, startY, IID_RESTORE_HEALTH, 500) {}
void RestoreHealthGoodie::doItemSomething() {
getWorld()->getAvatar()->setHitPoints(20);
}
//AmmoGoodie Definitions
AmmoGoodie::AmmoGoodie(StudentWorld* world, int startX, int startY)
: Goodie(world, startX, startY, IID_AMMO, 100) {}
void AmmoGoodie::doItemSomething() {
getWorld()->getAvatar()->increaseAmmo();
}
//Crystal Definitions
Crystal::Crystal(StudentWorld* world, int startX, int startY)
: PickupableItem(world, startX, startY, IID_CRYSTAL, 50) {}