diff options
author | metamuffin <metamuffin@disroot.org> | 2024-08-13 13:25:14 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-08-13 16:03:38 +0200 |
commit | a8376aab4159a449a205de3ed7fdcaa5f6ca6369 (patch) | |
tree | 18683ebb64f9d41fb856d5e302f537785bb03f3d /server/src/entity/mod.rs | |
parent | 16ff78180669411326d42ea32d4a9260c018236c (diff) | |
download | hurrycurry-a8376aab4159a449a205de3ed7fdcaa5f6ca6369.tar hurrycurry-a8376aab4159a449a205de3ed7fdcaa5f6ca6369.tar.bz2 hurrycurry-a8376aab4159a449a205de3ed7fdcaa5f6ca6369.tar.zst |
access entities as trait object
Diffstat (limited to 'server/src/entity/mod.rs')
-rw-r--r-- | server/src/entity/mod.rs | 70 |
1 files changed, 37 insertions, 33 deletions
diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs index 10d0c155..efee6a6d 100644 --- a/server/src/entity/mod.rs +++ b/server/src/entity/mod.rs @@ -15,6 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ +pub mod bot; pub mod conveyor; pub mod customers; pub mod environment_effect; @@ -29,44 +30,51 @@ use environment_effect::{EnvironmentController, EnvironmentEffect, EnvironmentEf use hurrycurry_client_lib::Game; use hurrycurry_protocol::{ glam::{IVec2, Vec2}, - ItemIndex, PacketC, Recipe, TileIndex, + ItemIndex, PacketC, PacketS, Recipe, TileIndex, }; use item_portal::ItemPortal; use player_portal::PlayerPortal; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, HashSet, VecDeque}; +pub type DynEntity = Box<dyn Entity + Send + Sync + 'static>; +pub type Entities = Vec<DynEntity>; + pub struct EntityContext<'a> { pub game: &'a mut Game, pub packet_out: &'a mut VecDeque<PacketC>, + pub packet_in: &'a mut VecDeque<PacketS>, pub score_changed: &'a mut bool, pub dt: f32, } -pub trait EntityT: Clone { +pub trait Entity { fn tick(&mut self, c: EntityContext<'_>) -> Result<()>; + fn destructor(&mut self, _c: EntityContext<'_>) {} } -macro_rules! entities { - ($($e:ident),*) => { - #[derive(Debug, Clone)] - pub enum Entity { $($e($e)),* } - impl EntityT for Entity { - fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { - match self { $(Entity::$e(x) => x.tick(c)),*, } - } - } - }; -} - -entities!( - Conveyor, - ItemPortal, - PlayerPortal, - Customers, - EnvironmentEffectController, - EnvironmentController -); +// macro_rules! entities { +// ($($e:ident),*) => { +// #[derive(Debug)] +// pub enum Entity { $($e($e)),* } +// impl EntityT for Entity { +// fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { +// match self { $(Entity::$e(x) => x.tick(c)),*, } +// } +// fn destructor(&mut self, c: EntityContext<'_>) { +// match self { $(Entity::$e(x) => x.destructor(c)),*, } +// } +// } +// }; +// } +// entities!( +// Conveyor, +// ItemPortal, +// PlayerPortal, +// Customers, +// EnvironmentEffectController, +// EnvironmentController +// ); #[derive(Debug, Clone, Deserialize, Serialize)] #[serde(rename_all = "snake_case")] @@ -101,13 +109,13 @@ pub fn construct_entity( raw_demands: &[(ItemIndex, Option<ItemIndex>, f32)], recipes: &[Recipe], initial_map: &HashMap<IVec2, (TileIndex, Option<ItemIndex>)>, -) -> Result<Entity> { +) -> Result<DynEntity> { Ok(match decl.to_owned() { - EntityDecl::ItemPortal { from, to } => Entity::ItemPortal(ItemPortal { + EntityDecl::ItemPortal { from, to } => Box::new(ItemPortal { from: from.or(pos).ok_or(anyhow!("no item portal start"))?, to, }), - EntityDecl::PlayerPortal { from, to } => Entity::PlayerPortal(PlayerPortal { + EntityDecl::PlayerPortal { from, to } => Box::new(PlayerPortal { from: from .or(pos.map(|v| v.as_vec2())) .ok_or(anyhow!("no player portal start"))?, @@ -125,7 +133,7 @@ pub fn construct_entity( let to = to .or(dir.map(|s| s + from)) .ok_or(anyhow!("conveyor has no destination"))?; - Entity::Conveyor(Conveyor { + Box::new(Conveyor { from, to, max_cooldown: 1. / speed.unwrap_or(2.), @@ -142,13 +150,9 @@ pub fn construct_entity( .filter(|(_, (tile, _))| *tile == chair) .map(|(e, _)| (*e, true)) .collect(); - Entity::Customers(Customers::new(chairs, demands)?) - } - EntityDecl::EnvironmentEffect(config) => { - Entity::EnvironmentEffectController(EnvironmentEffectController::new(config)) - } - EntityDecl::Environment(names) => { - Entity::EnvironmentController(EnvironmentController(names)) + Box::new(Customers::new(chairs, demands)?) } + EntityDecl::EnvironmentEffect(config) => Box::new(EnvironmentEffectController::new(config)), + EntityDecl::Environment(names) => Box::new(EnvironmentController(names)), }) } |