-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActor.cpp
More file actions
1026 lines (830 loc) · 15.2 KB
/
Actor.cpp
File metadata and controls
1026 lines (830 loc) · 15.2 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
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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Actor.h"
#include "StudentWorld.h"
// Students: Add code to this file (if you wish), Actor.h, StudentWorld.h, and StudentWorld.cpp
//
// Actor Implementation
Actor::Actor(int imageID, int startX, int startY, Direction startDirection, StudentWorld *SW, float size, unsigned int depth)
:GraphObject(imageID, startX, startY, startDirection, size, depth), m_isActive(true), p_StudentWorld(SW)
{
setVisible(true);
}
Actor::~Actor()
{};
bool Actor::isActive() const
{
return m_isActive;
}
StudentWorld* Actor::getWorld() const
{
return p_StudentWorld;
}
bool Actor::isObstacle()
{
return false;
}
bool Actor::isPickupable()
{
return false;
}
void Actor::setInactive()
{
m_isActive = false;
}
bool Actor::annoy(int HP)
{
return false;
}
void Actor::convertDirToCoord(Direction dir, int &cx, int &cy)
{
switch (dir)
{
case left:
cx = -1;
cy = 0;
break;
case right:
cx = 1;
cy = 0;
break;
case up:
cx = 0;
cy = 1;
break;
case down:
cx = 0;
cy = -1;
}
}
// End of Actor Implementation
//Dirt Implementation
Dirt::Dirt(int startX, int startY, StudentWorld *SW)
:Actor(IID_DIRT, startX, startY, right, SW, 0.25, 3)
{
}
Dirt::~Dirt() {};
bool Dirt::isObstacle()
{
return true;
}
void Dirt::doSomething()
{
return;
}
// End of Dirt Implementation
//Human Implementation
Human::Human(int imageID, int startX, int startY, Direction startDirection, int HP, StudentWorld *SW)
:Actor(imageID, startX, startY, startDirection, SW), m_HP(HP) {}
Human::~Human() {}
int Human::getHP() const
{
return m_HP;
}
void Human::decHP(int HP)
{
m_HP -= HP;
}
// End of Human Implementation
//FrackMan Implementation
FrackMan::FrackMan(int startX, int startY, StudentWorld *SW)
:Human(IID_PLAYER, startX, startY, right, 10, SW)
{
m_water = 5;
m_charge = 1;
m_nuggets = 0;
}
FrackMan::~FrackMan() {}
int FrackMan::getWater() const
{
return m_water;
}
int FrackMan::getSonarCharge() const
{
return m_charge;
}
int FrackMan::getGoldNuggets() const
{
return m_nuggets;
}
void FrackMan::addWater()
{
m_water += 5;
}
void FrackMan::addSonarCharge()
{
m_charge++;
}
void FrackMan::addGoldNuggets()
{
m_nuggets++;
}
void FrackMan::doSomething() //imcomplete
{
if (!isActive())
return;
if (getWorld()->removeDirt(getX(), getY()) == true)
getWorld()->playSound(SOUND_DIG);
if (true)
{
int keyInput;
if (getWorld()->getKey(keyInput))
{
switch(keyInput)
{
case KEY_PRESS_ESCAPE:
setInactive();
break;
case KEY_PRESS_SPACE:
addSquirt();
break;
case KEY_PRESS_LEFT:
turnOrMove(left);
break;
case KEY_PRESS_RIGHT:
turnOrMove(right);
break;
case KEY_PRESS_UP:
turnOrMove(up);
break;
case KEY_PRESS_DOWN:
turnOrMove(down);
break;
case 'z':
case 'Z':
if (m_charge > 0)
useSonar();
break;
case KEY_PRESS_TAB:
if (m_nuggets > 0)
dropBribe();
break;
}
}
}
}
void FrackMan::turnOrMove(Direction dir)
{
int cx, cy;
convertDirToCoord(dir, cx, cy);
if (getDirection() == dir)
{
if (!getWorld()->isThereObstacle(getX() + cx, getY() + cy, this))
moveTo(getX() + cx, getY() + cy);
else
moveTo(getX(), getY());
}
else
setDirection(dir);
}
bool FrackMan::setLeaveOilField()
{
return false;
}
bool FrackMan::annoy(int HP)
{
if (HP == 0)
return true;
decHP(HP);
if (getHP() <= 0)
{
setInactive();
getWorld()->playSound(SOUND_PLAYER_GIVE_UP);
}
return true;
}
void FrackMan::addSquirt()
{
if (m_water <= 0)
return;
int cx, cy;
convertDirToCoord(getDirection(), cx, cy);
cx *= 4;
cy *= 4;
if (!getWorld()->isThereObstacle(getX() + cx, getY() + cy))
{
Squirt *s = new Squirt(getX() + cx, getY() + cy, getDirection(), getWorld());
getWorld()->storeNewActor(s);
}
getWorld()->playSound(SOUND_PLAYER_SQUIRT);
m_water--;
//m_water--;
}
void FrackMan::useSonar()
{
m_charge--;
getWorld()->sonarHidden(this);
getWorld()->playSound(SOUND_SONAR);
}
void FrackMan::dropBribe()
{
m_nuggets--;
if (m_water <= 0)
return;
Bribe *b = new Bribe(getX(), getY(), getWorld());
getWorld()->storeNewActor(b);
}
//End of Frackman implementation
//Protester Implementation
Protester::Protester(int imageID, int startX, int startY, int HP, StudentWorld *SW)
:Human(imageID, startX, startY, left, HP, SW), m_leaveOilField(false) //fix:: test purposes!!
{
m_restCount = 0;
m_nSquares = getWorld()->randInt(8, 60);
//fix:set private members
m_xNext = getX();
m_yNext = getY();
m_shoutCount = 0;
m_perpendicularCount = 200;
}
Protester:: ~Protester()
{
}
int Protester::getRestCount()
{
return m_restCount;
}
bool Protester::getLeaveOilField()
{
return m_leaveOilField;
}
bool Protester::canUseRadar()
{
return false;
}
void Protester::addRestCount(int count)
{
m_restCount += count;
}
void Protester::setRestCount(int count)
{
m_restCount = count;
}
void Protester::doSomething()
{
if (!isActive())
return;
if (m_restCount > 0)
{
m_restCount--;
return;
}
else
{
m_restCount = calcRestCount();
m_perpendicularCount--;
}
if (m_leaveOilField) //if protester needs to leave the oilfield
{
leaveOilField();
return;
}
if (m_shoutCount > 0)
{
m_shoutCount--;
return;
}
int x, y;
getWorld()->whereIsFrackMan(x, y);
if (getWorld()->isThereHuman(this, true, 4) && isFacingFrackMan(x, y, getDirection())) //frackman within range && Protester is facing frackman
{
if (m_shoutCount <= 0)
{
getWorld()->playSound(SOUND_PROTESTER_YELL);
getWorld()->causeAnnoyance(IID_PLAYER);
m_shoutCount = 15;
}
return;
}
if (!getWorld()->isThereHuman(this, true, 4))
{
if (useRadar())
return;
char dir;
if (getWorld()->isInDirectLine(this, dir))
{
turnThenMove(dir);
m_nSquares = 0;
return;
}
}
m_nSquares--;
if (m_nSquares <= 0)
{
moveToNewDirection();
m_nSquares = getWorld()->randInt(8, 60);
return;
}
if (!movePerpendicular() && !stepForward())
{
m_nSquares = 0;
m_restCount = 0;
return;
}
}
void Protester::setNext(int x, int y)
{
m_xNext = x;
m_yNext = y;
}
bool Protester::setLeaveOilField()
{
m_leaveOilField = true;
return true;
}
void Protester::turnThenMove(char dir)
{
if (dir != 'n')
{
int cx, cy;
Direction direction;
switch (dir)
{
case 'l':
cx = -1;
cy = 0;
direction = left;
break;
case 'r':
cx = 1;
cy = 0;
direction = right;
break;
case 'u':
cx = 0;
cy = 1;
direction = up;
break;
case 'd':
cx = 0;
cy = -1;
direction = down;
}
setDirection(direction);
moveTo(getX() + cx, getY() + cy);
setNext(getX(), getY());
return;
}
if (m_xNext == getX() + 1)
{
setDirection(right);
moveTo(m_xNext, m_yNext);
}
if (m_xNext == getX() - 1)
{
setDirection(left);
moveTo(m_xNext, m_yNext);
}
if (m_yNext == getY() + 1)
{
setDirection(up);
moveTo(m_xNext, m_yNext);
}
if (m_yNext == getY() - 1)
{
setDirection(down);
moveTo(m_xNext, m_yNext);
}
setNext(getX(), getY());
}
void Protester::moveToNewDirection()
{
int dir = getWorld()->randInt(0, 3);
switch (dir)
{
case 0:
if (!getWorld()->isThereObstacle(getX() + 1, getY()))
turnThenMove('r');
break;
case 1:
if (!getWorld()->isThereObstacle(getX() - 1, getY()))
turnThenMove('l');
break;
case 2:
if (!getWorld()->isThereObstacle(getX(), getY() - 1))
turnThenMove('d');
break;
case 3:
if (!getWorld()->isThereObstacle(getX(), getY() + 1))
turnThenMove('u');
break;
}
}
bool Protester::movePerpendicular()
{
if (m_perpendicularCount > 0)
return false;
if (getDirection() == up || getDirection() == down)
{
if (!getWorld()->isThereObstacle(getX() - 1, getY()))
{
turnThenMove('l');
m_perpendicularCount = 200;
return true;
}
if (!getWorld()->isThereObstacle(getX() + 1, getY()))
{
turnThenMove('r');
m_perpendicularCount = 200;
return true;
}
}
if (getDirection() == left || getDirection() == right)
{
if (!getWorld()->isThereObstacle(getX(), getY() - 1))
{
turnThenMove('d');
m_perpendicularCount = 200;
return true;
}
if (!getWorld()->isThereObstacle(getX(), getY() + 1))
{
turnThenMove('u');
m_perpendicularCount = 200;
return true;
}
}
return false;
}
bool Protester::stepForward()
{
Direction dir = getDirection();
int cx = 0;
int cy = 0;
convertDirToCoord(dir, cx, cy);
if (!getWorld()->isThereObstacle(getX() + cx, getY() + cy))
{
moveTo(getX() + cx, getY() + cy);
setNext(getX(), getY());
return true;
}
else
return false;
}
void Protester::leaveOilField()
{
if (getX() == 60 && getY() == 60)
{
setInactive();
return;
}
else
turnThenMove();
}
bool Protester::annoy(int HP)
{
if (HP == -1 && m_leaveOilField)
return false;
if (HP == 0 || HP == -1)
return true;
decHP(HP);
if (getHP() > 0)
{
getWorld()->playSound(SOUND_PROTESTER_ANNOYED);
int count = getWorld()->max(50, 100 - (getWorld()->getLevel() * 10));
setRestCount(count);
}
else
{
m_leaveOilField = true;
getWorld()->playSound(SOUND_PROTESTER_GIVE_UP);
setRestCount(0);
if (HP == 100)
getWorld()->increaseScore(500);
else if (canUseRadar())
{
getWorld()->increaseScore(250);
}
else
{
getWorld()->increaseScore(100);
}
}
return true;
}
bool Protester::isFacingFrackMan(int x, int y, Direction dir)
{
int lowerX, lowerY, upperX, upperY;
switch (dir)
{
case left:
lowerX = -4;
upperX = -1;
lowerY = -4;
upperY = 4;
break;
case right:
lowerX = 1;
upperX = 4;
lowerY = -4;
upperY = 4;
break;
case down:
lowerX = -4;
upperX = 4;
lowerY = -4;
upperY = -1;
break;
case up:
lowerX = -4;
upperX = 4;
lowerY = 1;
upperY = 4;
break;
}
if (x >= getX() + lowerX && x <= getX() + upperX && y >= getY() + lowerY && y <= getY() + upperY)
return true;
else
return false;
}
int Protester::calcRestCount()
{
int N = getWorld()->max(0, 3 - (getWorld()->getLevel() / 4));
return N;
}
// End of Protester Implementation
//Regular Protester Implementation
RegularProtester::RegularProtester(int startX, int startY, StudentWorld *SW)
:Protester(IID_PROTESTER, startX, startY, 5, SW) {}
RegularProtester::~RegularProtester()
{
}
bool RegularProtester::useRadar()
{
return false;
}
void RegularProtester::getBribed()
{
getWorld()->playSound(SOUND_PROTESTER_FOUND_GOLD);
getWorld()->increaseScore(25);
setLeaveOilField();
}
//End of Regular Protester Implementation
//HardCore Protester Implementation
HardcoreProtester::HardcoreProtester(int startX, int startY, StudentWorld *SW)
:Protester(IID_HARD_CORE_PROTESTER, startX, startY, 20, SW) {}
HardcoreProtester::~HardcoreProtester()
{
}
bool HardcoreProtester::canUseRadar()
{
return true;
}
bool HardcoreProtester::useRadar()
{
int x = getX();
int y = getY();
turnThenMove();
if (x == getX() && y == getY())
return false;
else
{
return true;
}
}
void HardcoreProtester::getBribed()
{
getWorld()->playSound(SOUND_PROTESTER_FOUND_GOLD);
getWorld()->increaseScore(50);
int possibleTick = 100 - (getWorld()->getLevel() * 10);
setRestCount(getWorld()->max(possibleTick, 50));
}
//End of Hardcore Protester Implementation
//Boulder Implementation
Boulder::Boulder(int startX, int startY, StudentWorld *SW)
:Actor(IID_BOULDER, startX, startY, down, SW, 1.0, 1)
{
m_tickUntilFall = 30;
m_dirtUnder = true;
}
Boulder::~Boulder()
{
}
bool Boulder::isObstacle()
{
return true;
}
void Boulder::doSomething()
{
if (!isActive())
return;
if (m_dirtUnder == true)
{
if (getWorld()->dirtUnderneath(this))
return;
else
{
m_dirtUnder = false;
return;
}
}
if (m_tickUntilFall > 0)
{
m_tickUntilFall--;
return;
}
if (m_tickUntilFall <= 0)
{
if (m_tickUntilFall == 0)
{
getWorld()->playSound(SOUND_FALLING_ROCK);
m_tickUntilFall--;
}
if (!getWorld()->isThereObstacle(getX(), getY() - 1, this))
{
moveTo(getX(), getY() - 1);
getWorld()->annoySurroundings(this, 100);
}
else
setInactive();
}
}
Squirt::Squirt(int startX, int startY, Direction startDirection, StudentWorld *SW)
:Actor(IID_WATER_SPURT, startX, startY, startDirection, SW, 1.0, 1)
{
m_travelDistance = 4;
}
Squirt::~Squirt()
{
}
void Squirt::doSomething()
{
if (!isActive())
return;
getWorld()->annoySurroundings(this, 2);
if (!isActive())
return;
if (m_travelDistance <= 0)
{
setInactive();
return;
}
int cx, cy;
convertDirection(cx, cy);
if (getWorld()->isThereObstacle(getX() + cx, getY() + cy))
{
setInactive();
return;
}
else
{
moveTo(getX() + cx, getY() + cy);
m_travelDistance--;
return;
}
}
void Squirt::convertDirection(int &cx, int &cy)
{
Direction dir = getDirection();
switch (dir)
{
case left:
cx = -1;
cy = 0;
break;
case right:
cx = 1;
cy = 0;
break;
case down:
cx = 0;
cy = -1;
break;
case up:
cx = 0;
cy = 1;
break;
}
}
ActivatingObject::ActivatingObject(int imageID, int startX, int startY, StudentWorld *SW, bool ticking, int tick)
:Actor(imageID, startX, startY, right, SW, 1.0, 2)
{
setVisible(false);
m_canYouSeeThis = false;
m_isTicking = ticking;
m_tickLeft = tick;
int t = getWorld()->max(100, 300 - 10 * getWorld()->getLevel());
if (m_tickLeft == -1)
m_tickLeft = t;
}
ActivatingObject::~ActivatingObject()
{
}
void ActivatingObject::doSomething()
{
if (!isActive())
return;
if (!m_canYouSeeThis && getWorld()->isThereHuman(this, true, 4))
{
showItself();
checkTickLeft();
return;
}
if (getWorld()->isThereHuman(this, true, 3))
{
activate();
return;
}
else
{
checkTickLeft();
}
}
void ActivatingObject::checkTickLeft()
{
if (!m_isTicking)
return;
m_tickLeft--;
if (m_tickLeft <= 0)
setInactive();
}
void ActivatingObject::showItself()
{
setVisible(true);
m_canYouSeeThis = true;
}
bool ActivatingObject::isPickupable()
{
return true;
}
OilBarrel::OilBarrel(int startX, int startY, StudentWorld *SW)
:ActivatingObject(IID_BARREL, startX, startY, SW)
{
}
OilBarrel::~OilBarrel()
{
}
void OilBarrel::activate()
{
setInactive();
getWorld()->playSound(SOUND_FOUND_OIL);
getWorld()->increaseScore(1000);
getWorld()->barrelFound();
}
GoldNugget::GoldNugget(int startX, int startY, StudentWorld *SW)
:ActivatingObject(IID_GOLD, startX, startY, SW)
{
}
GoldNugget::~GoldNugget()
{
}
void GoldNugget::activate()
{
setInactive();
getWorld()->playSound(SOUND_GOT_GOODIE);
getWorld()->addToInventory(IID_GOLD);
getWorld()->increaseScore(10);
}
Bribe::Bribe(int startX, int startY, StudentWorld *SW)
:ActivatingObject(IID_GOLD, startX, startY, SW, true, 100)
{
setVisible(true);
}
Bribe::~Bribe()
{
}
void Bribe::doSomething()
{
if (!isActive())
return;
if (getWorld()->isThereHuman(this, false, 3))
{
setInactive();
}
else
{
checkTickLeft();
}
}
void Bribe::activate()
{
//nothing
}
SonarKit::SonarKit(int startX, int startY, StudentWorld *SW)
:ActivatingObject(IID_SONAR, startX, startY, SW, true)
{
setVisible(true);
}
SonarKit::~SonarKit()
{
}