diff options
author | metamuffin <metamuffin@disroot.org> | 2024-06-18 19:36:36 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-06-23 19:21:22 +0200 |
commit | 48934ff63ee14d4759eda36512af87361dd915dd (patch) | |
tree | f7a80115eacfee7b6871040a87f5fb0087098ea8 /server/src/game.rs | |
parent | 6ec47d729509db83eaeb6a9d855ce2483d70f227 (diff) | |
download | hurrycurry-48934ff63ee14d4759eda36512af87361dd915dd.tar hurrycurry-48934ff63ee14d4759eda36512af87361dd915dd.tar.bz2 hurrycurry-48934ff63ee14d4759eda36512af87361dd915dd.tar.zst |
remodel game.
Diffstat (limited to 'server/src/game.rs')
-rw-r--r-- | server/src/game.rs | 224 |
1 files changed, 111 insertions, 113 deletions
diff --git a/server/src/game.rs b/server/src/game.rs index 5cb155f1..d481dc4f 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -1,10 +1,10 @@ use crate::{ - interaction::{interact, tick_tile, Out}, - protocol::{ItemID, ItemIndex, PacketC, PacketS, PlayerID, RecipeIndex, TileIndex}, - recipes::Gamedata, + data::Gamedata, + interaction::interact, + protocol::{ItemIndex, PacketC, PacketS, PlayerID, RecipeIndex, TileIndex}, }; use anyhow::{anyhow, bail, Result}; -use glam::IVec2; +use glam::{IVec2, Vec2}; use log::info; use std::{ collections::{HashMap, VecDeque}, @@ -12,29 +12,32 @@ use std::{ sync::Arc, }; -pub struct ActiveRecipe { +pub struct Involvement { pub recipe: RecipeIndex, pub progress: f32, pub working: usize, } +pub struct Item { + pub kind: ItemIndex, + pub active: Option<Involvement>, +} + pub struct Tile { - kind: TileIndex, - items: Vec<ItemID>, - active: Option<ActiveRecipe>, + pub kind: TileIndex, + pub item: Option<Item>, } -struct Player { - name: String, - interacting: bool, - hand: Option<ItemID>, +pub struct Player { + pub name: String, + pub position: Vec2, + pub interacting: bool, + pub item: Option<Item>, } pub struct Game { data: Arc<Gamedata>, - item_id_counter: ItemID, tiles: HashMap<IVec2, Tile>, - items: HashMap<ItemID, ItemIndex>, players: HashMap<PlayerID, Player>, packet_out: VecDeque<PacketC>, } @@ -43,8 +46,6 @@ impl Game { pub fn new(gamedata: Arc<Gamedata>) -> Self { let mut g = Self { data: gamedata.clone(), - item_id_counter: 0, - items: Default::default(), packet_out: Default::default(), players: Default::default(), tiles: Default::default(), @@ -69,19 +70,18 @@ impl Game { out.push(PacketC::AddPlayer { id, name: player.name.clone(), - hand: player.hand.map(|i| (i, self.items[&i].clone())), + hand: player.item.as_ref().map(|i| i.kind), }) } - for (&pos, tdata) in &self.tiles { + for (&tile, tdata) in &self.tiles { out.push(PacketC::UpdateMap { - pos, + pos: tile, tile: tdata.kind.clone(), }); - for &id in &tdata.items { + if let Some(item) = &tdata.item { out.push(PacketC::ProduceItem { - id, - pos, - kind: self.items[&id].clone(), + item: item.kind, + tile, }) } } @@ -94,7 +94,8 @@ impl Game { self.players.insert( player, Player { - hand: None, + item: None, + position: self.data.spawn, interacting: false, name: name.clone(), }, @@ -110,8 +111,8 @@ impl Game { .players .remove(&player) .ok_or(anyhow!("player does not exist"))?; - if let Some(id) = p.hand { - self.items.remove(&id).expect("hand item lost"); + if let Some(id) = p.item { + // TODO place on ground } self.packet_out .push_back(PacketC::RemovePlayer { id: player }) @@ -137,108 +138,105 @@ impl Game { bail!("already (not) interacting") } - 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]); + let tile_had_item = tile.item.is_some(); - 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 }); - } - Out::SetActive(progress) => { - self.packet_out.push_back(PacketC::SetActive { - tile: pos, - progress, - }); - } - }, - ); + interact(&self.data, edge, tile, player); + + // interact( + // &self.data, + // edge, + // tilekind, + // &mut tile.active, + // item, + // 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 }); + // } + // Out::SetActive(progress) => { + // self.packet_out.push_back(PacketC::SetActive { + // tile: pos, + // progress, + // }); + // } + // }, + // ); player.interacting = edge; } + PacketS::Collide { player, force } => {} } Ok(()) } pub fn tick(&mut self, dt: f32) { for (&pos, tile) in &mut self.tiles { - let items = tile.items.iter().map(|e| self.items[e]).collect::<Vec<_>>(); - tick_tile( - dt, - &self.data, - tile.kind, - &mut tile.active, - items, - |out| match out { - Out::Take(_) | Out::Put => { - unreachable!() - } - 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 }); - } - Out::SetActive(progress) => { - self.packet_out.push_back(PacketC::SetActive { - tile: pos, - progress, - }); - } - }, - ); + // let items = tile.items.iter().map(|e| self.items[e]).collect::<Vec<_>>(); + // tick_tile( + // dt, + // &self.data, + // tile.kind, + // &mut tile.active, + // items, + // |out| match out { + // Out::Take(_) | Out::Put => { + // unreachable!() + // } + // 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 }); + // } + // Out::SetActive(progress) => { + // self.packet_out.push_back(PacketC::SetActive { + // tile: pos, + // progress, + // }); + // } + // }, + // ); } } } impl From<TileIndex> for Tile { fn from(kind: TileIndex) -> Self { - Self { - kind, - items: vec![], - active: None, - } + Self { kind, item: None } } } |