aboutsummaryrefslogtreecommitdiff
path: root/server/src/entity/player_portal_pair.rs
blob: 9691ca05767c4e75a740c1a737b75d018423749c (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
110
111
112
113
114
115
116
117
118
119
120
121
/*
    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/>.

*/
use super::{Entity, EntityContext};
use anyhow::Result;
use hurrycurry_locale::TrError;
use hurrycurry_protocol::{
    PacketC, TileIndex,
    glam::{IVec2, Vec2, vec2},
};

#[derive(Debug, Clone)]
pub struct PlayerPortalPair {
    a_i: IVec2,
    b_i: IVec2,
    a_f: Vec2,
    b_f: Vec2,
    in_tile: TileIndex,
    neutral_tile: TileIndex,
    out_tile: TileIndex,
    state: i8,
    delay: f32,
}
impl PlayerPortalPair {
    pub fn new(
        a: IVec2,
        b: IVec2,
        in_tile: TileIndex,
        neutral_tile: TileIndex,
        out_tile: TileIndex,
    ) -> Self {
        Self {
            a_i: a,
            b_i: b,
            a_f: a.as_vec2() + vec2(0.5, 0.5),
            b_f: b.as_vec2() + vec2(0.5, 0.5),
            in_tile,
            neutral_tile,
            out_tile,
            state: 1,
            delay: 0.,
        }
    }
}
impl Entity for PlayerPortalPair {
    fn tick(&mut self, c: EntityContext) -> Result<(), TrError> {
        let qn = |p, r| {
            let mut o = None;
            c.game
                .players_spatial_index
                .query(p, r, |pid, _| o = Some(pid));
            o
        };

        self.delay -= c.dt;

        let near_a = qn(self.a_f, 1.5);
        let near_b = qn(self.b_f, 1.5);
        let in_a = qn(self.a_f, 0.7);
        let in_b = qn(self.b_f, 0.7);

        if (self.state == 1 && near_b.is_none() && in_a.is_none())
            || (self.state == -1 && near_a.is_none() && in_b.is_none())
        {
            c.game.set_tile(self.b_i, Some(self.neutral_tile));
            c.game.set_tile(self.a_i, Some(self.neutral_tile));
            self.state = 0;
            return Ok(());
        }

        if self.state == 0 && in_a.is_some() {
            c.game.set_tile(self.a_i, Some(self.in_tile));
            c.game.set_tile(self.b_i, Some(self.out_tile));
            self.state = 1;
            self.delay = 0.2;
            return Ok(());
        }

        if self.state == 0 && in_b.is_some() {
            c.game.set_tile(self.b_i, Some(self.in_tile));
            c.game.set_tile(self.a_i, Some(self.out_tile));
            self.state = -1;
            self.delay = 0.2;
            return Ok(());
        }

        if self.state == 1
            && self.delay < 0.
            && let Some(player) = in_a
            && let Some(p) = c.game.players.get_mut(&player)
        {
            p.movement.position = self.b_f;
            c.packet_out.push_back(PacketC::MovementSync { player });
        }

        if self.state == -1
            && self.delay < 0.
            && let Some(player) = in_b
            && let Some(p) = c.game.players.get_mut(&player)
        {
            p.movement.position = self.a_f;
            c.packet_out.push_back(PacketC::MovementSync { player });
        }

        Ok(())
    }
}