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
|
import { getMap } from "../map.js"
import { Vec2, Map, Rotation, Key, Player, GameState, Tile } from "../types.js"
export const MOVE_SPEED = .08333
export const roundPos = (pos: Vec2): Vec2 => {
return {x: Math.round(pos.x), y: Math.round(pos.y)}
}
export const isStablePos = (pos: Vec2): boolean => {
let rpos = roundPos(pos)
return Math.abs(rpos.x - pos.x) < .05 && Math.abs(rpos.y - pos.y) < .05
}
export const getTile = (
map: Map,
pos: Vec2,
ox: number,
oy: number
): number => {
let x = Math.round(pos.x + ox)
let y = Math.round(pos.y + oy)
if (x < 0 || x >= map.width || y < 0 || y >= map.height) return Tile.WALL
return map.data[y * map.width + x]
}
export const getTileFrontWithRot = (
map: Map,
pos: Vec2,
rot: Rotation
): number => {
let collider = 1
switch(rot) {
case Rotation.NORTH:
collider = getTile(map, pos, 0, -.51)
break
case Rotation.SOUTH:
collider = getTile(map, pos, 0, .51)
break
case Rotation.WEST:
collider = getTile(map, pos, -.51, 0)
break
case Rotation.EAST:
collider = getTile(map, pos, .51, 0)
break
}
return collider
}
const getRot = (key: Key): Rotation => {
switch (key) {
case Key.UP: return Rotation.NORTH
case Key.DOWN: return Rotation.SOUTH
case Key.LEFT: return Rotation.WEST
case Key.RIGHT: return Rotation.EAST
case Key.NOTHING: return Rotation.NOTHING
}
}
export const incrementPos = (
pos: Vec2,
rot: Rotation,
speed: number
): void => {
switch (rot) {
case Rotation.NORTH:
pos.y -= speed
break
case Rotation.SOUTH:
pos.y += speed
break
case Rotation.WEST:
pos.x -= speed
break
case Rotation.EAST:
pos.x += speed
break
}
}
const updateMovementForPlayer = (
map: Map,
player: Player,
inputKey: Key
) => {
let inputRot = getRot(inputKey)
let moveRot = player.moveRotation
let currentPosition = player.pos
let turningFrontTile = getTileFrontWithRot(map, currentPosition, inputRot)
if (turningFrontTile == Tile.WALL || turningFrontTile == Tile.GHOST_WALL) {
inputRot = Rotation.NOTHING
}
let turning = inputRot != Rotation.NOTHING && inputRot != moveRot
player.inputRotation = inputRot
if (turning && isStablePos(currentPosition)) {
currentPosition = roundPos(currentPosition)
player.moveRotation = inputRot
moveRot = inputRot
}
let movePos = structuredClone(currentPosition)
incrementPos(movePos, moveRot, MOVE_SPEED)
let frontTile = getTileFrontWithRot(map, currentPosition, moveRot)
if (frontTile != Tile.WALL && frontTile != Tile.GHOST_WALL) {
player.pos = movePos
player.moving = true
} else {
player.pos = roundPos(currentPosition)
player.moving = false
}
}
export const updateMovement = (data: GameState) => {
let map = getMap(data.mapId)
if (!map) return
for (const id in data.players) {
const player = data.players[id]
if(!player) {
continue
}
let inputKey = data.input[id]
updateMovementForPlayer(map, player, inputKey)
}
}
|