-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ghost.h
973 lines (901 loc) · 32.3 KB
/
Ghost.h
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
// The ghost work on a simple phenomenon of number of pills, they are read and according to that they are allowed to leave.
#ifndef GHOST_H
#define GHOST_H
extern int gameTick;
extern Pacman pacman;
extern bool frighten;
extern int eatenCount;
typedef enum {SCATTER, CHASE, FRIGHTEN, LEAVE, DEATH, PEN} movement;
typedef enum {RED, BLUE, PINK, YELLOW} colour;
direction tempDir = NONE;
class Ghost {
public:
float ghostX;
float ghostY;
direction currentDir;
colour ghostColour;
movement moveType;
float speed;
bool eaten;
/**
*
* x set the ghosts x position
* y set the ghosts y position
* ghost set the colour of the ghost
*/
Ghost(float x, float y, colour ghost) {
ghostX = x;
ghostY = y;
switch (ghost) {
case RED:
currentDir = LEFT;
moveType = SCATTER;
break;
case PINK:
currentDir = DOWN;
moveType = PEN;
break;
case BLUE:
currentDir = UP;
moveType = PEN;
break;
case YELLOW:
currentDir = UP;
moveType = PEN;
break;
}
eaten = false;
ghostColour = ghost;
setSpeed();
}
/**
* Draws the score added to the game when pacman eats the ghost. Thie is drawn at the ghosts x and y position at the time of eating
* and the score also depends on how many ghosts, if any, had been eaten before it
* x the ghosts X position at the time of eating
* y the ghosts y position at the time of eating
*/
void drawEatScore(float x, float y)
{
glPushMatrix();
unsigned int eat_score_tex;
switch(eatenCount) { // Pick which texture to draw depending on which number ghost is beign eaten
case 1: eat_score_tex = score_200_tex;
break;
case 2: eat_score_tex = score_400_tex;
break;
case 3: eat_score_tex = score_800_tex;
break;
case 4: eat_score_tex = score_1600_tex;
break;
}
translateBottomLeft();
translateToMazeCoords(x, y); // Translate to correct part of the screen for drawing
glTranslatef(-2.0f, 0.0f, 0.0f);
if(eatenCount != 0) { // Only draw if a ghost has been eaten
drawTex(eat_score_tex, 10, 8, 0);
}
glPopMatrix();
}
/**
* Method to draw the ghost with the correct texture and the eyes facing the correct direction.
*/
void draw() {
glPushMatrix();
unsigned int ghost_tex;
unsigned int ghost_eye_tex;
if (moveType != FRIGHTEN && moveType != DEATH) { // As long as the ghost is not in death or frighten mode, assign its relevant texture
int tickDiv = gameTick % 20;
if (tickDiv % 20 < 10) {
switch (ghostColour) { // Check ghost colour in order to determine which tex file to use
case RED:
ghost_tex = ghost_r_0_tex;
break;
case BLUE:
ghost_tex = ghost_b_0_tex;
break;
case PINK:
ghost_tex = ghost_p_0_tex;
break;
case YELLOW:
ghost_tex = ghost_y_0_tex;
break;
}
} else {
switch (ghostColour) {
case RED:
ghost_tex = ghost_r_1_tex;
break;
case BLUE:
ghost_tex = ghost_b_1_tex;
break;
case PINK:
ghost_tex = ghost_p_1_tex;
break;
case YELLOW:
ghost_tex = ghost_y_1_tex;
break;
}
}
switch (currentDir) { // Eye position/tex file is dependent on direction the ghost is moving
case LEFT:
ghost_eye_tex = eye_left_tex;
break;
case UP:
ghost_eye_tex = eye_up_tex;
break;
case RIGHT:
ghost_eye_tex = eye_right_tex;
break;
case DOWN:
ghost_eye_tex = eye_down_tex;
break;
}
} else if(moveType == FRIGHTEN){ // If the ghost is frightened assign its relevant texture
int tickDiv = gameTick % 40;
if (tickDiv % 40 < 10) {
ghost_tex = ghost_scared_0_tex;
} else if (tickDiv % 40 < 20) {
ghost_tex = ghost_scared_1_tex;
} else if (tickDiv % 40 < 30) {
ghost_tex = ghost_scared_2_tex;
} else {
ghost_tex = ghost_scared_3_tex;
}
} else if(moveType == DEATH) {
switch (currentDir) { // Eye position/tex file is dependent on direction the ghost is moving
case LEFT:
ghost_eye_tex = eye_left_tex;
break;
case UP:
ghost_eye_tex = eye_up_tex;
break;
case RIGHT:
ghost_eye_tex = eye_right_tex;
break;
case DOWN:
ghost_eye_tex = eye_down_tex;
break;
}
}
translateBottomLeft(); // Translate to bottom left of screen
translateToMazeCoords(ghostX, ghostY);
glTranslatef(-3.0f, -3.0f, 0.0f);
if(moveType != DEATH) { // If the ghost is not dead then draw its body texture
drawTex(ghost_tex, 14, 14, 0);
}
if (moveType != FRIGHTEN) { // If the ghost is not frightened then draw the eye texture
drawTex(ghost_eye_tex, 14, 14, 0);
}
glPopMatrix();
}
/**
* Method to reset the ghosts to their initial settings/ positions when the game is restarted
*/
void reset() {
switch (ghostColour) { // Different settings depending on the colour of the ghost
case RED:
ghostX = 13.5f;
ghostY = 19.0f;
currentDir = LEFT;
moveType = SCATTER;
break;
case BLUE:
ghostX = 11.5f;
ghostY = 16.0f;
currentDir = UP;
if (pills <= 163) {
moveType = LEAVE;
} else {
moveType = PEN;
}
break;
case YELLOW:
ghostX = 15.5f;
ghostY = 16.0f;
currentDir = UP;
if (pills <= 214) {
moveType = LEAVE;
} else {
moveType = PEN;
}
break;
case PINK:
ghostX = 13.5f;
ghostY = 16.0f;
currentDir = DOWN;
moveType = LEAVE;
break;
}
eaten = false; // Upon reset no ghost has been eaten
}
/**
* Method determines what mode ghosts are in, and sets the speed accordingly
*/
void setSpeed() {
if (moveType == FRIGHTEN) { // Move slower if frightened
speed = 0.05f;
} else {
speed = 0.1f;
}
}
/**
* Method determines the colour of the ghost and assigns the relevant movement technique to each ghost
* ghost Variable used if a ghost needs to potentially use another ghosts position in its movement tehcnique
*/
void move(Ghost ghost) {
setSpeed();
switch (ghostColour) {
case RED:
redGhostMove(ghost);
break;
case BLUE:
blueGhostMove(ghost);
break;
case PINK:
pinkGhostMove(ghost);
break;
case YELLOW:
yellowGhostMove(ghost);
break;
}
}
/**
* Returns the ghosts current X position
* return ghosts X position
*/
int getGhostX() {
return (int) floor(ghostX);
}
/**
* Returns the ghosts current X position
* return ghosts Y position
*/
int getGhostY() {
return (int) floor(ghostY);
}
/**
* Returns the number of possible exits from the specified position
* x the X position to check
* y the Y position to check
* return the number of exits from the current position that arent walls
*/
int checkJuncs(float x, float y) {
int exit = 0;
if (!isWall(LEFT)) { // Check all directions and if its not a wall in that direction, increment the counter
exit++;
}
if (!isWall(UP)) {
exit++;
}
if (!isWall(RIGHT)) {
exit++;
}
if (!isWall(DOWN)) {
exit++;
}
return exit;
}
/**
* Returns the tile type at the specified coordinates
* x X position of the tile to check
* y Y position of the tile to check
* return The type of the tile at the specified coordinates
*/
tile getTile(float x, float y) {
return maze[(int) floor(x)][(int) floor(y)];
}
/**
* Looks ahead one tile in the specified direction and returns that type of tile
* dir The direction to look ahead in
* return The type of the tile at the specified position
*/
tile getFollowingTile(direction dir) {
switch (dir) {
case LEFT:
return getTile(getGhostX() - 1, getGhostY());
case UP:
return getTile(getGhostX(), getGhostY() + 1);
case RIGHT:
return getTile(getGhostX() + 1, getGhostY());
case DOWN:
return getTile(getGhostX(), getGhostY() - 1);
default:
return getTile(getGhostX(), getGhostY());
}
}
/**
* Returns a boolean on whether the ghost is currently at the centre of its tile
* return Whether it is at the centre of the tile
*/
bool isAtCenter() {
return (int) (ghostY * 10.0f) % 10 == 0 && (int) (ghostX * 10.0f) % 10 == 0;
}
/**
*
* x the given x position of a ghost
* y the given y position of a ghost
* ghostCol the ghost colour of the current ghost
* ghost another ghost object as some ghosts need to use other ghosts in order to target their movement
* return returns the distance from the point given to the target tile
*/
float getDistance(int x, int y, colour ghostCol, Ghost ghost) {
std::vector<int> v = getTargetTile(ghostCol);
int targetX = v.at(0);
int targetY = v.at(1);
if (ghostCol == BLUE && moveType == CHASE) {
float tempX = ghost.getGhostX() - targetX;
float tempY = ghost.getGhostY() - targetY;
targetX = ghost.getGhostX() + 2 * tempX;
targetY = ghost.getGhostY() + 2 * tempY;
}
return (sqrt(((targetX - x) * (targetX - x)) + ((targetY - y) * (targetY - y))));
}
/**
* Method that handles when a ghost reaches a corner, which way it should turn
*/
void cornerHandler() {
if (isAtCenter()) { // Checks it is at the centre of its tile
if (isWall(currentDir)) { // Checks whether if it were to keep moving in it current direction if it would hit a wall
if (!isWall(LEFT) && currentDir != RIGHT) { // Checks its new direction wouldnt make it hit a wall AND that its not reversing direction
currentDir = LEFT;
} else if (!isWall(UP) && currentDir != DOWN) {
currentDir = UP;
} else if (!isWall(RIGHT) && currentDir != LEFT) {
currentDir = RIGHT;
} else {
currentDir = DOWN;
}
}
}
}
/**
* Used to move ghosts back into the pen for the final move during the DEATH moveType.
* Due to the fact that they see the ghost gate as a wall so the code moves them down until their X and Y coordinates satisfy
* certain conditions and then their moveType is changed back to CHASE or SCATTER depending on the gameTick.
*/
void moveToPen()
{
if (ghostX <= 13.55 && ghostX >= 13.45) { // Initially check they have the correct X coordinates
if (ghostY >= 18.95f) { // If so move them up and out of the gate
currentDir = DOWN;
} else if (ghostY <= 17.0f){
moveType = LEAVE;
}
} else { // If they dont have the correct x coordinates
if (ghostX < 13.5f) { // Determine if they need to move left or right
currentDir = RIGHT;
} else if (ghostX > 13.5f) {
currentDir = LEFT;
}
}
// As on the final call to this method the moveType is changed they could still move 0.1f in a direciton when not needed, so necessary check for moveType
switch (currentDir) {
case LEFT:
ghostX -= speed;
break;
case UP:
ghostY += speed;
break;
case RIGHT:
ghostX += speed;
break;
case DOWN:
ghostY -= speed;
break;
}
}
/**
* Sets the ghosts movement, dependent on colour, to take a route back to the ghost pen so that they can respawn and join the game again
* colGhost The specified ghost colour
* ghost Pass in a ghost object in case the chosen move technique must use another ghost to calculate where to move
*/
void setDeath(colour colGhost, Ghost ghost)
{
float distance = 999.0f;
bool move = true;
if(ghostX <= 13.55 && ghostX >= 13.45 && ghostY >= 15.5f && ghostY <= 20.0f) {
moveToPen();
} else {
if (checkJuncs(ghostX, ghostY) >= 3 && isAtCenter()) {
if (currentDir != DOWN && !isOnlyWall(UP)) {
float d = getDistance(getGhostX(), getGhostY() + 1, colGhost, ghost);
if (d < distance) {
tempDir = UP;
distance = d;
}
}
if (currentDir != LEFT && !isOnlyWall(RIGHT)) {
float d = getDistance(getGhostX() + 1, getGhostY(), colGhost, ghost);
if (d < distance) {
tempDir = RIGHT;
distance = d;
}
}
if (currentDir != RIGHT && !isOnlyWall(LEFT)) {
float d = getDistance(getGhostX() - 1, getGhostY(), colGhost, ghost);
if (d < distance) {
tempDir = LEFT;
distance = d;
}
}
if (currentDir != UP && !isOnlyWall(DOWN)) {
float d = getDistance(getGhostX(), getGhostY() - 1, colGhost, ghost);
if (d < distance) {
tempDir = DOWN;
distance = d;
}
}
currentDir = tempDir;
} else {
cornerHandler();
}
if(move) {
switch (currentDir) {
case LEFT:
ghostX -= speed;
ghostY = round(ghostY);
break;
case UP:
ghostY += speed;
ghostX = round(ghostX);
break;
case RIGHT:
ghostX += speed;
ghostY = round(ghostY);
break;
case DOWN:
ghostY -= speed;
ghostX = round(ghostX);
break;
}
}
}
};
/**
* Determines which direction to move by checking all potential moves around itself and comparing how close each
* potential move will put it to the target tile. It then selects the move which puts it the closest to the
* target tile
* colGhost The colour of the current ghost
* ghost Pass in a ghost object in case the chosen move technique must use another ghost to calculate where to move
*/
void moveTargetTile(colour colGhost, Ghost ghost)
{
float distance = 999.0f;
if (checkJuncs(ghostX, ghostY) >= 3 && isAtCenter()) {
if (currentDir != DOWN && !isWall(UP)) {
float d = getDistance(getGhostX(), getGhostY()+1, colGhost, ghost);
if(d < distance){
tempDir = UP;
distance = d;
}
}
if (currentDir != LEFT && !isWall(RIGHT)) {
float d = getDistance(getGhostX()+1, getGhostY(), colGhost, ghost);
if(d < distance){
tempDir = RIGHT;
distance = d;
}
}
if (currentDir != RIGHT && !isWall(LEFT) ) {
float d = getDistance(getGhostX()-1, getGhostY(), colGhost, ghost);
if(d < distance){
tempDir = LEFT;
distance = d;
}
}
if (currentDir != UP && !isWall(DOWN)) {
float d = getDistance(getGhostX(), getGhostY()-1, colGhost, ghost);
if(d < distance){
tempDir = DOWN;
distance = d;
}
}
currentDir = tempDir;
} else {
cornerHandler();
}
switch (currentDir) {
case LEFT:
ghostX -= speed;
ghostY = round(ghostY);
break;
case UP:
ghostY += speed;
ghostX = round(ghostX);
break;
case RIGHT:
ghostX += speed;
ghostY = round(ghostY);
break;
case DOWN:
ghostY -= speed;
ghostX = round(ghostX);
break;
}
}
/**
* Sets the ghosts movement during FRIGHTEN. When initially called the ghost reverses direction and then at every junction
* the ghost will pick a random direction to move in aslong as that chosen direction does not cause it to hit a wall or reverse.
* It then calls its relevant corner handler to handle corners correctly.
*/
void setFrighten()
{
direction chosenDir = NONE;
if (checkJuncs(ghostX, ghostY) >= 3) { // If at a junction
do {
int randNum = rand() % checkJuncs(ghostX, ghostY) - 1; // Select a random number between 0 and 3
if(randNum == 0 && currentDir != RIGHT) {
chosenDir = LEFT;
} else if(randNum == 1 && currentDir != DOWN) {
chosenDir = UP;
} else if(randNum == 2 && currentDir != LEFT) {
chosenDir = RIGHT;
} else if(currentDir != UP){
chosenDir = DOWN;
}
} while (isWall(chosenDir) && currentDir != NONE); // Only select the direction if it is not in a wall and the ghost is currently moving
} else {
cornerHandler();
}
if(isAtCenter() && chosenDir != NONE) {
if(!isWall(chosenDir)){
currentDir = chosenDir; // Only apply chosen direction if the ghost is at its centre, the chosen direction is NONE and the chosen direciton doesnt take it into a wal
}
}
switch (currentDir) { // Move ghost in its chosen direction incrementally according to its speed
case LEFT:
ghostX -= speed;
ghostY = round(ghostY);
break;
case UP:
ghostY += speed;
ghostX = round(ghostX);
break;
case RIGHT:
ghostX += speed;
ghostY = round(ghostY);
break;
case DOWN:
ghostY -= speed;
ghostX = round(ghostX);
break;
}
}
/**
* Determines what movement technique each ghost should take depending on its colour
* col the colour of the ghost
* ghost pass in a ghost object in case the chosen move technique must use another ghost to calculate where to move
*/
void setMove(colour col, Ghost ghost)
{
float distance = getDistance(ghostX, ghostY, RED, ghost); // Get the distance between the ghost and pacman
switch(col) {
case RED:
moveTargetTile(RED, ghost);
break;
case PINK:
moveTargetTile(RED, ghost);
break;
case YELLOW:
if (distance >= 8.0f) { // Yellows movement switches depending on how close he is to pacman
moveTargetTile(RED, ghost);
} else {
moveTargetTile(YELLOW, ghost);
}
break;
case BLUE:
moveTargetTile(BLUE, ghost);
break;
}
}
/**
* Calls the correct method for movement depending on what the current MoveType of the ghost is.
* ghost used for certain movement techniques for certain ghosts
*/
void redGhostMove(Ghost ghost)
{
switch(moveType){
case SCATTER:
portalCheck();
setMove(RED, ghost);
break;
case CHASE:
portalCheck();
setMove(RED, ghost);
break;
case FRIGHTEN:
portalCheck();
setFrighten();
break;
case DEATH:
setDeath(RED, ghost);
break;
case LEAVE:
leaveMove();
break;
}
}
/**
* Calls the correct method for movement depending on what the current MoveType of the ghost is.
* ghost used for certain movement techniques for certain ghosts
*/
void blueGhostMove(Ghost ghost)
{
if(pills == 163){
moveType = LEAVE;
}
switch(moveType){
case SCATTER:
portalCheck();
setMove(BLUE, ghost);
break;
case CHASE:
portalCheck();
setMove(BLUE, ghost);
break;
case FRIGHTEN:
portalCheck();
setFrighten();
break;
case DEATH:
setDeath(BLUE, ghost);
break;
case LEAVE:
leaveMove();
break;
case PEN: penMove();
break;
}
}
/**
* Calls the correct method for movement depending on what the current MoveType of the ghost is.
* ghost used for certain movement techniques for certain ghosts
*/
void yellowGhostMove(Ghost ghost)
{
if(pills == 214){
moveType = LEAVE;
}
switch(moveType){
case SCATTER:
portalCheck();
setMove(YELLOW, ghost);
break;
case CHASE:
portalCheck();
setMove(YELLOW, ghost);
break;
case FRIGHTEN:
portalCheck();
setFrighten();
break;
case DEATH: setDeath(YELLOW, ghost);
break;
case LEAVE: leaveMove();
break;
case PEN: penMove();
break;
}
}
/**
* Calls the correct method for movement depending on what the current MoveType of the ghost is.
* ghost used for certain movement techniques for certain ghosts
*/
void pinkGhostMove(Ghost ghost)
{
switch(moveType){
case SCATTER:
portalCheck();
setMove(PINK, ghost);
break;
case CHASE:
portalCheck();
setMove(PINK, ghost);
break;
case FRIGHTEN:
portalCheck();
setFrighten();
break;
case DEATH: setDeath(PINK, ghost);
break;
case LEAVE: leaveMove();
break;
case PEN: penMove();
break;
}
}
/**
* Boolean function which returns if the next tile in the ghosts direction is a wall
* dir the ghosts current direction
* return boolean value which is true if it is a wall in front of the ghost
*/
bool isWall(direction dir)
{
if (getFollowingTile(dir) == W || getFollowingTile(dir) == G) {
return true;
}
return false;
}
/**
* Boolean function which returns if the next tile in the ghosts direction is a wall (not including ghost gate)
* dir the ghosts current direction
* return boolean value which is true if it is a wall in front of the ghost
*/
bool isOnlyWall(direction dir)
{
if (getFollowingTile(dir) == W) {
return true;
}
return false;
}
/**
* Moves the ghosts up and down inside the ghost pen until they are told to leave
*/
void penMove()
{
if(ghostY <= 15.5f && currentDir == DOWN) {
if(ghostColour == PINK && moveType == PEN) {
moveType = LEAVE;
} else {
currentDir = UP;
}
} else if(ghostY >= 16.5f && currentDir == UP) {
currentDir = DOWN;
}
switch (currentDir) {
case UP:
ghostY += speed;
break;
case DOWN:
ghostY -= speed;
break;
}
}
/**
* Checks whether the ghost is currently on a portal tile. If they are, translate them to the other side of the maze respectively
*/
void portalCheck()
{
switch(getTile(ghostX, ghostY)){
case P: if(getGhostX() >= 26.5) {
ghostX = 1.0f;
} else {
ghostX = 26.5;
}
}
}
/**
* Method that allows the ghosts to move from the ghost pen and enter the maze, no matter where in the pen they are
*/
void leaveMove()
{
if (ghostX <= 13.55 && ghostX >= 13.45) { // Initially check they have the correct X coordinates
if (ghostY < 19.0f) { // If so move them up and out of the gate
currentDir = UP;
}
} else { // If they dont have the correct x coordinates
if (ghostX < 13.5f) { // Determine if they need to move left or right
currentDir = RIGHT;
} else if (ghostX > 13.5f) {
currentDir = LEFT;
}
}
if(ghostY >= 19.0f) {
currentDir = LEFT;
if(isAtCenter()){
moveType = SCATTER;
}
}
if(moveType == LEAVE) { // As on the final call to this method the moveType is changed they could still move 0.1f in a direciton when not needed, so necessary check for moveType
switch (currentDir) {
case LEFT:
ghostX -= speed;
break;
case UP:
ghostY += speed;
break;
case RIGHT:
ghostX += speed;
break;
case DOWN:
ghostY -= speed;
break;
}
}
}
/**
* Creates the vectors for the relevant target tiles depending on which mode the ghost is in and also what colour the ghost is
* ghostCol the colour of the ghost
* return vector containing the x and y coordinate of the target tile
*/
std::vector<int> getTargetTile(colour ghostCol)
{
std::vector<int> v;
switch(moveType) {
case CHASE:
switch (ghostCol) {
case RED:
v.push_back(pacman.getPacX());
v.push_back(pacman.getPacY());
break;
case BLUE:
switch(pacman.currentDir) {
case LEFT:
v.push_back(pacman.getPacX() - 2);
v.push_back(pacman.getPacY());
break;
case UP:
v.push_back(pacman.getPacX() );
v.push_back(pacman.getPacY() + 2);
break;
case RIGHT:
v.push_back(pacman.getPacX() + 2);
v.push_back(pacman.getPacY());
break;
case DOWN:
v.push_back(pacman.getPacX());
v.push_back(pacman.getPacY() - 2);
break;
default:
v.push_back(pacman.getPacX());
v.push_back(pacman.getPacY());
}
break;
case YELLOW:
v.push_back(0);
v.push_back(0);
break;
case PINK:
switch(pacman.currentDir) {
case LEFT:
v.push_back(pacman.getPacX() - 4);
v.push_back(pacman.getPacY());
break;
case UP:
v.push_back(pacman.getPacX() );
v.push_back(pacman.getPacY() + 4);
break;
case RIGHT:
v.push_back(pacman.getPacX() + 4);
v.push_back(pacman.getPacY());
break;
case DOWN:
v.push_back(pacman.getPacX());
v.push_back(pacman.getPacY() - 4);
break;
default:
v.push_back(pacman.getPacX());
v.push_back(pacman.getPacY());
}
break;
}
break;
case SCATTER:
switch (ghostColour) {
case RED:
v.push_back(100);
v.push_back(100);
break;
case BLUE:
v.push_back(20);
v.push_back(0);
break;
case YELLOW:
v.push_back(0);
v.push_back(0);
break;
case PINK:
v.push_back(0);
v.push_back(100);
break;
}
break;
case DEATH:
v.push_back(13.5);
v.push_back(19);
break;
}
return v;
}
};
#endif