aboutsummaryrefslogtreecommitdiff
path: root/server/src/entity/mod.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-07-18 15:42:11 +0200
committermetamuffin <metamuffin@disroot.org>2024-07-18 15:42:11 +0200
commit5f883c80e7fc63c97910d003c44aea814ab8a5d6 (patch)
treeb73a8c8f78db103671128e686136f08aa276923a /server/src/entity/mod.rs
parentefc29c03f7be043ae8d037a93efce8cfa7c384cc (diff)
downloadhurrycurry-5f883c80e7fc63c97910d003c44aea814ab8a5d6.tar
hurrycurry-5f883c80e7fc63c97910d003c44aea814ab8a5d6.tar.bz2
hurrycurry-5f883c80e7fc63c97910d003c44aea814ab8a5d6.tar.zst
reimplement customers as entity
Diffstat (limited to 'server/src/entity/mod.rs')
-rw-r--r--server/src/entity/mod.rs43
1 files changed, 26 insertions, 17 deletions
diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs
index a1f690a3..beee9309 100644
--- a/server/src/entity/mod.rs
+++ b/server/src/entity/mod.rs
@@ -16,27 +16,20 @@
*/
pub mod conveyor;
+pub mod customers;
pub mod portal;
-use crate::{
- data::{Gamedata, ItemTileRegistry},
- game::Tile,
-};
+use std::collections::{HashMap, HashSet};
+
+use crate::{data::ItemTileRegistry, game::Game, interaction::Recipe};
use anyhow::{anyhow, Result};
use conveyor::Conveyor;
-use hurrycurry_protocol::{glam::IVec2, PacketC};
+use customers::{demands::generate_demands, Customers};
+use hurrycurry_protocol::{glam::IVec2, ItemIndex, TileIndex};
use portal::Portal;
use serde::{Deserialize, Serialize};
-use std::collections::{HashMap, VecDeque};
pub trait EntityT: Clone {
- fn tick(
- &mut self,
- data: &Gamedata,
- points: &mut i64,
- packet_out: &mut VecDeque<PacketC>,
- tiles: &mut HashMap<IVec2, Tile>,
- dt: f32,
- ) -> Result<()>;
+ fn tick(&mut self, game: &mut Game, dt: f32) -> Result<()>;
}
macro_rules! entities {
@@ -44,14 +37,14 @@ macro_rules! entities {
#[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)),*, }
+ fn tick(&mut self, game: &mut Game, dt: f32) -> Result<()> {
+ match self { $(Entity::$e(x) => x.tick(game, dt)),*, }
}
}
};
}
-entities!(Conveyor, Portal);
+entities!(Conveyor, Portal, Customers);
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
@@ -68,12 +61,18 @@ pub enum EntityDecl {
from: Option<IVec2>,
to: IVec2,
},
+ Customers {},
}
pub fn construct_entity(
pos: Option<IVec2>,
decl: &EntityDecl,
reg: &ItemTileRegistry,
+ tiles_used: &HashSet<TileIndex>,
+ items_used: &HashSet<ItemIndex>,
+ raw_demands: &[(ItemIndex, Option<ItemIndex>, f32)],
+ recipes: &[Recipe],
+ initial_map: &HashMap<IVec2, (TileIndex, Option<ItemIndex>)>,
) -> Result<Entity> {
Ok(match decl.to_owned() {
EntityDecl::Portal { from, to } => Entity::Portal(Portal {
@@ -101,5 +100,15 @@ pub fn construct_entity(
cooldown: 0.,
})
}
+ EntityDecl::Customers {} => {
+ let demands = generate_demands(tiles_used, items_used, &raw_demands, &recipes);
+ let chair = reg.register_tile("chair".to_string());
+ let chairs = initial_map
+ .iter()
+ .filter(|(_, (tile, _))| *tile == chair)
+ .map(|(e, _)| (*e, true))
+ .collect();
+ Entity::Customers(Customers::new(chairs, demands))
+ }
})
}