aboutsummaryrefslogtreecommitdiff
path: root/test-client/util.ts
blob: e435b2c3caf01569d6cff9488ae3fb5e390bd8c0 (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
/*
    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/>.

*/
export interface V2 { x: number, y: number }
export function length(p: V2): number { return Math.sqrt(p.x * p.x + p.y * p.y) }
export function normalize_mut(p: V2) { const l = length(p); if (l == 0) return; p.x /= l; p.y /= l }
export function normalize(p: V2): V2 { let l = length(p); if (l == 0) l = 1; return { x: p.x / l, y: p.y / l } }
export function lerp_exp(current: number, target: number, dt: number): number {
    return target + (current - target) * Math.exp(-dt)
}
export function lerp_v2(a: V2, b: V2, t: number): V2 {
    return {
        x: a.x * (1 - t) + b.x * t,
        y: a.y * (1 - t) + b.y * t,
    }
}
export function lerp_exp_v2_mut(current: V2, target: V2, dt: number) {
    current.x = target.x + (current.x - target.x) * Math.exp(-dt)
    current.y = target.y + (current.y - target.y) * Math.exp(-dt)
}
export function floor_v2(p: V2): V2 { return { x: Math.floor(p.x), y: Math.floor(p.y) } }
export function ceil_v2(p: V2): V2 { return { x: Math.ceil(p.x), y: Math.ceil(p.y) } }
export function add_v2(p: V2, o: V2 | number) {
    if (typeof o == "number") return { x: p.x + o, y: p.y + o }
    else return { x: p.x + o.x, y: p.y + o.y }
}
export function sub_v2(p: V2, o: V2) { return { x: p.x - o.x, y: p.y - o.y } }