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
|
/*
Hurry Curry! - a game about cooking
Copyright (C) 2025 Hurry Curry! Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { data } from "./main.ts";
import { tiles } from "./main.ts";
import { V2, normalize, length, sub_v2, lerp_exp_v2_mut } from "./util.ts";
export const PLAYER_SIZE = 0.4
export const PLAYER_FRICTION = 15
export const PLAYER_SPEED = 55
export const BOOST_FACTOR = 2.5
export const BOOST_DURATION = 0.3
export const BOOST_RESTORE = 0.5
export interface MovementBase {
position: V2,
vel: V2,
facing: V2,
rot: number,
boosting: boolean,
stamina: number
}
export function update_movement(p: MovementBase, dt: number, direction: V2, boost: boolean) {
if (length(direction) > 0.05) lerp_exp_v2_mut(p.facing, direction, dt * 10.)
if (length(direction) < 0.5) direction.x = direction.y = 0
p.rot = Math.atan2(p.facing.x, p.facing.y)
boost &&= length(direction) > 0.1
p.boosting = boost && (p.boosting || p.stamina >= 1) && p.stamina > 0
if (p.boosting) p.stamina -= dt / BOOST_DURATION
else p.stamina += dt / BOOST_RESTORE
p.stamina = Math.max(Math.min(p.stamina, 1), 0)
const speed = PLAYER_SPEED * (p.boosting ? BOOST_FACTOR : 1)
p.vel.x += direction.x * dt * speed
p.vel.y += direction.y * dt * speed
p.position.x += p.vel.x * dt
p.position.y += p.vel.y * dt
collide_player(p, dt)
lerp_exp_v2_mut(p.vel, { x: 0, y: 0 }, dt * PLAYER_FRICTION)
}
function collide_player(p: MovementBase, _dt: number) {
for (let xo = -1; xo <= 1; xo++) {
for (let yo = -1; yo <= 1; yo++) {
const x = Math.floor(p.position.x) + xo
const y = Math.floor(p.position.y) + yo
const tile = tiles.get([x, y].toString())
if (tile && !data.tile_collide[tile.kind]) continue
const d = aabb_point_distance(x, y, x + 1, y + 1, p.position.x, p.position.y)
if (d > PLAYER_SIZE) continue
const h = 0.01
const d_sample_x = aabb_point_distance(x, y, x + 1, y + 1, p.position.x + h, p.position.y)
const d_sample_y = aabb_point_distance(x, y, x + 1, y + 1, p.position.x, p.position.y + h)
const grad_x = (d_sample_x - d) / h
const grad_y = (d_sample_y - d) / h
p.position.x += (PLAYER_SIZE - d) * grad_x
p.position.y += (PLAYER_SIZE - d) * grad_y
const vdotn = (grad_x * p.vel.x) + (grad_y * p.vel.y)
p.vel.x -= grad_x * vdotn
p.vel.y -= grad_y * vdotn
}
}
}
export function collide_player_player(a: MovementBase, b: MovementBase, dt: number) {
const diff = sub_v2(a.position, b.position)
const d = length(diff)
if (d < 0.01) return
if (d >= PLAYER_SIZE * 2) return
const norm = normalize(diff);
const f = 100 / (1 + d)
a.vel.x += norm.x * f * dt
a.vel.y += norm.y * f * dt
}
export function aabb_point_distance(
min_x: number,
min_y: number,
max_x: number,
max_y: number,
px: number,
py: number
): number {
const dx = px - Math.max(min_x, Math.min(max_x, px))
const dy = py - Math.max(min_y, Math.min(max_y, py))
return Math.sqrt(dx * dx + dy * dy)
}
|