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
|
import { getMap } from "../map.js";
import { Map, Vec2, GhostType, GameState, SpawnIndex, Player, Rotation, GhostState, Tile, Ghost } from "../types.js";
import { random } from "./logic.js";
import { MOVE_SPEED, roundPos, isStablePos, getTile, getTileFrontWithRot, incrementPos } from "./movement.js";
const diff = (a: Vec2, b:Vec2): Vec2 => {
return {x: a.x - b.x, y: a.y - b.y}
}
const dist = (a: Vec2, b: Vec2): number => {
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2))
}
const trans = (pos: Vec2, rot: Rotation, dist: number): Vec2 => {
switch (rot) {
case Rotation.NORTH:
case Rotation.NOTHING:
return {x: pos.x - dist, y: pos.y - dist}
case Rotation.EAST:
return {x: pos.x + dist, y: pos.y}
case Rotation.SOUTH:
return {x: pos.x, y: pos.y + dist}
case Rotation.WEST:
return {x: pos.x - dist, y: pos.y}
}
}
const getNearestPlayer = (state: GameState, pos: Vec2): Player => {
let min = undefined;
let nearest = undefined;
for (let id in state.players) {
let player = state.players[id];
if (!id) continue;
let d = dist(player.pos, pos)
if (!min || min > d) {
min = d
nearest = player
}
}
return nearest
}
const pickTargetScatter = (state: GameState, type: GhostType): Vec2 => {
let map = getMap(state.mapId)
switch (type) {
case GhostType.BLINKY:
return {x: 0, y: -1}
case GhostType.PINKY:
return {x: map.width - 1, y: -1}
case GhostType.INKY:
return {x: map.width - 1, y: map.height}
case GhostType.CLYDE:
return {x: 0, y: map.height}
}
}
const pickTargetChase = (state: GameState, type: GhostType): Vec2 => {
let ghost = state.ghosts[type]
let player = getNearestPlayer(state, ghost.pos)
switch (type) {
case GhostType.BLINKY:
return {x: player.pos.x, y: player.pos.y}
case GhostType.PINKY:
return trans(player.pos, player.moveRotation, 2)
case GhostType.INKY:
let target = trans(player.pos, player.moveRotation, 1)
let vec = diff(target, state.ghosts[GhostType.BLINKY].pos)
return {x: target.x + vec.x, y: target.y + vec.y}
case GhostType.CLYDE:
if (dist(ghost.pos, player.pos) > 8)
return {x: player.pos.x, y: player.pos.y}
else
return pickTargetScatter(state, type)
}
}
const pickTarget = (state: GameState, map: Map, type: GhostType): Vec2 => {
let ghost: Ghost = state.ghosts[type]
switch (ghost.state) {
case GhostState.SCATTER:
return pickTargetScatter(state, type)
case GhostState.CHASE:
return pickTargetChase(state, type)
case GhostState.EATEN:
return structuredClone(map.spawns[SpawnIndex.GHOST_SPAWN])
case GhostState.SCARED:
return {
x: random(state) % map.width,
y: random(state) % map.height
}
}
}
const flipRot = (rot: Rotation) => {
switch (rot) {
case Rotation.NORTH:
return Rotation.SOUTH
case Rotation.SOUTH:
return Rotation.NORTH
case Rotation.EAST:
return Rotation.WEST
case Rotation.WEST:
return Rotation.EAST
}
}
const updateGhost = (state: GameState, map: Map, type: GhostType) => {
let ghost: Ghost = state.ghosts[type]
if (!ghost) {
ghost = {
pos: structuredClone(map.spawns[SpawnIndex.GHOST_SPAWN]),
target: structuredClone(map.spawns[SpawnIndex.GHOST_SPAWN]),
type,
state: GhostState.SCARED,
currentDirection: Rotation.EAST,
}
state.ghosts[type] = ghost
}
if (isStablePos(ghost.pos)) {
ghost.pos = roundPos(ghost.pos)
let front = getTileFrontWithRot(map, ghost.pos, ghost.currentDirection) == Tile.WALL
let north = getTile(map, ghost.pos, 0, -1) != Tile.WALL
let east = getTile(map, ghost.pos, 1, 0) != Tile.WALL
let south = getTile(map, ghost.pos, 0, 1) != Tile.WALL
let west = getTile(map, ghost.pos, -1, 0) != Tile.WALL
let isIntersection =
(north && east) ||
(east && south) ||
(south && west) ||
(west && north)
if (!isIntersection && front) {
ghost.currentDirection = flipRot(ghost.currentDirection)
} else if (isIntersection) {
let target = pickTarget(state, map, type)
ghost.target = target
let newRot = ghost.currentDirection
let min = undefined
if (north && ghost.currentDirection !== Rotation.SOUTH) {
let d = dist({x: ghost.pos.x, y: ghost.pos.y - 1}, target)
if (!min || min > d) {
min = d
newRot = Rotation.NORTH
}
}
if (east && ghost.currentDirection !== Rotation.WEST) {
let d = dist({x: ghost.pos.x + 1, y: ghost.pos.y}, target)
if (!min || min > d) {
min = d
newRot = Rotation.EAST
}
}
if (south && ghost.currentDirection !== Rotation.NORTH) {
let d = dist({x: ghost.pos.x, y: ghost.pos.y + 1}, target)
if (!min || min > d) {
min = d
newRot = Rotation.SOUTH
}
}
if (west && ghost.currentDirection !== Rotation.EAST) {
let d = dist({x: ghost.pos.x - 1, y: ghost.pos.y}, target)
if (!min || min > d) {
min = d
newRot = Rotation.WEST
}
}
ghost.currentDirection = newRot
}
}
incrementPos(ghost.pos, ghost.currentDirection, MOVE_SPEED)
}
export const updateGhosts = (state: GameState) => {
let map = getMap(state.mapId)
if (!map) return
updateGhost(state, map, GhostType.BLINKY)
updateGhost(state, map, GhostType.PINKY)
updateGhost(state, map, GhostType.INKY)
updateGhost(state, map, GhostType.CLYDE)
}
|