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