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 | |
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')
-rw-r--r-- | server/src/entity/bot.rs | 13 | ||||
-rw-r--r-- | server/src/entity/conveyor.rs | 4 | ||||
-rw-r--r-- | server/src/entity/customers/mod.rs | 4 | ||||
-rw-r--r-- | server/src/entity/environment_effect.rs | 6 | ||||
-rw-r--r-- | server/src/entity/item_portal.rs | 4 | ||||
-rw-r--r-- | server/src/entity/mod.rs | 70 | ||||
-rw-r--r-- | server/src/entity/player_portal.rs | 4 |
7 files changed, 61 insertions, 44 deletions
diff --git a/server/src/entity/bot.rs b/server/src/entity/bot.rs new file mode 100644 index 00000000..06477e8a --- /dev/null +++ b/server/src/entity/bot.rs @@ -0,0 +1,13 @@ +use super::{EntityContext, Entity}; +use anyhow::Result; +use hurrycurry_bot::BotAlgo; + +pub struct BotDriver { + algo: Box<dyn BotAlgo>, +} + +impl Entity for BotDriver { + fn tick(&mut self, c: EntityContext<'_>) -> Result<()> { + Ok(()) + } +} diff --git a/server/src/entity/conveyor.rs b/server/src/entity/conveyor.rs index f7f091c7..5602e082 100644 --- a/server/src/entity/conveyor.rs +++ b/server/src/entity/conveyor.rs @@ -15,7 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use super::{EntityContext, EntityT}; +use super::{EntityContext, Entity}; use crate::server::interact_effect; use anyhow::{anyhow, Result}; use hurrycurry_protocol::{glam::IVec2, ItemIndex, ItemLocation}; @@ -30,7 +30,7 @@ pub struct Conveyor { pub(super) max_cooldown: f32, } -impl EntityT for Conveyor { +impl Entity for Conveyor { fn tick(&mut self, c: EntityContext) -> Result<()> { let from = c .game diff --git a/server/src/entity/customers/mod.rs b/server/src/entity/customers/mod.rs index 85da2c07..5038eaf2 100644 --- a/server/src/entity/customers/mod.rs +++ b/server/src/entity/customers/mod.rs @@ -18,7 +18,7 @@ pub mod demands; mod pathfinding; -use super::{EntityContext, EntityT}; +use super::{EntityContext, Entity}; use crate::{data::Demand, server::Server}; use anyhow::{anyhow, bail, Result}; use fake::{faker, Fake}; @@ -74,7 +74,7 @@ impl Customers { } } -impl EntityT for Customers { +impl Entity for Customers { fn tick(&mut self, c: EntityContext) -> Result<()> { // self.spawn_cooldown -= dt; // self.spawn_cooldown = self.spawn_cooldown.max(0.); diff --git a/server/src/entity/environment_effect.rs b/server/src/entity/environment_effect.rs index 8f54d29c..b0e811c3 100644 --- a/server/src/entity/environment_effect.rs +++ b/server/src/entity/environment_effect.rs @@ -15,7 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use super::{EntityContext, EntityT}; +use super::{EntityContext, Entity}; use hurrycurry_protocol::PacketC; use rand::random; use serde::{Deserialize, Serialize}; @@ -49,7 +49,7 @@ impl EnvironmentEffectController { } } } -impl EntityT for EnvironmentEffectController { +impl Entity for EnvironmentEffectController { fn tick(&mut self, c: EntityContext) -> anyhow::Result<()> { if self.next_transition < Instant::now() { if self.active { @@ -73,7 +73,7 @@ impl EntityT for EnvironmentEffectController { #[derive(Debug, Clone)] pub struct EnvironmentController(pub Vec<String>); -impl EntityT for EnvironmentController { +impl Entity for EnvironmentController { fn tick(&mut self, c: EntityContext) -> anyhow::Result<()> { if c.game.environment_effects.is_empty() { c.game.environment_effects.extend(self.0.clone()); diff --git a/server/src/entity/item_portal.rs b/server/src/entity/item_portal.rs index be6acd06..faaa210b 100644 --- a/server/src/entity/item_portal.rs +++ b/server/src/entity/item_portal.rs @@ -15,7 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use super::{EntityContext, EntityT}; +use super::{EntityContext, Entity}; use crate::server::interact_effect; use anyhow::{anyhow, Result}; use hurrycurry_protocol::{glam::IVec2, ItemLocation}; @@ -26,7 +26,7 @@ pub struct ItemPortal { pub(super) to: IVec2, } -impl EntityT for ItemPortal { +impl Entity for ItemPortal { fn tick(&mut self, c: EntityContext) -> Result<()> { let [from, to] = c .game 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)), }) } diff --git a/server/src/entity/player_portal.rs b/server/src/entity/player_portal.rs index 11442677..53ba9c39 100644 --- a/server/src/entity/player_portal.rs +++ b/server/src/entity/player_portal.rs @@ -15,7 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use super::{EntityContext, EntityT}; +use super::{EntityContext, Entity}; use anyhow::{anyhow, Result}; use hurrycurry_protocol::{glam::Vec2, PacketC}; @@ -25,7 +25,7 @@ pub struct PlayerPortal { pub(super) to: Vec2, } -impl EntityT for PlayerPortal { +impl Entity for PlayerPortal { fn tick(&mut self, c: EntityContext) -> Result<()> { let mut players = Vec::new(); c.game |