diff options
Diffstat (limited to 'server/src/entity')
| -rw-r--r-- | server/src/entity/conveyor.rs | 7 | ||||
| -rw-r--r-- | server/src/entity/mod.rs | 56 | 
2 files changed, 46 insertions, 17 deletions
| diff --git a/server/src/entity/conveyor.rs b/server/src/entity/conveyor.rs index b74c03ef..48d0d154 100644 --- a/server/src/entity/conveyor.rs +++ b/server/src/entity/conveyor.rs @@ -1,4 +1,4 @@ -use super::Entity; +use super::EntityT;  use crate::{      data::Gamedata,      game::{interact_effect, Tile}, @@ -8,7 +8,7 @@ use anyhow::{anyhow, Result};  use glam::IVec2;  use std::collections::{HashMap, VecDeque}; -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone)]  pub struct Conveyor {      pub(super) from: IVec2,      pub(super) to: IVec2, @@ -16,7 +16,7 @@ pub struct Conveyor {      pub(super) max_cooldown: f32,  } -impl Entity for Conveyor { +impl EntityT for Conveyor {      fn tick(          &mut self,          data: &Gamedata, @@ -46,6 +46,7 @@ impl Entity for Conveyor {                  Some(to.kind),                  packet_out,                  points, +                true,              );          } diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs index 089c60a5..d286d3bb 100644 --- a/server/src/entity/mod.rs +++ b/server/src/entity/mod.rs @@ -1,15 +1,12 @@  pub mod conveyor; -  use crate::{data::Gamedata, game::Tile, protocol::PacketC}; -use anyhow::Result; +use anyhow::{anyhow, Result};  use conveyor::Conveyor;  use glam::IVec2;  use serde::{Deserialize, Serialize};  use std::collections::{HashMap, VecDeque}; -pub type DynEntity = Box<dyn Entity + Send + Sync + 'static>; - -pub trait Entity { +pub trait EntityT: Clone {      fn tick(          &mut self,          data: &Gamedata, @@ -20,23 +17,54 @@ pub trait Entity {      ) -> 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: IVec2, -        to: IVec2, +        from: Option<IVec2>, +        to: Option<IVec2>, +        dir: Option<IVec2>,          speed: Option<f32>,      },  } -pub fn construct_entity(decl: &EntityDecl) -> DynEntity { -    match decl.to_owned() { -        EntityDecl::Conveyor { from, to, speed } => Box::new(Conveyor { +pub fn construct_entity(pos: Option<IVec2>, decl: &EntityDecl) -> Result<Entity> { +    Ok(match decl.to_owned() { +        EntityDecl::Conveyor {              from,              to, -            max_cooldown: 1. / speed.unwrap_or(2.), -            ..Default::default() -        }), -    } +            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() +            }) +        } +    })  } | 
