1 #include "Ghost1.h"
2 #include "time.h"
3 #include "math.h"
4 #include <string>
5 
6 class Ghost1;
7 
Ghost1(char * filename,int x,int y,int speed,AnimationFactory * af)8 Ghost1::Ghost1(char* filename, int x, int y, int speed, AnimationFactory* af) :
9 	Ghost(filename, x, y, speed, af){
10 	this->startx = x;
11 	this->starty = y;
12 	this->lev = lev;
13 	this->speed = speed;
14 
15 		direction = 2;
16 		xpos = 50 + (x*SIZET);
17 		ypos = 50 + (y*SIZET);
18 		nextypos = ypos;
19 		nextxpos = xpos;
20 		// annoyingly must currently be a divisor of 30
21 		//speed = 5;
22 
23 //		int n; int j;
24 //		for(j=0;j<SIZEY;j++){
25 //			for(n=0; n<SIZEX;n++){
26 //				this->lev.map[n][j] = lev.map[n][j];
27 //			}
28 //		}
29 
30 		//printf("%c\n",this->map[1][1]);
31 };
32 
33 //Ghost1::~Ghost1(){
34 //	int i;
35 //	for(i=0; i<frames.size(); i++){
36 //		SDL_FreeSurface(frames[i]);
37 //	}
38 //};
39 
40 // x & y are pacman's current grid positions
move(int x,int y)41 void Ghost1::move(int x, int y){
42 
43 	if(timeleft>0) timeleft--; else effect = ' ';
44 	if(respawntime>0){
45 		respawntime--;
46 	} else {
47 
48 		//int pacx = (x / SIZET) - 1;
49 		//int pacy = (y / SIZET) - 1;
50 
51 		// need to ensure it keeps going forwards unless interrupted
52 
53 		if(nextxpos == xpos && nextypos == ypos){
54 			// actual position matches wanted position, so
55 			// pacman is in square, so move
56 			int lft = 0;
57 			int rit = 0;
58 			int back = 0;
59 			lft = direction - 1;
60 			if(lft == 0) lft = 4;
61 			rit = direction + 1;
62 			if(rit == 5) rit = 1;
63 			back = direction - 2;
64 			if(back < 1) back = (0 - back) + (2 * direction);
65 
66 			int newdirection = 0; // test left right and set as newdirection
67 			// temporarily. Then test if straight on. If straight on is ok,
68 			// take 50-50 chance between straight on and a turn.
69 
70 			//if(testdirection(direction)==false){
71 				// cannot go forwards, try left and right...
72 				if(testdirection(lft) && !testdirection(rit)){
73 					// left is ok, right is blocked
74 					newdirection = lft;
75 				} else if(!testdirection(lft) && testdirection(rit)){
76 					// right is ok, left is blocked
77 					newdirection = rit;
78 				} else if(!testdirection(lft) && !testdirection(rit)){
79 					// left and right are blocked
80 					// ...go backwards
81 					newdirection = back;
82 				} else if(testdirection(lft) && testdirection(rit)){
83 					// left / right both ok. pick one at random.
84 					int rn = rand()%2;
85 					// random between 0 and 1 inclusive
86 					if(rn==1){
87 						newdirection = lft;
88 					} else {
89 						newdirection = rit;
90 					}
91 				}
92 
93 			if(!testdirection(direction)){
94 				direction = newdirection;
95 			} else if(testdirection(direction) && !testdirection(rit)
96 						&& !testdirection(lft)){
97 				// only forward and back are available, go forwards.
98 				direction = direction;
99 			} else {
100 				// 50% move forwards, 50% turn
101 				int rn = rand()%2;
102 				if(rn==1){
103 					direction = newdirection;
104 				} else {
105 					direction = direction;
106 				}
107 			}
108 
109 			int ax = (xpos / SIZET) - 1;
110 			int ay = (ypos / SIZET) - 1;
111 
112 			switch(direction){
113 				case 1:	nextypos = lev.map[ax][ay-1].t; break;
114 				case 2: nextxpos = lev.map[ax+1][ay].l; break;
115 				case 3: nextypos = lev.map[ax][ay+1].t; break;
116 				case 4: nextxpos = lev.map[ax-1][ay].l; break;
117 			};
118 
119 		} else if(nextxpos!=xpos){
120 			switch(direction){
121 				case 4: xpos = xpos - speed; break;
122 				case 2: xpos = xpos + speed; break;
123 			};
124 		} else if(nextypos!=ypos){
125 			switch(direction){
126 				case 1: ypos = ypos - speed; break;
127 				case 3: ypos = ypos + speed; break;
128 			};
129 		}
130 	}
131 };
132 
133