diff options
author | metamuffin <metamuffin@disroot.org> | 2024-06-17 23:41:25 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-06-23 19:20:50 +0200 |
commit | a99aa006599827ea999a5684e40635175c8d790a (patch) | |
tree | 1b36a73833d3a87384e7bbfb379c4adceb72cd27 /server/src/game.rs | |
parent | 6f0424b9b4cddc0495eb673d314c570e27e61e83 (diff) | |
download | hurrycurry-a99aa006599827ea999a5684e40635175c8d790a.tar hurrycurry-a99aa006599827ea999a5684e40635175c8d790a.tar.bz2 hurrycurry-a99aa006599827ea999a5684e40635175c8d790a.tar.zst |
a
Diffstat (limited to 'server/src/game.rs')
-rw-r--r-- | server/src/game.rs | 98 |
1 files changed, 70 insertions, 28 deletions
diff --git a/server/src/game.rs b/server/src/game.rs index a7a07881..3b16cc05 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -1,20 +1,27 @@ use crate::{ - protocol::{ItemID, ItemIndex, PacketC, PacketS, PlayerID, TileIndex}, + interaction::{interact, Out}, + protocol::{ItemID, ItemIndex, PacketC, PacketS, PlayerID, RecipeIndex, TileIndex}, recipes::Gamedata, }; use anyhow::{anyhow, Result}; use glam::IVec2; +use log::info; use std::{ collections::{HashMap, VecDeque}, ops::Deref, sync::Arc, }; +pub struct ActiveRecipe { + pub recipe: RecipeIndex, + pub progress: f32, + pub working: usize, +} + pub struct Tile { kind: TileIndex, items: Vec<ItemID>, - active: bool, - progress: f32, + active: Option<ActiveRecipe>, } struct Player { @@ -69,17 +76,13 @@ impl Game { } g.tiles.extend( - [([-5, 1], "pan"), ([-5, 2], "pan"), ([4, 3], "meat-spawn")].map(|(k, v)| { - ( - IVec2::from_array(k), - Tile { - active: false, - items: vec![], - kind: gamedata.get_tile(v).unwrap().into(), - progress: 0., - }, - ) - }), + [ + ([-5, 1], "pan"), + ([-5, 2], "pan"), + ([4, 3], "meat-spawn"), + ([4, 1], "trash"), + ] + .map(|(k, v)| (IVec2::from_array(k), gamedata.get_tile(v).unwrap().into())), ); g @@ -150,19 +153,59 @@ impl Game { .push_back(PacketC::Position { player, pos, rot }); } PacketS::Interact { pos, edge } => { + let pid = player; + let player = self + .players + .get_mut(&player) + .ok_or(anyhow!("player does not exist"))?; + let tile = self + .tiles + .get_mut(&pos) + .ok_or(anyhow!("tile does not exist"))?; + + let items = tile.items.iter().map(|e| self.items[e]).collect::<Vec<_>>(); + let tilekind = tile.kind; + let hand = player.hand.map(|e| self.items[&e]); - // if let Some(item) = player_data.hand.take() { - // info!("put {item}"); - // tile.items.push(item); - // self.packet_out.push_back(PacketC::PutItem { item, pos }) - // } else { - // if let Some(item) = tile.items.pop() { - // info!("take {item}"); - // player_data.hand = Some(item); - // self.packet_out - // .push_back(PacketC::TakeItem { item, player }) - // } - // } + interact( + &self.data, + edge, + tilekind, + &mut tile.active, + items, + hand, + |out| match out { + Out::Take(index) => { + info!("take"); + let item = tile.items.remove(index); + player.hand = Some(item); + self.packet_out + .push_back(PacketC::TakeItem { item, player: pid }) + } + Out::Put => { + info!("put"); + let hand = player.hand.take().unwrap(); + tile.items.push(hand); + self.packet_out + .push_back(PacketC::PutItem { item: hand, pos }) + } + Out::Produce(kind) => { + info!("produce"); + let id = self.item_id_counter; + self.item_id_counter += 1; + self.items.insert(id, kind); + tile.items.push(id); + self.packet_out + .push_back(PacketC::ProduceItem { id, pos, kind }); + } + Out::Consume(index) => { + info!("consume"); + let id = tile.items.remove(index); + info!("left {:?}", tile.items); + self.packet_out.push_back(PacketC::ConsumeItem { id, pos }); + } + }, + ); } } Ok(()) @@ -173,9 +216,8 @@ impl From<TileIndex> for Tile { fn from(kind: TileIndex) -> Self { Self { kind, - progress: 0., - active: false, items: vec![], + active: None, } } } |