aboutsummaryrefslogtreecommitdiff
path: root/server/src/customer/movement.rs
blob: ca7afdbe2f5382b4107ffdb104c5fd130f6eebfd (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
    Hurry Curry! - a game about cooking
    Copyright 2024 metamuffin
    Copyright 2024 tpart

    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/>.

*/
use crate::protocol::PacketS;
use glam::{IVec2, Vec2};
use std::collections::HashSet;

const PLAYER_SIZE: f32 = 0.4;
const PLAYER_FRICTION: f32 = 10.0;
const PLAYER_SPEED: f32 = 55.0;
const BOOST_FACTOR: f32 = 2.5;
const BOOST_DURATION: f32 = 0.3;
const BOOST_RESTORE: f32 = 0.5;

pub struct MovementBase {
    pub position: Vec2,
    pub facing: Vec2,
    pub vel: Vec2,
    pub boosting: bool,
    pub stamina: f32,
}

impl MovementBase {
    pub fn new(position: Vec2) -> Self {
        Self {
            position,
            facing: Vec2::X,
            vel: Vec2::ZERO,
            boosting: false,
            stamina: 0.,
        }
    }
    pub fn update(
        &mut self,
        map: &HashSet<IVec2>,
        direction: Vec2,
        mut boost: bool,
        dt: f32,
    ) -> PacketS {
        let direction = direction.clamp_length_max(1.);
        if direction.length() > 0.1 {
            self.facing = direction + (self.facing - direction) * (-dt * 10.).exp();
        }
        let rot = self.facing.x.atan2(self.facing.y);
        boost &= direction.length() > 0.1;
        self.boosting = boost && (self.boosting || self.stamina >= 1.) && self.stamina > 0.;
        self.stamina += if self.boosting {
            -dt / BOOST_DURATION
        } else {
            dt / BOOST_RESTORE
        };
        self.stamina = self.stamina.max(0.).min(1.);
        let speed = PLAYER_SPEED * if self.boosting { BOOST_FACTOR } else { 1. };
        self.vel += direction * dt * speed;
        self.position += self.vel * dt;
        self.vel = self.vel * (-dt * PLAYER_FRICTION).exp();
        collide_player(self, map);

        PacketS::Position {
            pos: self.position,
            boosting: false,
            rot,
        }
    }
}

pub fn collide_player(p: &mut MovementBase, map: &HashSet<IVec2>) {
    for xo in -1..=1 {
        for yo in -1..=1 {
            let tile = IVec2::new(xo, yo) + p.position.as_ivec2();
            if map.contains(&tile) {
                continue;
            }
            let tile = tile.as_vec2();
            let d = aabb_point_distance(tile, tile + Vec2::ONE, p.position);
            if d > PLAYER_SIZE {
                continue;
            }
            let h = 0.01;
            let d_sample_x =
                aabb_point_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(h, 0.));
            let d_sample_y =
                aabb_point_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(0., h));
            let grad = (Vec2::new(d_sample_x, d_sample_y) - d) / h;

            p.position += (PLAYER_SIZE - d) * grad;
            p.vel -= grad * grad.dot(p.vel);
        }
    }
}

pub fn aabb_point_distance(min: Vec2, max: Vec2, p: Vec2) -> f32 {
    (p - p.clamp(min, max)).length()
}