1 #include "Ghost2.h"
2 #include "time.h"
3 #include "math.h"
4 #include <string>
5 
6 class Ghost2;
7 
Ghost2(char * filename,int x,int y,int speed,AnimationFactory * af)8 Ghost2::Ghost2(char* filename, int x, int y, int speed, AnimationFactory* af) : Ghost(filename,x,y,speed, af){
9 
10 	this->speed = speed;
11 	this->startx = x;
12 	this->starty = y;
13 	direction = 2;
14 	xpos = 50 + (x*SIZET);
15 	ypos = 50 + (y*SIZET);
16 	nextypos = ypos;
17 	nextxpos = xpos;
18 	// annoyingly must currently be a divisor of 30
19 	//speed = 5;
20 
21 	int n; int j;
22 	for(j=0;j<SIZEY;j++){
23 		for(n=0; n<SIZEX;n++){
24 			this->lev.map[n][j] = lev.map[n][j];
25 		}
26 	}
27 
28 	//printf("%c\n",this->map[1][1]);
29 
30 
31 };
32 
33 //Ghost2::~Ghost2(){
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 Ghost2::move(int x, int y){
42 	if(timeleft>0) timeleft--; else effect = ' ';
43 	if(respawntime>0){
44 		respawntime--;
45 	} else {
46 	// pacman's X / Y
47 	int pacx = (x / SIZET) - 1;
48 	int pacy = (y / SIZET) - 1;
49 	// ghost's X / Y
50 	int ax = (xpos / SIZET) - 1;
51 	int ay = (ypos / SIZET) - 1;
52 
53 	bool infront = false; // pac is above
54 	bool left = false; // pac is left
55 
56 	// determine which direction pacman is in
57 	switch(direction){
58 		case 1: if(pacy < ay){
59 							infront = true;
60 						} else {
61 							infront = false;
62 						}
63 						if(pacx < ax){
64 							left = true;
65 						} else {
66 							left = false;
67 						} break;
68 		case 2: if(pacy < ay){
69 							left = true;
70 						} else {
71 							left = false;
72 						}
73 						if(pacx < ax){
74 							infront = false;
75 						} else {
76 							infront = true;
77 						} break;
78 		case 3:	if(pacy < ay){
79 							infront = false;
80 						} else {
81 							infront = true;
82 						}
83 						if(pacx < ax){
84 							left = false;
85 						} else {
86 							left = true;
87 						} break;
88 		case 4: if(pacy < ay){
89 							left = false;
90 						} else {
91 							left = true;
92 						}
93 						if(pacx < ax){
94 							infront = true;
95 						} else {
96 							infront = false;
97 						} break;
98 	};
99 
100 
101 	// need to ensure it keeps going forwards unless interrupted
102 
103 	if(nextxpos == xpos && nextypos == ypos){
104 		// actual position matches wanted position, so
105 		// pacman is in square, so move
106 		int lft = 0;
107 		int rit = 0;
108 		int back = 0;
109 		lft = direction - 1;
110 		if(lft == 0) lft = 4;
111 		rit = direction + 1;
112 		if(rit == 5) rit = 1;
113 		back = direction - 2;
114 		if(back < 1) back = (0 - back) + (2 * direction);
115 
116 		int newdirection = 0; // test left right and set as newdirection
117 		// temporarily. Then test if straight on. If straight on is ok,
118 		// take 50-50 chance between straight on and a turn.
119 
120 		//if(testdirection(direction)==false){
121 			// cannot go forwards, try left and right...
122 			if(testdirection(lft) && !testdirection(rit)){
123 				// left is ok, right is blocked
124 				newdirection = lft;
125 			} else if(!testdirection(lft) && testdirection(rit)){
126 				// right is ok, left is blocked
127 				newdirection = rit;
128 			} else if(!testdirection(lft) && !testdirection(rit)){
129 				// left and right are blocked
130 				// ...go backwards
131 				newdirection = back;
132 			} else if(testdirection(lft) && testdirection(rit)){
133 				// left / right both ok. pick one at random.
134 				int rn = rand()%8;
135 				// random between 0 and 1 inclusive
136 
137 				int biaseddirection = 0;
138 				int unbiaseddirection = 0;
139 				if(left){ biaseddirection = lft; unbiaseddirection = rit; }
140 				if(!left){ biaseddirection = rit; unbiaseddirection = lft; }
141 
142 				if(rn==0){
143 					newdirection = unbiaseddirection;
144 				} else {
145 					newdirection = biaseddirection;
146 				}
147 			}
148 
149 		if(!testdirection(direction)){
150 			direction = newdirection;
151 		} else if(testdirection(direction) && !testdirection(rit)
152 					&& !testdirection(lft)){
153 			// only forward and back are available, go forwards.
154 			direction = direction;
155 		} else {
156 			// 50% move forwards, 50% turn
157 			int rn = rand()%8;
158 
159 			int biaseddirection = 0;
160 			int unbiaseddirection = 0;
161 			if(infront){ biaseddirection = direction; unbiaseddirection = newdirection; }
162 			if(!infront){ biaseddirection = newdirection; unbiaseddirection = direction; }
163 
164 			if(rn==0){
165 				direction = unbiaseddirection;
166 			} else {
167 				direction = biaseddirection;
168 			}
169 		}
170 
171 		switch(direction){
172 			case 1:	nextypos = lev.map[ax][ay-1].t; break;
173 			case 2: nextxpos = lev.map[ax+1][ay].l; break;
174 			case 3: nextypos = lev.map[ax][ay+1].t; break;
175 			case 4: nextxpos = lev.map[ax-1][ay].l; break;
176 		};
177 
178 	} else if(nextxpos!=xpos){
179 		switch(direction){
180 			case 4: xpos = xpos - speed; break;
181 			case 2: xpos = xpos + speed; break;
182 		};
183 	} else if(nextypos!=ypos){
184 		switch(direction){
185 			case 1: ypos = ypos - speed; break;
186 			case 3: ypos = ypos + speed; break;
187 		};
188 	}
189 }
190 };
191 
192