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

*/
pub mod conveyor;
use crate::{data::Gamedata, game::Tile, protocol::PacketC};
use anyhow::{anyhow, Result};
use conveyor::Conveyor;
use glam::IVec2;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};

pub trait EntityT: Clone {
    fn tick(
        &mut self,
        data: &Gamedata,
        points: &mut i64,
        packet_out: &mut VecDeque<PacketC>,
        tiles: &mut HashMap<IVec2, Tile>,
        dt: f32,
    ) -> Result<()>;
}

#[derive(Debug, Clone)]
pub enum Entity {
    Conveyor(Conveyor),
}
impl EntityT for Entity {
    fn tick(
        &mut self,
        data: &Gamedata,
        points: &mut i64,
        packet_out: &mut VecDeque<PacketC>,
        tiles: &mut HashMap<IVec2, Tile>,
        dt: f32,
    ) -> Result<()> {
        match self {
            Entity::Conveyor(x) => x.tick(data, points, packet_out, tiles, dt),
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EntityDecl {
    Conveyor {
        from: Option<IVec2>,
        to: Option<IVec2>,
        dir: Option<IVec2>,
        speed: Option<f32>,
    },
}

pub fn construct_entity(pos: Option<IVec2>, decl: &EntityDecl) -> Result<Entity> {
    Ok(match decl.to_owned() {
        EntityDecl::Conveyor {
            from,
            to,
            speed,
            dir,
        } => {
            let from = from.or(pos).ok_or(anyhow!("conveyor has no start"))?;
            let to = to
                .or(dir.map(|s| s + from))
                .ok_or(anyhow!("conveyor has no destination"))?;
            Entity::Conveyor(Conveyor {
                from,
                to,
                max_cooldown: 1. / speed.unwrap_or(2.),
                ..Default::default()
            })
        }
    })
}