-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghost.js
298 lines (258 loc) · 9.86 KB
/
ghost.js
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
var GhostState = {
DUMB: 0,
CHASING: 1,
EXPLORING: 3,
ECSTASY: 4
}
function Ghost(id, gx, gy, options) {
var defaults = {
direction: Directions.RIGHT,
color: Colors.GREY,
state: GhostState.DUMB,
clockwise: true,
speed: 1,
range: 6,
freezingTime: 0
}
options = (typeof options == "undefined") ? {} : options
for (var i in defaults) {
if (typeof options[i] == "undefined") {
this[i] = defaults[i]
} else {
this[i] = options[i]
}
}
this.id = id
this.gx = gx
this.gy = gy
this.game = undefined
this.initialState = JSON.parse(JSON.stringify(this))
}
Ghost.prototype.reset = function () {
for (var i in this.initialState) {
this[i] = this.initialState[i]
}
}
Ghost.prototype.onHitPlayer = function () {
this.state = GhostState.ECSTASY
this.freezingTime = Loop.lastTime + 3000
}
Ghost.prototype.dumbWalk = function () {
var tries = 0
while (true) {
var destX = this.x + Directions.DELTA[this.direction].dx * TILE / 8 * this.speed
var destY = this.y + Directions.DELTA[this.direction].dy * TILE / 8 * this.speed
var gridX = parseInt((this.x + Directions.DELTA[this.direction].dx * (HALF_TILE + 0.5)) / TILE)
var gridY = parseInt((this.y + Directions.DELTA[this.direction].dy * (HALF_TILE + 0.5)) / TILE)
if (!this.game.checkObstacle(gridX, gridY) && !this.game.checkOutOfBounds(gridX, gridY)) {
this.x = destX
this.y = destY
this.gx = gridX
this.gy = gridY
break
} else {
if (this.clockwise) {
Directions.nextDirection(this)
} else {
Directions.previousDirection(this)
}
}
tries++
if (tries > 4) {
break
}
}
}
Ghost.prototype.chaseWalk = function () {
var r = Math.random()
var destX, destY, gridX, gridY
if ((this.x - HALF_TILE) % TILE == 0 && (this.y - HALF_TILE) % TILE == 0) {
var distance = this.distanceToPlayer(this.gx, this.gy, this.game.player)
if (distance <= this.range) {
if (this.state != GhostState.CHASING) {
this.state = GhostState.CHASING
this.visitedTiles = new Grid(GRID_WIDTH, GRID_HEIGHT)
}
} else {
if (this.state == GhostState.CHASING) {
this.visitedTiles = undefined
}
this.state = GhostState.EXPLORING
}
if (this.state) {
if (this.state == GhostState.EXPLORING) {
if (r < 0.05) {
Directions.nextDirection(this)
} else if (r < 0.1) {
Directions.previousDirection(this)
}
} else if (this.state == GhostState.CHASING) {
this.setNextChasingDirection()
}
}
}
var tries = 0
while (true) {
destX = this.x + Directions.DELTA[this.direction].dx * TILE / 8 * this.speed
destY = this.y + Directions.DELTA[this.direction].dy * TILE / 8 * this.speed
gridX = parseInt((this.x + Directions.DELTA[this.direction].dx * (HALF_TILE + 0.5)) / TILE)
gridY = parseInt((this.y + Directions.DELTA[this.direction].dy * (HALF_TILE + 0.5)) / TILE)
if (!this.game.checkObstacle(gridX, gridY) &&
!this.game.checkEnemies(gridX, gridY, this) &&
!this.game.checkOutOfBounds(gridX, gridY)) {
this.x = destX
this.y = destY
if (this.state == GhostState.CHASING &&
(this.gx != gridX || this.gy != gridY)) {
var visit = this.visitedTiles.get(gridX, gridY)
if (!visit) {
this.visitedTiles.set(gridX, gridY, { count: 1, time: Loop.lastTime })
} else {
this.visitedTiles.set(gridX, gridY, { count: visit.count + 1, time: Loop.lastTime })
}
}
this.gx = gridX
this.gy = gridY
break
} else {
if (r < 0.5) {
Directions.nextDirection(this)
} else {
Directions.previousDirection(this)
}
}
tries++
if (tries > 4) {
break
}
}
}
Ghost.prototype.distanceToPlayer = function (x, y, player) {
var dx = player.gx - x
var dy = player.gy - y
return Math.sqrt(dx * dx + dy * dy)
}
Ghost.prototype.setNextChasingDirection = function () {
var nextX = this.gx
var nextY = this.gy
var smallestDistance = Number.MAX_SAFE_INTEGER
for (var x = -1; x <= 1; x++) {
for (var y = -1; y <= 1; y++) {
if ((x + y) * (x + y) != 1) { //consider only the cross
continue
}
var ex = this.gx + x
var ey = this.gy + y
if (ex > 0 && ex > 0
&& ex < GRID_WIDTH && ey < GRID_HEIGHT
&& !this.game.checkObstacle(ex, ey)) {
var distance = this.distanceToPlayer(ex, ey, this.game.player)
var visit = this.visitedTiles.get(ex, ey)
var visitCount = visit ? visit.count : 0
if (distance + visitCount < smallestDistance) {
nextX = ex
nextY = ey
smallestDistance = distance
}
}
}
}
var dx = nextX - this.gx
var dy = nextY - this.gy
if (dx * dx > dy * dy) {
if (dx > 0) {
this.direction = Directions.RIGHT
} else {
this.direction = Directions.LEFT
}
} else {
if (dy > 0) {
this.direction = Directions.DOWN
} else {
this.direction = Directions.UP
}
}
}
Ghost.prototype.ecstasy = function () {
if (Loop.lastTime <= this.freezingTime) {
//let the ghost center itself to the current tile
targetX = this.gx * TILE + HALF_TILE
targetY = this.gy * TILE + HALF_TILE
if (this.x != targetX || this.y != targetY) {
this.x += Directions.DELTA[this.direction].dx * TILE / 8 * this.speed
this.y += Directions.DELTA[this.direction].dy * TILE / 8 * this.speed
}
} else {
this.freezingTime = 0
this.state = GhostState.EXPLORING
}
}
Ghost.prototype.draw = function (ctx, scale) {
ctx.fillStyle = this.color
ctx.beginPath()
ctx.arc(this.x, this.y, scale / 2, Math.PI, 0)
ctx.fill()
ctx.fillRect(this.x - scale / 2, this.y - 1, scale, scale / 2)
if (this.state == GhostState.DUMB) {
ctx.fillStyle = "white"
ctx.beginPath()
ctx.arc(this.x + 0.70 * scale - scale / 2, this.y, scale / 5, 0, Math.PI)
ctx.arc(this.x + 0.30 * scale - scale / 2, this.y, scale / 5, 0, Math.PI)
ctx.fill()
ctx.fillStyle = "black"
ctx.beginPath()
ctx.arc(this.x + 0.70 * scale + scale / 2 * (1.0 + Directions.DELTA[this.direction].dx / 5) - scale,
this.y - 1 + 1 * (1.0 + Directions.DELTA[this.direction].dy / 5), scale / 9, 0, Math.PI)
ctx.arc(this.x + 0.30 * scale + scale / 2 * (1.0 + Directions.DELTA[this.direction].dx / 5) - scale,
this.y - 1 + 1 * (1.0 + Directions.DELTA[this.direction].dy / 5), scale / 9, 0, Math.PI)
ctx.fill()
} else if (this.state == GhostState.EXPLORING) {
ctx.fillStyle = "white"
ctx.beginPath()
ctx.arc(this.x + 0.70 * scale - scale / 2, this.y, scale / 4, 0, 2 * Math.PI)
ctx.arc(this.x + 0.30 * scale - scale / 2, this.y, scale / 4, 0, 2 * Math.PI)
ctx.fill()
ctx.fillStyle = "black"
ctx.beginPath()
ctx.arc(this.x + 0.70 * scale + scale / 2 * (1.0 + Directions.DELTA[this.direction].dx / 5) - scale,
this.y + 1 * (1.0 + Directions.DELTA[this.direction].dy / 5), scale / 8, 0, 2 * Math.PI)
ctx.arc(this.x + 0.30 * scale + scale / 2 * (1.0 + Directions.DELTA[this.direction].dx / 5) - scale,
this.y + 1 * (1.0 + Directions.DELTA[this.direction].dy / 5), scale / 8, 0, 2 * Math.PI)
ctx.fill()
} else if (this.state == GhostState.CHASING) {
ctx.fillStyle = "white"
ctx.beginPath()
ctx.arc(this.x + 0.70 * scale - scale / 2, this.y, scale / 4, Math.PI, 1.75 * Math.PI, true)
ctx.fill()
ctx.beginPath()
ctx.arc(this.x + 0.30 * scale - scale / 2, this.y, scale / 4, 1.25 * Math.PI, 0, true)
ctx.fill()
ctx.fillStyle = "black"
ctx.beginPath()
ctx.arc(this.x + 0.70 * scale + scale / 2 * (1.0 + Directions.DELTA[this.direction].dx / 5) - scale,
this.y + 1 * (1.0 + Directions.DELTA[this.direction].dy / 5), scale / 12, 0, 2 * Math.PI)
ctx.arc(this.x + 0.30 * scale + scale / 2 * (1.0 + Directions.DELTA[this.direction].dx / 5) - scale,
this.y + 1 * (1.0 + Directions.DELTA[this.direction].dy / 5), scale / 12, 0, 2 * Math.PI)
ctx.fill()
} else if (this.state === GhostState.ECSTASY) {
eyeSize = scale / 6 * (Math.sin(Loop.lastTime / 333 * Math.PI) / 2 + 0.5)
ctx.fillStyle = "white"
ctx.beginPath()
ctx.arc(this.x + 0.70 * scale - scale / 2, this.y, scale / 4, 0, 2 * Math.PI)
ctx.arc(this.x + 0.30 * scale - scale / 2, this.y, scale / 4, 0, 2 * Math.PI)
ctx.fill()
ctx.fillStyle = "black"
ctx.beginPath()
ctx.arc(this.x + 0.70 * scale - scale / 2, this.y, eyeSize, 0, 2 * Math.PI)
ctx.arc(this.x + 0.30 * scale - scale / 2, this.y, eyeSize, 0, 2 * Math.PI)
ctx.fill()
//smile
ctx.fillStyle = "black"
ctx.beginPath()
ctx.arc(this.x, this.y + scale / 3.5, scale / 7, Math.PI, 0, true)
ctx.fill()
}
//debug point
//ctx.fillStyle = "red"
//ctx.fillRect(this.x, this.y, TILE/8, TILE/8);
}