/* 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 . */ pub mod conveyor; pub mod customers; pub mod portal; use std::collections::{HashMap, HashSet, VecDeque}; 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 serde::{Deserialize, Serialize}; pub trait EntityT: Clone { fn tick(&mut self, game: &mut Game, packet_out: &mut VecDeque, dt: f32) -> Result<()>; } macro_rules! entities { ($($e:ident),*) => { #[derive(Debug, Clone)] pub enum Entity { $($e($e)),* } impl EntityT for Entity { fn tick(&mut self, game: &mut Game, packet_out: &mut VecDeque, dt: f32) -> Result<()> { match self { $(Entity::$e(x) => x.tick(game, packet_out, dt)),*, } } } }; } entities!(Conveyor, Portal, Customers); #[derive(Debug, Clone, Deserialize, Serialize)] #[serde(rename_all = "snake_case")] pub enum EntityDecl { Conveyor { from: Option, to: Option, filter_dir: Option, filter: Option, dir: Option, speed: Option, }, Portal { from: Option, to: IVec2, }, Customers {}, } pub fn construct_entity( pos: Option, decl: &EntityDecl, reg: &ItemTileRegistry, tiles_used: &HashSet, items_used: &HashSet, raw_demands: &[(ItemIndex, Option, f32)], recipes: &[Recipe], initial_map: &HashMap)>, ) -> Result { 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 .or(dir.map(|s| s + from)) .ok_or(anyhow!("conveyor has no destination"))?; Entity::Conveyor(Conveyor { from, to, max_cooldown: 1. / speed.unwrap_or(2.), filter_tile: filter_dir.map(|o| to + o), filter_item: filter.map(|name| reg.register_item(name)), 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)) } }) }