aboutsummaryrefslogtreecommitdiff
path: root/test-client/movement.ts
blob: 38f4b47b8d15a145dffea2a7ae75c7c715c6bb9a (plain)
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
/*
    Undercooked - a game about cooking
    Copyright 2024 metamuffin
    
    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, players, PlayerData } 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_SPEED = 65;

export function player_movement_update(p: PlayerData, dt: number, input: V2) {
    if (length(input) > 0.1) lerp_exp_v2_mut(p.facing, input, dt * 10.)
    p.rot = Math.atan2(p.facing.x, p.facing.y)
    p.vel.x += input.x * dt * PLAYER_SPEED
    p.vel.y += input.y * dt * PLAYER_SPEED
    p.x += p.vel.x * dt
    p.y += p.vel.y * dt
    collide_player(p, dt)
    lerp_exp_v2_mut(p.vel, { x: 0, y: 0 }, dt * 15.)
}

function collide_player(p: PlayerData, dt: number) {
    for (let xo = -1; xo <= 1; xo++) {
        for (let yo = -1; yo <= 1; yo++) {
            const x = Math.floor(p.x) + xo
            const y = Math.floor(p.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.x, p.y)
            if (d > PLAYER_SIZE) continue

            const h = 0.01
            const d_sample_x = aabb_point_distance(x, y, x + 1, y + 1, p.x + h, p.y)
            const d_sample_y = aabb_point_distance(x, y, x + 1, y + 1, p.x, p.y + h)
            const grad_x = (d_sample_x - d) / h
            const grad_y = (d_sample_y - d) / h

            p.x += (PLAYER_SIZE - d) * grad_x
            p.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
        }
    }

    for (const [_, player] of players) {
        const diff = sub_v2(p, player)
        const d = length(diff)
        if (d < 0.01) continue
        if (d >= PLAYER_SIZE * 2) continue
        const norm = normalize(diff);
        const f = 100 / (1 + d)
        p.vel.x += norm.x * f * dt
        p.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)
}