-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActor.cpp
981 lines (757 loc) · 26.8 KB
/
Actor.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
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
#include "Actor.h"
#include "StudentWorld.h"
#include "GraphObject.h"
#include "GameConstants.h"
#include <cstdlib>
//Throughout this code, there are many checks solely relating to the player. This is due to that fact
//that the player is stored individually as a pointer whereas every other Actor is contained in a list
Actor::Actor(StudentWorld* swd, int ID, int x, int y, Direction start, bool Barrier, int bulletEffect, int hitPoints)
: GraphObject(ID, x, y, start), m_world(swd), m_isBar(Barrier),
m_isAlive(true), m_bulletEffect(bulletEffect), m_hitPoints(hitPoints)
{
setVisible(true); //all Actors (other than exits) are immediately set visible
}
void Actor::doDamage()
{
m_hitPoints -= 2;
if (m_hitPoints <= 0)
setDead();
}
void Actor::setHealth(int toNum)
{
m_hitPoints = toNum;
}
void Actor::setDead()
{
m_isAlive = false;
}
StudentWorld* Actor::getWorld() const
{
return m_world;
}
bool Actor::isStealable() const
{
return false; //most actors cannot be stolen, only goodies
}
bool Actor::countsInFactoryCount() const
{
return false; //most actors do not count in the Factory's census
}
int Actor::getBulletEffect() const
{
return m_bulletEffect; //returns an integer that corresponds to the effect when a bullet hits it
}
bool Actor::isBarrier() const
{
return m_isBar;
}
bool Actor::isAlive() const
{
return m_isAlive;
}
int Actor::getHealth() const
{
return m_hitPoints;
}
Agent::Agent(StudentWorld* swd, int ID, int x, int y, int health, int sound, Direction start)
: Actor(swd, ID, x, y, start, true, BULLET_STRIKES, health), m_sound(sound),
deadBot(false)
{}
bool Agent::moveRegular(Direction d) //all agents move according to the same rules
{
switch (d) //d can be four possible values: up, down, left or right
{
case up:
{
Actor* check = getWorld()->spaceContains(getX(), getY() + 1); //returns pointer to that space
//the following code ensures that the space is either empty or does not contain the player, and that there exists no barrier on that space.
if (check == nullptr || (getWorld()->checkPlayer(getX(), getY() + 1) &&
!getWorld()->containsBarrier(getX(), getY() + 1)))
{
moveTo(getX(), getY() + 1); //move up
return true;
}
return false;
}
case right: //The content of each case is identical other than the changes necessary for
{ //different directions
Actor* check = getWorld()->spaceContains(getX() + 1, getY());
if (check == nullptr || (getWorld()->checkPlayer(getX() + 1, getY()) &&
!getWorld()->containsBarrier(getX() + 1, getY())))
{
moveTo(getX() + 1, getY()); //move right
return true;
}
return false;
}
case down:
{
Actor* check = getWorld()->spaceContains(getX(), getY() - 1);
if (check == nullptr || (getWorld()->checkPlayer(getX(), getY() - 1) &&
!getWorld()->containsBarrier(getX(), getY() - 1)))
{
moveTo(getX(), getY() - 1); //move down
return true;
}
return false;
}
case left:
{
Actor* check = getWorld()->spaceContains(getX() - 1, getY());
if (check == nullptr || (getWorld()->checkPlayer(getX() - 1, getY()) &&
!getWorld()->containsBarrier(getX() - 1, getY())))
{
moveTo(getX() - 1, getY()); //move left
return true;
}
return false;
}
default:
return false;
}
return false; //code should never be reached, assuming switch direction is given correctly
}
void Agent::shoot()
{
switch (getDirection()) //this switch direction is used to determine the location and direction of
{ // dynamically allocated bullet
case up:
{
getWorld()->addBullet(getX(), getY() + 1, up);
getWorld()->playSound(m_sound);
break;
}
case right:
{
getWorld()->addBullet(getX() + 1, getY(), right);
getWorld()->playSound(m_sound);
break;
}
case down:
{
getWorld()->addBullet(getX(), getY() - 1, down);
getWorld()->playSound(m_sound);
break;
}
case left:
{
getWorld()->addBullet(getX() - 1, getY(), left);
getWorld()->playSound(m_sound);
break;
}
}
}
Player::Player(StudentWorld* swd, int x, int y)
: Agent(swd, IID_PLAYER, x, y, 20, SOUND_PLAYER_FIRE, right), m_ammo(20)
{}
void Player::doSomething()
{
if (!isAlive())
return;
int key;
if (getWorld()->getKey(key)) //function in studentWorld to get input direction from user
{
switch (key) // 6 options for the user
{
case KEY_PRESS_DOWN:
{
setDirection(down);
Actor* act = getWorld()->spaceContains(getX(), getY() - 1);
if (act != nullptr)
{
Boulder* bd = dynamic_cast<Boulder*>(act); //used to check for a Boulder in
if (bd != nullptr) //the intended space to move to
{
if(bd->push(getX(), getY() - 2)) //dynamic_cast pointer needed
moveRegular(down); // since Boulders serve as exception
//Sometimes they are a barrier,
break; //sometimes they are moveable
}
}
moveRegular(down);
break;
}
case KEY_PRESS_UP:
{
setDirection(up);
Actor* act = getWorld()->spaceContains(getX(), getY() + 1);
if (act != nullptr)
{
Boulder* bd = dynamic_cast<Boulder*>(act);
if (bd != nullptr)
{
if (bd->push(getX(), getY() + 2)) //if the space does have a boulder, push it
moveRegular(up);
break;
}
}
moveRegular(up);
break;
}
case KEY_PRESS_LEFT:
{
setDirection(left);
Actor* act = getWorld()->spaceContains(getX() - 1, getY());
if (act != nullptr)
{
Boulder* bd = dynamic_cast<Boulder*>(act);
if (bd != nullptr)
{
if(bd->push(getX() - 2, getY()))
moveRegular(left);
break;
}
}
moveRegular(left);
break;
}
case KEY_PRESS_RIGHT:
{
setDirection(right);
Actor* act = getWorld()->spaceContains(getX() + 1, getY());
if (act != nullptr)
{
Boulder* bd = dynamic_cast<Boulder*>(act);
if (bd != nullptr)
{
if (bd->push(getX() + 2, getY()))
moveRegular(right);
break;
}
}
moveRegular(right);
break;
}
case KEY_PRESS_ESCAPE:
{
setDead(); //give up life to restart the level
break;
}
case KEY_PRESS_SPACE:
{
if (m_ammo > 0) //if you have ammo, shoot
{
m_ammo--;
shoot(); //use Player's base class Agent's shoot function
}
break;
}
}
}
}
void Player::doDamage()
{
Actor::doDamage(); //use Actor's doDamage() function to decrease health
if (isAlive())
getWorld()->playSound(SOUND_PLAYER_IMPACT);
else
getWorld()->playSound(SOUND_PLAYER_DIE);
}
void Player::addAmmo()
{
m_ammo += 20;
}
int Player::healthPercent() const
{
return (getHealth()* 5);
}
int Player::ammoCount() const
{
return m_ammo;
}
Robot::Robot(StudentWorld* swd, int ID, int x, int y, int health, Direction d, int bonus)
: Agent(swd, ID, x, y, health, SOUND_ENEMY_FIRE, d ), m_bonus(bonus), m_continue(false)
{setTick();}
void Robot::doSomething() //this is the beginning sequence for all 3 robots doSomething()
{
m_continue = false;
if (!isAlive())
return;
if (m_tick != 1) //ensure that the Robot is only acting one per their tick int
{
decTick();
return;
}
if (doesShoot() && canAim()) //if the robot is a shooting Robot and it is facing toward the player
{ //with a clear, straight path it should shoot
shoot();
setTick();
return;
}
m_continue = true;
}
void Robot::doDamage()
{
Actor::doDamage(); //use Actor's doDamage function to decrease health by 2 hit-points
if (isAlive())
getWorld()->playSound(SOUND_ROBOT_IMPACT);
else
{
getWorld()->playSound(SOUND_ROBOT_DIE);
getWorld()->increaseScore(m_bonus); //every robot has an individual bonus given to the player
} //if it is killed
}
bool Robot::canAim() //used to see if the player can be shot by the robot
{
//The following series of code repeats the same style of checks for all directions and possibilites:
//1. It checks to see if one of the coordinates of the robot is the same as the player's
//2. Depending on how the player's second coordinate relates to the robot's second coord, it checks
//that the robot's direction is correct for these relative positions
//3. It then loops through all blocks in the way to check that the path is clear for the bullet
//4. If all of these are true, it returns true
if (getWorld()->getPlayer()->getX() == getX())
{
if (getWorld()->getPlayer()->getY() > getY() && getDirection() == up)
{
for (int k = 1; k < getWorld()->getPlayer()->getY() - getY(); k++)
{
if (getWorld()->spaceContains(getX(), getY() + k) != nullptr)
{
if (getWorld()->spaceContains(getX(), getY() + k)->isBarrier())
return false;
}
}
return true;
}
else if (getWorld()->getPlayer()->getY() < getY() && getDirection() == down)
{
for (int k = 1; k < getY() - getWorld()->getPlayer()->getY(); k++)
{
if (getWorld()->spaceContains(getX(), getY() - k) != nullptr)
{
if (getWorld()->spaceContains(getX(), getY() - k)->isBarrier())
return false;
}
}
return true;
}
}
else if (getWorld()->getPlayer()->getY() == getY())
{
if (getWorld()->getPlayer()->getX() > getX() && getDirection() == right)
{
for (int k = 1; k < getWorld()->getPlayer()->getX() - getX(); k++)
{
if (getWorld()->spaceContains(getX() + k, getY()) != nullptr)
{
if (getWorld()->spaceContains(getX() + k, getY())->isBarrier())
return false;
}
}
return true;
}
else if (getWorld()->getPlayer()->getX() < getX() && getDirection() == left)
{
for (int k = 1; k < getX() - getWorld()->getPlayer()->getX(); k++)
{
if (getWorld()->spaceContains(getX() - k, getY()) != nullptr)
{
if (getWorld()->spaceContains(getX() - k, getY())->isBarrier())
return false;
}
}
return true;
}
}
return false;
}
void Robot::setTick() //This sets the robots tick to ensure it operates at the correct pace
{
m_tick = (28 - getWorld()->getLevel()) / 4; //This code was given by the spec
if (m_tick < 3)
m_tick = 3;
}
void Robot::decTick()
{
m_tick--;
}
bool Robot::doesShoot() const
{
return true;
}
bool Robot::shouldContinue() const
{
return m_continue;
}
SnarlBot::SnarlBot(StudentWorld* swd, int x, int y, Direction d)
: Robot(swd, IID_SNARLBOT, x, y, 10 ,d, 100)
{}
void SnarlBot::doSomething()
{
Robot::doSomething(); //use Base class's doSomething() to complete first few checks
if (!shouldContinue()) //if the Robot base class's doSomething() returned early, then return
return;
bool didMove = moveRegular(getDirection()); //move with the agent's moveRegular function
if (!didMove) //if the SnarlBot couldn't move, reverse it's current direction
{
if (getDirection() == right)
setDirection(left);
else if (getDirection() == left)
setDirection(right);
else if (getDirection() == up)
setDirection(down);
else if (getDirection() == down)
setDirection(up);
}
setTick(); //reset tick, since tick == 1
}
KleptoBot::KleptoBot(StudentWorld* swd, int ID, int x, int y, int health, int bonus)
: Robot(swd, ID, x, y, health, right , bonus), hasGoodie(false), m_currentDirectionSteps(0)
{
m_distanceBeforeTurning = rand() % 6 + 1; //sets the distance to a random number between 1 & 6
}
void KleptoBot::doSomething()
{
Robot::doSomething(); //start the function off as all robot's do
if (!shouldContinue())
return;
bool didMove = false;
Pickup* goodie = getWorld()->takeStealable(getX(), getY());
if (goodie != nullptr && !hasGoodie) //if the Robot is on a goodie, and hasn't already taken one:
{
int stealChance = rand() % 10 + 1; //set stealChance to a random number between 1 & 10
if (stealChance == 5) //5 is an arbitray number as it could be any number between 1 & 10
{
goodie->setDead(); //destroy the goodie
getWorld()->playSound(SOUND_ROBOT_MUNCH);
hasGoodie = true;
if (goodie->getBonus() == 1000) //By knowning the bonuns associated with the bonus, it's
goodieKind = 'L'; //type can be known and set to be re-created later
else if (goodie->getBonus() == 500) //when/if the KleptoBot is killed
goodieKind = 'R';
else if (goodie->getBonus() == 100)
goodieKind = 'A';
setTick(); //reset the tick, since it == 1
return;
}
}
//if the KleptoBot hasn't moved it's random limit of steps before it must turn
//and if the KleptoBot can move, then it is to move, increase it's count, reset it ticks and return
if (m_currentDirectionSteps <= m_distanceBeforeTurning)
{
didMove = moveRegular(getDirection());
if (didMove)
{
m_currentDirectionSteps++;
setTick();
return;
}
}
if (!didMove) //if it couldn't move, then it must select a random direction and try to move again
{
m_distanceBeforeTurning = rand() % 6 + 1;
int y = 0;
bool doneUp = false;
bool doneRight = false;
bool doneDown = false;
bool doneLeft = false;
while (int x = rand() % 4 + 1) //loop serves to check all 4 direction randomly if needed
{ //the done(Dir) boolean ensures that no direction is tested twice
if (y == 0) //if it's the first iteration of the loop, set y = to the random number from 1 - 4
y = x; //if the KleptoBot cannot move, y will be used to set the direction the Bot faces
if (x == 1 && !doneUp)
{
if (moveRegular(up))
{
setDirection(up);
setTick();
return;
}
doneUp = true;
}
if (x == 2 && !doneRight) // if it hasn't been tested:
{
if (moveRegular(right)) //check to move right
{
setDirection(right); //if it can move, change the direction and reset the Tick
setTick();
return;
}
doneRight = true;
}
if (x == 3 && !doneDown)
{
if (moveRegular(down))
{
setDirection(right);
setTick();
return;
}
doneDown = true;
}
if (x == 4 && !doneLeft)
{
if(moveRegular(left))
{
setDirection(left);
setTick();
return;
}
doneLeft = true;
}
if (doneUp && doneRight && doneDown && doneLeft)
break;
}
//if the Bot cannot move in any direction, set the direction the Bot faces to the first random dir
if (y == 1)
setDirection(up);
if (y == 2)
setDirection(right);
if (y == 3)
setDirection(down);
if (y == 4)
setDirection(left);
}
setTick();
}
void KleptoBot::doDamage()
{
Robot::doDamage();
if (!isAlive() && hasGoodie) //if the KleptoBot has a goodie
{ //add it back to where the Bot is when it dies
getWorld()->addGoodieBack(getX(), getY(), goodieKind);
}
}
bool KleptoBot::countsInFactoryCount() const
{
return true; //KleptoBot are the only thing counted in the Factory Census
}
RegularKleptoBot::RegularKleptoBot(StudentWorld* swd, int x, int y)
:KleptoBot(swd, IID_KleptoBot, x, y, 5, 10)
{}
bool RegularKleptoBot::doesShoot() const
{
return false;
}
AngryKleptoBot::AngryKleptoBot(StudentWorld* swd, int x, int y)
:KleptoBot(swd,IID_ANGRY_KleptoBot, x, y, 8, 20)
{}
Barrier::Barrier(StudentWorld* swd, int ID, int x, int y, int hitPoints)
: Actor(swd, ID, x, y, none, true, BULLET_DESTROYED, hitPoints)
{}
void Barrier::doSomething(){} //Barriers (other than Factories) do nothing
Wall::Wall(StudentWorld* swd, int x, int y)
: Barrier(swd, IID_WALL, x, y)
{}
Boulder::Boulder(StudentWorld* swd, int x, int y)
: Barrier(swd, IID_BOULDER, x, y, 10)
{}
bool Boulder::push(int moveX, int moveY)
{
Actor* space = getWorld()->spaceContains(moveX, moveY);
Hole* hol = dynamic_cast<Hole*>(space); //dynamic_cast pointer needed to check for a Hole
if (space == nullptr || hol != nullptr) //if the space is empty, or there is a hole, move there
{
moveTo(moveX, moveY);
return true;
}
return false;
}
int Boulder::getBulletEffect() const
{
return BULLET_STRIKES; //unlike other Barriers, a Boulder is affected and damaged by a bullet
}
Hole::Hole(StudentWorld* swd, int x, int y)
:Barrier(swd, IID_HOLE, x, y)
{}
void Hole::doSomething()
{
if (!isAlive())
return;
Boulder* check = getWorld()->getBoulder(getX(), getY());
if (check != nullptr) //if a Boulder is on itself, is dies and the boulder does as well
{
setDead();
check->setDead();
}
}
int Hole::getBulletEffect() const
{
return BULLET_NOTHING; //unlike other barriers, a bullet is unaffected by a Hole
}
Factory::Factory(StudentWorld* swd, int x, int y, bool angry)
:Barrier(swd, IID_ROBOT_FACTORY, x, y), m_angry(angry)
{}
void Factory::doSomething()
{
int random = rand() % 50 + 1;
int count = 0;
bool result = getWorld()->doCensusCount(getX(), getY(), count); //take census of 7 x 7 surrounding
if (result && count < 3) //if there is no Bot on top of the factory, and the count of Kleptos < 3:
{
if (random == 10 && !m_angry) //25 is an arbitrary number, could be any number x for which 1 <= x <= 50
//There is a 1 in 50 chance every tick that a KleptoBot will be added. Depending on the
//specification of the level, a particular factory makes either RegularKleptoBots or AngryKleptos
{
getWorld()->addKleptoBot(getX(), getY(), 10);
getWorld()->playSound(SOUND_ROBOT_BORN);
}
else if (random == 10 && m_angry)
{
getWorld()->addKleptoBot(getX(), getY(), 20);
getWorld()->playSound(SOUND_ROBOT_BORN);
}
}
}
Accessible::Accessible(StudentWorld* swd, int ID, int x, int y, Direction start)
: Actor(swd, ID, x, y,start,false)
{}
Pickup::Pickup(StudentWorld* swd, int ID, int x, int y, int bonus, int sound)
: Accessible(swd, ID, x, y), m_bonus(bonus), m_sound(sound)
{}
void Pickup::doSomething()
{
if (!isAlive() || !isOpen()) //if it's Alive and open (only meaningful for the exit), continue
return;
if (!getWorld()->checkPlayer(getX(), getY()))
{ //if the player picks up the Pickup
getWorld()->increaseScore(m_bonus); //give the associated bonus, play the associated sound, kill
setDead();
getWorld()->playSound(m_sound);
inform(); //this function does different things for different pickups
} //this function allows the general Base class doSomething to
} //function properly
bool Pickup::isOpen() const
{
return true;
}
bool Pickup::isStealable() const
{
return true;
}
int Pickup::getBonus() const
{
return m_bonus;
}
Jewel::Jewel(StudentWorld* swd, int x, int y)
: Pickup(swd, IID_JEWEL, x, y, 50)
{}
void Jewel::inform()
{}
bool Jewel::isStealable() const
{
return false;
}
Exit::Exit(StudentWorld* swd, int x, int y)
: Pickup(swd, IID_EXIT, x, y, 2000, SOUND_FINISHED_LEVEL), m_open(false)
{
setVisible(false); //unlike all other Actors, the exit starts off as invisible
}
void Exit::inform()
{
getWorld()->addRunningBonus(); //the Exit additionally adds the running bonus
getWorld()->setLevelComplete(); //and sets the level as completed
}
void Exit::setOpen()
{
m_open = true;
}
bool Exit::isOpen() const
{
return m_open;
}
bool Exit::isStealable() const
{
return false;
}
ExtraLife::ExtraLife(StudentWorld* swd, int x, int y)
:Pickup(swd, IID_EXTRA_LIFE, x, y, 1000)
{}
void ExtraLife::inform()
{
getWorld()->incLives();
}
RestoreHealth::RestoreHealth(StudentWorld* swd, int x, int y)
:Pickup(swd, IID_RESTORE_HEALTH, x, y, 500)
{}
void RestoreHealth::inform()
{
getWorld()->getPlayer()->setHealth(20);
}
Ammo::Ammo(StudentWorld* swd, int x, int y)
: Pickup(swd, IID_AMMO, x, y, 100)
{}
void Ammo::inform()
{
getWorld()->getPlayer()->addAmmo();
}
Bullet::Bullet(StudentWorld* swd, int x, int y, Direction d)
:Accessible(swd, IID_BULLET, x, y, d)
{}
void Bullet::doSomething()
{
if (!isAlive())
return;
if (attemptHit()) //attempts to hit the Actor on the current space
return;
switch (getDirection()) //if couldn't hit, it move ahead one space in it's given direction
{
case up:
{
moveTo(getX(), getY() + 1);
break;
}
case right:
{
moveTo(getX() + 1, getY());
break;
}
case down:
{
moveTo(getX(), getY() - 1);
break;
}
case left:
{
moveTo(getX() - 1, getY());
break;
}
}
attemptHit(); //attempts to hit what is on it's new space
}
bool Bullet::attemptHit()
{
bool playerShot = getWorld()->checkPlayer(getX(), getY());
if (!playerShot) //if the bullet and the player are on the same space, doDamage & set bullet dead
{
getWorld()->getPlayer()->doDamage();
setDead();
return true;
}
Actor* target = getWorld()->spaceContains(getX(), getY());
Actor* target2 = getWorld()->secondBulletCheck(getX(), getY());
if (target != nullptr) // realistically target can never be the nullptr since spaceContains will
// at least return a pointer to the actor in bullet.
{
if (target->getBulletEffect() == BULLET_STRIKES) //BULLET_STRIKES means to do damage
{
target->doDamage();
setDead();
return true;
}
else if(target->getBulletEffect() == BULLET_DESTROYED) //hit a barrier
{
if (target2 == nullptr) //this checks to ensure there are not multiple Actors on the same space
{ //For example, if there is a jewel and robot, the bullet must strike.
setDead(); //If the first target pointer is set pointing to the jewel though, the
return true; //bullet will think to continue on. THis shows that a 2nd check is
} //neede
else
{
target2->doDamage();
setDead();
return true;
}
}
else if(target->getBulletEffect() == BULLET_NOTHING)
{
if (target2 != nullptr) //do 2nd check again
{
target2->doDamage();
setDead();
return true;
}
}
}
return false;
}