summaryrefslogtreecommitdiff
path: root/server/src/entity/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/entity/mod.rs')
-rw-r--r--server/src/entity/mod.rs56
1 files changed, 42 insertions, 14 deletions
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()
+ })
+ }
+ })
}