forked from dogeared/pacman
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathghost.js
More file actions
313 lines (253 loc) · 7.92 KB
/
ghost.js
File metadata and controls
313 lines (253 loc) · 7.92 KB
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
import { PacManConfig } from './pacman_config.js';
function Ghost(game, map, colour) {
var position = null,
UP = 3,
LEFT = 2,
DOWN = 1,
RIGHT = 11,
direction = null,
eatable = null,
eaten = null,
due = null,
user = undefined,
mode = Ghost.CHILL;
function getNewCoord(dir, current) {
var speed = isVunerable() ? 1 : isHidden() ? 4 : 2,
xSpeed = (dir === LEFT && -speed || dir === RIGHT && speed || 0),
ySpeed = (dir === DOWN && speed || dir === UP && -speed || 0);
return {
"x": addBounded(current.x, xSpeed),
"y": addBounded(current.y, ySpeed)
};
};
/* Collision detection(walls) is done when a ghost lands on an
* exact block, make sure they dont skip over it
*/
function addBounded(x1, x2) {
var rem = x1 % 10,
result = rem + x2;
if (rem !== 0 && result > 10) {
return x1 + (10 - rem);
} else if (rem > 0 && result < 0) {
return x1 - rem;
}
return x1 + x2;
};
function isVunerable() {
return eatable !== null;
};
function isDangerous() {
return eaten === null;
};
function isHidden() {
return eatable === null && eaten !== null;
};
function getRandomDirection() {
var moves = (direction === LEFT || direction === RIGHT)
? [UP, DOWN] : [LEFT, RIGHT];
return moves[Math.floor(Math.random() * 2)];
};
function reset(ghostNum) {
eaten = null;
eatable = null;
position = { "x": 90, "y": 80 };
position.x += (ghostNum < 2) ? (ghostNum+1) * -10 : ghostNum * 10;
direction = getRandomDirection();
due = getRandomDirection();
};
function onWholeSquare(x) {
return x % 10 === 0;
};
function oppositeDirection(dir) {
return dir === LEFT && RIGHT ||
dir === RIGHT && LEFT ||
dir === UP && DOWN || UP;
};
function makeEatable() {
direction = oppositeDirection(direction);
eatable = game.getTick();
};
function eat() {
eatable = null;
eaten = game.getTick();
};
function pointToCoord(x) {
return Math.round(x / 10);
};
function nextSquare(x, dir) {
var rem = x % 10;
if (rem === 0) {
return x;
} else if (dir === RIGHT || dir === DOWN) {
return x + (10 - rem);
} else {
return x - rem;
}
};
function onGridSquare(pos) {
return onWholeSquare(pos.y) && onWholeSquare(pos.x);
};
function secondsAgo(tick) {
return (game.getTick() - tick) / PacManConfig.FPS;
};
function getColour() {
if (eatable) {
if (secondsAgo(eatable) > 5) {
return game.getTick() % 20 > 10 ? "#FFFFFF" : "#0000BB";
} else {
return "#0000BB";
}
} else if (eaten) {
return "#222";
}
return colour;
};
function draw(ctx) {
var s = map.blockSize,
top = (position.y / 10) * s,
left = (position.x / 10) * s;
if (eatable && secondsAgo(eatable) > 8) {
eatable = null;
}
if (eaten && secondsAgo(eaten) > 3) {
eaten = null;
}
var tl = left + s;
var base = top + s - 3;
var inc = s / 10;
var high = game.getTick() % 10 > 5 ? 3 : -3;
var low = game.getTick() % 10 > 5 ? -3 : 3;
ctx.fillStyle = getColour();
ctx.beginPath();
ctx.moveTo(left, base);
ctx.quadraticCurveTo(left, top, left + (s / 2), top);
ctx.quadraticCurveTo(left + s, top, left + s, base);
// Wavy things at the bottom
ctx.quadraticCurveTo(tl - (inc * 1), base + high, tl - (inc * 2), base);
ctx.quadraticCurveTo(tl - (inc * 3), base + low, tl - (inc * 4), base);
ctx.quadraticCurveTo(tl - (inc * 5), base + high, tl - (inc * 6), base);
ctx.quadraticCurveTo(tl - (inc * 7), base + low, tl - (inc * 8), base);
ctx.quadraticCurveTo(tl - (inc * 9), base + high, tl - (inc * 10), base);
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.fillStyle = "#FFF";
ctx.arc(left + 6, top + 6, s / 6, 0, 300, false);
ctx.arc((left + s) - 6, top + 6, s / 6, 0, 300, false);
ctx.closePath();
ctx.fill();
var f = s / 12;
var off = {};
off[RIGHT] = [f, 0];
off[LEFT] = [-f, 0];
off[UP] = [0, -f];
off[DOWN] = [0, f];
ctx.beginPath();
ctx.fillStyle = "#000";
ctx.arc(left + 6 + off[direction][0], top + 6 + off[direction][1],
s / 15, 0, 300, false);
ctx.arc((left + s) - 6 + off[direction][0], top + 6 + off[direction][1],
s / 15, 0, 300, false);
ctx.closePath();
ctx.fill();
};
function pane(pos) {
if (pos.y === 100 && pos.x >= 190 && direction === RIGHT) {
return { "y": 100, "x": -10 };
}
if (pos.y === 100 && pos.x <= -10 && direction === LEFT) {
return position = { "y": 100, "x": 190 };
}
return false;
};
function moveChill(ctx) {
var oldPos = position,
onGrid = onGridSquare(position),
npos = null;
if (due !== direction) {
npos = getNewCoord(due, position);
if (onGrid &&
map.isFloorSpace({
"y": pointToCoord(nextSquare(npos.y, due)),
"x": pointToCoord(nextSquare(npos.x, due))
})) {
direction = due;
} else {
npos = null;
}
}
if (npos === null) {
npos = getNewCoord(direction, position);
}
if (onGrid &&
map.isWallSpace({
"y": pointToCoord(nextSquare(npos.y, direction)),
"x": pointToCoord(nextSquare(npos.x, direction))
})) {
due = getRandomDirection();
return moveChill(ctx);
}
position = npos;
var tmp = pane(position);
if (tmp) {
position = tmp;
}
due = getRandomDirection();
return {
"new": position,
"old": oldPos
};
}
function moveRadar(ctx) {
if (
!user || isVunerable() || isHidden() ||
(position.y === 80 && position.x > 80 && position.x < 100)
) {
return moveChill(ctx);
}
// console.log('user position: ' + JSON.stringify(user.getPosition()));
let userPos = user.getPosition();
let dy = Math.abs(userPos.y - position.y);
let dx = Math.abs(userPos.x - position.x);
if (dy > dx) { // try UP/DOWN first
due = (userPos.y > position.y) ? DOWN : UP
} else { // try LEFT/RIGHT first
due = (userPos.x > position.x) ? RIGHT : LEFT
}
return moveChill(ctx);
}
function move(ctx) {
if (mode === Ghost.CHILL) {
return moveChill(ctx);
} else if (mode === Ghost.RADAR) {
return moveRadar(ctx);
}
};
function getMode() {
return mode;
}
function setMode(newMode) {
if (newMode === Ghost.RADAR || newMode === Ghost.CHILL) {
mode = newMode;
}
console.log('new mode: ' + (newMode == Ghost.RADAR ? 'RADAR' : 'CHILL'));
}
function setUser(newUser) {
user = newUser;
}
return {
eat: eat,
isVunerable: isVunerable,
isDangerous: isDangerous,
makeEatable: makeEatable,
reset: reset,
move: move,
draw: draw,
getMode: getMode,
setMode: setMode,
setUser: setUser
};
};
Ghost.CHILL = 0;
Ghost.RADAR = 1;
export { Ghost };