diff options
Diffstat (limited to 'server/src/entity')
| -rw-r--r-- | server/src/entity/item_portal.rs (renamed from server/src/entity/portal.rs) | 4 | ||||
| -rw-r--r-- | server/src/entity/mod.rs | 31 | ||||
| -rw-r--r-- | server/src/entity/player_portal.rs | 48 | 
3 files changed, 73 insertions, 10 deletions
diff --git a/server/src/entity/portal.rs b/server/src/entity/item_portal.rs index 3ed19719..f63ff274 100644 --- a/server/src/entity/portal.rs +++ b/server/src/entity/item_portal.rs @@ -22,12 +22,12 @@ use hurrycurry_protocol::{glam::IVec2, ItemLocation, PacketC};  use std::collections::VecDeque;  #[derive(Debug, Default, Clone)] -pub struct Portal { +pub struct ItemPortal {      pub(super) from: IVec2,      pub(super) to: IVec2,  } -impl EntityT for Portal { +impl EntityT for ItemPortal {      fn tick(          &mut self,          game: &mut Game, diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs index 95172ed0..ad0bde6f 100644 --- a/server/src/entity/mod.rs +++ b/server/src/entity/mod.rs @@ -17,16 +17,21 @@  */  pub mod conveyor;  pub mod customers; -pub mod portal; -use std::collections::{HashMap, HashSet, VecDeque}; +pub mod item_portal; +pub mod player_portal;  use crate::{data::ItemTileRegistry, game::Game, interaction::Recipe};  use anyhow::{anyhow, Result};  use conveyor::Conveyor;  use customers::{demands::generate_demands, Customers}; -use hurrycurry_protocol::{glam::IVec2, ItemIndex, PacketC, TileIndex}; -use portal::Portal; +use hurrycurry_protocol::{ +    glam::{IVec2, Vec2}, +    ItemIndex, PacketC, TileIndex, +}; +use item_portal::ItemPortal; +use player_portal::PlayerPortal;  use serde::{Deserialize, Serialize}; +use std::collections::{HashMap, HashSet, VecDeque};  pub trait EntityT: Clone {      fn tick(&mut self, game: &mut Game, packet_out: &mut VecDeque<PacketC>, dt: f32) -> Result<()>; @@ -44,7 +49,7 @@ macro_rules! entities {      };  } -entities!(Conveyor, Portal, Customers); +entities!(Conveyor, ItemPortal, PlayerPortal, Customers);  #[derive(Debug, Clone, Deserialize, Serialize)]  #[serde(rename_all = "snake_case")] @@ -57,10 +62,14 @@ pub enum EntityDecl {          dir: Option<IVec2>,          speed: Option<f32>,      }, -    Portal { +    ItemPortal {          from: Option<IVec2>,          to: IVec2,      }, +    PlayerPortal { +        from: Option<Vec2>, +        to: Vec2, +    },      Customers {},  } @@ -75,8 +84,14 @@ pub fn construct_entity(      initial_map: &HashMap<IVec2, (TileIndex, Option<ItemIndex>)>,  ) -> Result<Entity> {      Ok(match decl.to_owned() { -        EntityDecl::Portal { from, to } => Entity::Portal(Portal { -            from: from.or(pos).ok_or(anyhow!("no portla start"))?, +        EntityDecl::ItemPortal { from, to } => Entity::ItemPortal(ItemPortal { +            from: from.or(pos).ok_or(anyhow!("no item portal start"))?, +            to, +        }), +        EntityDecl::PlayerPortal { from, to } => Entity::PlayerPortal(PlayerPortal { +            from: from +                .or(pos.map(|v| v.as_vec2())) +                .ok_or(anyhow!("no player portal start"))?,              to,          }),          EntityDecl::Conveyor { diff --git a/server/src/entity/player_portal.rs b/server/src/entity/player_portal.rs new file mode 100644 index 00000000..6ee04f15 --- /dev/null +++ b/server/src/entity/player_portal.rs @@ -0,0 +1,48 @@ +/* +    Hurry Curry! - a game about cooking +    Copyright 2024 metamuffin + +    This program is free software: you can redistribute it and/or modify +    it under the terms of the GNU Affero General Public License as published by +    the Free Software Foundation, version 3 of the License only. + +    This program is distributed in the hope that it will be useful, +    but WITHOUT ANY WARRANTY; without even the implied warranty of +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the +    GNU Affero General Public License for more details. + +    You should have received a copy of the GNU Affero General Public License +    along with this program.  If not, see <https://www.gnu.org/licenses/>. + +*/ +use super::EntityT; +use crate::game::Game; +use anyhow::{anyhow, Result}; +use hurrycurry_protocol::{glam::Vec2, PacketC}; +use std::collections::VecDeque; + +#[derive(Debug, Default, Clone)] +pub struct PlayerPortal { +    pub(super) from: Vec2, +    pub(super) to: Vec2, +} + +impl EntityT for PlayerPortal { +    fn tick( +        &mut self, +        game: &mut Game, +        _packet_out: &mut VecDeque<PacketC>, +        _dt: f32, +    ) -> Result<()> { +        let mut players = Vec::new(); +        game.players_spatial_index +            .query(self.from, 0.5, |pid, _| players.push(pid)); + +        for p in players { +            let p = game.players.get_mut(&p).ok_or(anyhow!("player missing"))?; +            p.movement.position = self.to; +        } + +        Ok(()) +    } +}  |