diff options
Diffstat (limited to 'server/src/entity')
-rw-r--r-- | server/src/entity/conveyor.rs | 54 | ||||
-rw-r--r-- | server/src/entity/mod.rs | 42 |
2 files changed, 96 insertions, 0 deletions
diff --git a/server/src/entity/conveyor.rs b/server/src/entity/conveyor.rs new file mode 100644 index 00000000..b74c03ef --- /dev/null +++ b/server/src/entity/conveyor.rs @@ -0,0 +1,54 @@ +use super::Entity; +use crate::{ + data::Gamedata, + game::{interact_effect, Tile}, + protocol::{ItemLocation, PacketC}, +}; +use anyhow::{anyhow, Result}; +use glam::IVec2; +use std::collections::{HashMap, VecDeque}; + +#[derive(Debug, Default)] +pub struct Conveyor { + pub(super) from: IVec2, + pub(super) to: IVec2, + pub(super) cooldown: f32, + pub(super) max_cooldown: f32, +} + +impl Entity for Conveyor { + fn tick( + &mut self, + data: &Gamedata, + points: &mut i64, + packet_out: &mut VecDeque<PacketC>, + tiles: &mut HashMap<IVec2, Tile>, + dt: f32, + ) -> Result<()> { + let [from, to] = tiles + .get_many_mut([&self.from, &self.to]) + .ok_or(anyhow!("conveyor does ends in itself"))?; + + if from.item.is_some() { + self.cooldown += dt; + if self.cooldown < self.max_cooldown { + return Ok(()); + } + self.cooldown = 0.; + + interact_effect( + data, + true, + &mut to.item, + ItemLocation::Tile(self.to), + &mut from.item, + ItemLocation::Tile(self.from), + Some(to.kind), + packet_out, + points, + ); + } + + Ok(()) + } +} diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs new file mode 100644 index 00000000..089c60a5 --- /dev/null +++ b/server/src/entity/mod.rs @@ -0,0 +1,42 @@ +pub mod conveyor; + +use crate::{data::Gamedata, game::Tile, protocol::PacketC}; +use 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 { + 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, Deserialize, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum EntityDecl { + Conveyor { + from: IVec2, + to: IVec2, + speed: Option<f32>, + }, +} + +pub fn construct_entity(decl: &EntityDecl) -> DynEntity { + match decl.to_owned() { + EntityDecl::Conveyor { from, to, speed } => Box::new(Conveyor { + from, + to, + max_cooldown: 1. / speed.unwrap_or(2.), + ..Default::default() + }), + } +} |