aboutsummaryrefslogtreecommitdiff
path: root/server/src/entity/tram.rs
blob: 27c3bcbbb3a35abf87e12864d70dc87d8c8df59a (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
/*
    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, PacketS, PlayerClass, PlayerID};
use rand::random;

pub struct Tram {
    length: usize,
    character: i32,
    ids: Vec<PlayerID>,
    points: Vec<Vec2>,
    index: usize,
}

impl Tram {
    pub fn new(character: i32, length: usize, points: Vec<Vec2>) -> Self {
        Self {
            character,
            ids: Vec::new(),
            length,
            points,
            index: 0,
        }
    }
}
impl Entity for Tram {
    fn finished(&self) -> bool {
        false
    }
    fn tick(&mut self, c: EntityContext<'_>) -> Result<()> {
        if self.ids.is_empty() {
            for i in 0..self.length {
                let id = PlayerID(random());
                c.packet_in.push_back(PacketS::Join {
                    name: format!("Tram {i}"),
                    character: self.character,
                    class: PlayerClass::Bot,
                    id: Some(id),
                });
                self.ids.push(id);
            }
        }

        let mut ppos = None;
        for id in &self.ids {
            if let Some(p) = c.game.players.get(id) {
                let target = if let Some(pos) = ppos {
                    pos
                } else {
                    let tar = self.points[self.index];
                    if p.movement.position.distance(tar) < 2. {
                        self.index += 1;
                        self.index %= self.points.len();
                    }
                    tar
                };
                ppos = Some(p.movement.position);
                let dir = if p.movement.position.distance(target) > 2. {
                    target - p.movement.position
                } else {
                    Vec2::ZERO
                };
                c.packet_in.push_back(PacketS::Movement {
                    player: *id,
                    dir,
                    boost: false,
                    pos: None,
                });
            }
        }

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