aboutsummaryrefslogtreecommitdiff
path: root/server/src/entity/pedestrians.rs
blob: cedb9e463ff2fc400b8f051ff54023c9d52e2c9a (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
/*
    Hurry Curry! - a game about cooking
    Copyright 2025 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/>.

*/
use super::{Entity, EntityContext};
use anyhow::Result;
use hurrycurry_protocol::{glam::Vec2, Character, PacketS, PlayerClass, PlayerID};
use rand::{random, rng};
use rand_distr::Distribution;
use std::collections::HashMap;

pub struct Pedestrians {
    pub players: HashMap<PlayerID, usize>,
    pub points: Vec<Vec2>,
    pub spawn_delay_distr: rand_distr::Normal<f32>,
    pub cooldown: f32,
    pub speed: f32,
}

impl Entity for Pedestrians {
    fn finished(&self) -> bool {
        false
    }
    fn tick(&mut self, c: EntityContext<'_>) -> Result<()> {
        self.cooldown -= c.dt;
        if self.cooldown <= 0. && self.players.len() < 32 {
            let id = PlayerID(random());
            c.packet_in.push_back(PacketS::Join {
                name: "Pedestrian".to_string(),
                character: Character {
                    color: random(),
                    hairstyle: random(),
                    headwear: 0,
                },
                class: PlayerClass::Customer,
                id: Some(id),
                position: self.points.get(0).copied(),
            });
            self.players.insert(id, 0);
            self.cooldown += self.spawn_delay_distr.sample(&mut rng()).max(0.1);
        }

        let mut remove = Vec::new();

        for (id, index) in &mut self.players {
            if let Some(player) = c.game.players.get(id) {
                let diff = self.points[*index] - player.movement.position;
                if diff.length() < 0.8 {
                    *index += 1;
                    if *index >= self.points.len() {
                        remove.push(*id)
                    }
                } else {
                    c.packet_in.push_back(PacketS::Movement {
                        player: *id,
                        dir: diff.normalize_or_zero() * self.speed,
                        boost: false,
                        pos: None,
                    });
                }
            }
        }

        for id in remove {
            if self.players.remove(&id).is_some() {
                c.packet_in.push_back(PacketS::Leave { player: id });
            }
        }

        Ok(())
    }
    fn destructor(&mut self, c: EntityContext<'_>) {
        for (id, _) in self.players.drain() {
            c.packet_in.push_back(PacketS::Leave { player: id })
        }
    }
}