-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghost.pde
431 lines (357 loc) · 10.8 KB
/
ghost.pde
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
PVector ghostStartLoc = new PVector(10,13,0);
PVector ghostBoxMid = new PVector(10,12,0);
PVector ghostBoxLeft = new PVector(9,12,0);
PVector ghostBoxRight = new PVector(11,12,0);
float curGhostSpeed;
float FRIGHTENED_SPEED = PAC_SPEED*.30;
float TUNNEL_SPEED = PAC_SPEED*.55;
float DEAD_SPEED = 5.0/60.0;
color frightenedC = #0032F2;
int frightenedTime;
int frightenedTimer = 0;
class Clyde extends Ghost {
Clyde() {
fillC = #FF7300;
homeBase = new PVector(-2,-2); // upper left
ghostBox = ghostBoxRight;
initialize();
}
void initialize() {
ghostInitialize();
loc.set(ghostBox);
moving = false;
inBox = true;
norm_speed = curGhostSpeed;
}
void determineChaseTarget() {
// if less than 8 tiles from pacman, target hometile
// else same as blinky
int targetX, targetY;
int distance = 0;
distance += abs(loc.x - pac.loc.x);
distance += abs(loc.y - pac.loc.y);
if (distance < 6) {
target.set(homeBase);
} else {
target.set(blinky.target);
}
}
}
class Inky extends Ghost {
Inky() {
fillC = #00CEFF;
homeBase = new PVector(mapW+2,-1); // upper right
ghostBox = ghostBoxLeft;
initialize();
}
void initialize() {
ghostInitialize();
loc.set(ghostBox);
moving = false;
inBox = true;
norm_speed = curGhostSpeed;
}
void determineChaseTarget() {
// target is 4 squares ahead of pac
int targetX, targetY;
int pacTileX = int(pac.loc.x);
int pacTileY = int(pac.loc.y);
pacTileX += pac.direction.x*2;
pacTileY += pac.direction.y*2;
targetX = int(2*(pacTileX - blinky.loc.x));
targetY = int(2*(pacTileY - blinky.loc.y));
target.set(float(targetX),float(targetY),0.0);
}
}
class Pinky extends Ghost {
Pinky() {
fillC = #FF74B7;
homeBase = new PVector(-1,mapH+2); // bottom left
ghostBox = ghostBoxMid;
initialize();
}
void initialize() {
ghostInitialize();
loc.set(ghostBox);
moving = false; // start right away
inBox = true;
norm_speed = curGhostSpeed;
//moveRequested = 3; // SOUTH
}
void determineChaseTarget() {
// target is 4 squares ahead of pac
int targetX = int(pac.loc.x);
int targetY = int(pac.loc.y);
targetX += pac.direction.x*4;
targetY += pac.direction.y*4;
target.set(float(targetX),float(targetY),0.0);
}
}
class Blinky extends Ghost {
Blinky() {
initialize();
fillC = #F20000;
homeBase = new PVector(mapW+1,-1); // upper right
ghostBox = new PVector(10,14);
}
void initialize() {
ghostInitialize();
loc.set(ghostStartLoc);
moving = false; // start right away
inBox = false;
moveRequested = 4;
norm_speed = curGhostSpeed;
}
void determineChaseTarget() {
// target is pac's location
target.set(pac.loc);
}
}
class Ghost extends Man {
PVector target;
PVector homeBase;
PVector ghostBox;
int mode; // scatter-0, chase-1, frightened-2
boolean frightened;
boolean dead;
color fillC, curFill;
float norm_speed;
//int frightenedTimer = 0;
boolean reverseGhost;
Ghost() {
ghostInitialize();
isGhost = true;
}
void ghostInitialize() {
moveRequested = 0;
moving = false;
mode = 0;
dead = false;
target = new PVector(0,0,0);
loc = new PVector(0,0,0);
//direction = new PVector(-1,0); // edited
direction = new PVector(0,0);
frightened = false;
reverseGhost = false;
}
void drawGhost() {
PVector drawLoc = getLoc(loc.y,loc.x);
if (!dead) {
ellipseMode(CENTER);
stroke(0);
if (!frightened)
fill(fillC);
else
fill(curFill);
ellipse(drawLoc.x, drawLoc.y, pacDiameter-2, pacDiameter-2);
rectMode(CENTER);
noStroke();
rect(drawLoc.x, drawLoc.y+pacDiameter/4, pacDiameter-2, pacDiameter/2);
}
drawEyes(drawLoc);
}
void drawEyes(PVector drawLoc) {
fill(255);
noStroke();
ellipseMode(CENTER);
// draw eyes
ellipse(drawLoc.x-3, drawLoc.y, 5, 8); // left
ellipse(drawLoc.x+3, drawLoc.y, 5, 8); // right
// draw pupils
if (!frightened) {
fill(0);
ellipse(drawLoc.x-3+2*direction.x, drawLoc.y+3*direction.y, 2, 2); // left
ellipse(drawLoc.x+3+2*direction.x, drawLoc.y+3*direction.y, 2, 2); // right
}
}
void updateCondition() {
updateMove();
if (!inBox)
updateMoveDecision();
// handle speed
if (frightened) {
speed = FRIGHTENED_SPEED;
} else if (dead) {
speed = DEAD_SPEED;
} else {
if (inTunnel) {
speed = TUNNEL_SPEED;
} else {
speed = norm_speed;
}
}
// see if we need to stop frightened
if (frightened) {
if (millis() - frightenedTimer > frightenedTime) {
frightened = false;
ghostMult = 1;
} else if (frightenedTime - (millis() - frightenedTimer) < 2000) {
float fraction = float((frightenedTime - (millis() - frightenedTimer)))/2000.0;
curFill = avgColor(frightenedC, fillC, 1-fraction);
}
}
// if we are in the box we need to move out
if (inBox && moving && !dead) {
if (abs(PVector.dist(loc,ghostBoxLeft)) < 2*speed && direction.x==0 && direction.y==0) {
moveRequested = 2;
} else if (abs(PVector.dist(loc,ghostBoxRight)) < 2*speed && direction.x==0 && direction.y==0) {
moveRequested = 4;
} else if (abs(PVector.dist(loc,ghostBoxMid)) < 2*speed && direction.y==0) {
moveRequested = 3;
} else if (abs(PVector.dist(loc,ghostStartLoc)) < 2*speed) {
// move out of the box
loc.x = round(loc.x);
loc.y = round(loc.y);
inBox = false;
//direction.x = 0; direction.y = 0;
moveRequested = 4;
}
}
// check for pacman collision
if (!dead) {
float pacDist = loc.dist(pac.loc);
if (pacDist <= 8*PAC_SPEED) {
if (frightened) {
// ghost die!
killGhost(this);
// dead = true;
// frightened = false;
// score += 500;
} else {
// pacman die!
pacDie();
}
}
}
// move dead ghost back to home
if (dead) {
if (loc.dist(ghostBoxMid) < 2*PAC_SPEED) {
// if close enough to home, make alive again! rawr payback!
direction.y = 1;
dead = false;
loc.set(ghostBoxMid);
} else if (loc.dist(ghostStartLoc) < 2*PAC_SPEED) {
inBox = true;
direction.y = -1;
direction.x = 0;
loc.x = ghostStartLoc.x;
}
}
}
void frighten() {
if (moving) {
frightened = true;
reverseGhost = true;
frightenedTimer = millis();
curFill = frightenedC;
}
}
void kill() {
dead = true;
target.set(ghostBox);
}
void determineChaseTarget() {
// empty - defined in subclass
}
void determineTarget() {
if (frightened) {
// frightened
target.set(int(random(0,mapW)), int(random(0,mapH)), 0.0);
} else {
if (mode == 0) {
// SCATTER
target.set(homeBase);
} else if (mode == 1) {
// CHASE
determineChaseTarget(); // to be filled by subclass
}
}
}
void updateMoveDecision() {
if (!inTunnel && !dead) {
determineTarget();
if (reverseGhost) {
if (abs(loc.x-int(loc.x)) < 2*speed && abs(loc.y-int(loc.y)) < 2*speed) {
if (lastMove == 1)
moveRequested = 3;
else if (lastMove == 2)
moveRequested = 4;
else if (lastMove == 3)
moveRequested = 1;
else
moveRequested = 2;
reverseGhost = false;
return;
} else {
return;
}
}
inTunnel = loc.x < 0 || loc.x >= mapW;
if (moveRequested == 0 && !inTunnel && !dead) {
// no decision made for a move - update moveRequest
int curX = round(loc.x);
int curY = round(loc.y);
PVector nextTile = new PVector(curX+direction.x, curY+direction.y, 0.0);
int nextX = int(nextTile.x);
int nextY = int(nextTile.y);
if (nextY != tunnel1.y || (nextX > tunnel1.x && nextX < tunnel2.x)) {
if (level[nextY][nextX].numNeighbors > 2) {
// make decision based on target tile
int bestMoveInd=0; float minDist = 9999;
for (int i=0; i<level[nextY][nextX].numNeighbors; i++) {
// choose best move based on shortest euclidean distance to
// target tile
PVector thisTile = resultTile(nextTile, level[nextY][nextX].neighbor[i]);
if (curX != round(thisTile.x) || curY != round(thisTile.y)) {
float distance = PVector.dist(thisTile, target);
// distance thru tunnel is not the same
if (thisTile.dist(tunnel1) < .5 ) {
distance = PVector.dist(target, tunnel2);
} else if(thisTile.dist(tunnel2) < .5) {
distance = PVector.dist(target, tunnel1);
}
if (distance < minDist) {
minDist = distance;
bestMoveInd = i;
}
}
}
int bestMove = level[nextY][nextX].neighbor[bestMoveInd];
moveRequested = bestMove;
} else if (level[nextY][nextX].numNeighbors == 2) {
// take the option that doesnt reverse you
PVector thisTile = resultTile(nextTile, level[nextY][nextX].neighbor[0]);
if (curX == round(thisTile.x) && curY == round(thisTile.y)) {
// if this is current tile, use 2nd option
moveRequested = level[nextY][nextX].neighbor[1];
} else {
// otherwise use first option
moveRequested = level[nextY][nextX].neighbor[0];
}
} else {
// get out of a dead end
if (direction.x == 0 && direction.y == 0)
moveRequested = level[nextY][nextX].neighbor[0];
}
} // end if nextTile != either tunnel
}// end if move requested = 0
}// end if not inTunnel and not dead
else if (dead) {
// move ghost back to home
moveRequested = ghostDirections[int(loc.y)][int(loc.x)];
}
}// end void update
}// end class Ghost
color avgColor(color color1, color color2, float percent) {
float r1,g1,b1,r2,g2,b2,r3,g3,b3;
r1 = red(color1);
g1 = green(color1);
b1 = blue(color1);
r2 = red(color2);
g2 = green(color2);
b2 = blue(color2);
r3 = percent*r2 + (1-percent)*r1;
g3 = percent*g2 + (1-percent)*g1;
b3 = percent*b2 + (1-percent)*b1;
return color(r3,g3,b3);
}