diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-11 16:55:39 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-11 16:55:39 +0200 |
commit | 2537e764a359328870c3eabb16ee5238becd3c73 (patch) | |
tree | 07b731da36fd795399dbb62446e8a36ae3318f0b /server/src/entity/mod.rs | |
parent | 51f3c580fcfac8d37e5e345031cadda141f0340f (diff) | |
download | hurrycurry-2537e764a359328870c3eabb16ee5238becd3c73.tar hurrycurry-2537e764a359328870c3eabb16ee5238becd3c73.tar.bz2 hurrycurry-2537e764a359328870c3eabb16ee5238becd3c73.tar.zst |
add conveyor filters
Diffstat (limited to 'server/src/entity/mod.rs')
-rw-r--r-- | server/src/entity/mod.rs | 56 |
1 files changed, 37 insertions, 19 deletions
diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs index 925ed5f4..a1f690a3 100644 --- a/server/src/entity/mod.rs +++ b/server/src/entity/mod.rs @@ -16,10 +16,15 @@ */ pub mod conveyor; -use crate::{data::Gamedata, game::Tile}; +pub mod portal; +use crate::{ + data::{Gamedata, ItemTileRegistry}, + game::Tile, +}; use anyhow::{anyhow, Result}; use conveyor::Conveyor; use hurrycurry_protocol::{glam::IVec2, PacketC}; +use portal::Portal; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, VecDeque}; @@ -34,43 +39,54 @@ pub trait EntityT: Clone { ) -> 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), +macro_rules! entities { + ($($e:ident),*) => { + #[derive(Debug, Clone)] + pub enum Entity { $($e($e)),* } + 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::$e(x) => x.tick(data, points, packet_out, tiles, dt)),*, } + } } - } + }; } +entities!(Conveyor, Portal); + #[derive(Debug, Clone, Deserialize, Serialize)] #[serde(rename_all = "snake_case")] pub enum EntityDecl { Conveyor { from: Option<IVec2>, to: Option<IVec2>, + filter_dir: Option<IVec2>, + filter: Option<String>, dir: Option<IVec2>, speed: Option<f32>, }, + Portal { + from: Option<IVec2>, + to: IVec2, + }, } -pub fn construct_entity(pos: Option<IVec2>, decl: &EntityDecl) -> Result<Entity> { +pub fn construct_entity( + pos: Option<IVec2>, + decl: &EntityDecl, + reg: &ItemTileRegistry, +) -> Result<Entity> { Ok(match decl.to_owned() { + EntityDecl::Portal { from, to } => Entity::Portal(Portal { + from: from.or(pos).ok_or(anyhow!("no portla start"))?, + to, + }), EntityDecl::Conveyor { from, to, speed, dir, + filter, + filter_dir, } => { let from = from.or(pos).ok_or(anyhow!("conveyor has no start"))?; let to = to @@ -80,7 +96,9 @@ pub fn construct_entity(pos: Option<IVec2>, decl: &EntityDecl) -> Result<Entity> from, to, max_cooldown: 1. / speed.unwrap_or(2.), - ..Default::default() + filter_tile: filter_dir.map(|o| to + o), + filter_item: filter.map(|name| reg.register_item(name)), + cooldown: 0., }) } }) |