diff options
Diffstat (limited to 'server/client-lib/src/lib.rs')
-rw-r--r-- | server/client-lib/src/lib.rs | 51 |
1 files changed, 34 insertions, 17 deletions
diff --git a/server/client-lib/src/lib.rs b/server/client-lib/src/lib.rs index 54c7cd6e..a40eafc1 100644 --- a/server/client-lib/src/lib.rs +++ b/server/client-lib/src/lib.rs @@ -20,7 +20,8 @@ pub mod network; pub mod spatial_index; use hurrycurry_protocol::{ - glam::IVec2, movement::MovementBase, Gamedata, Hand, ItemIndex, ItemLocation, Message, MessageTimeout, PacketC, PlayerClass, PlayerID, RecipeIndex, Score, TileIndex + glam::IVec2, movement::MovementBase, Gamedata, Hand, ItemIndex, ItemLocation, Message, + MessageTimeout, PacketC, PlayerClass, PlayerID, RecipeIndex, Score, TileIndex, }; use spatial_index::SpatialIndex; use std::{ @@ -54,7 +55,7 @@ pub struct Player { pub class: PlayerClass, pub character: i32, pub interacting: Option<(IVec2, Hand)>, - pub items: [Option<Item>; 2], + pub items: Vec<Option<Item>>, pub communicate_persist: Option<(Message, MessageTimeout)>, pub movement: MovementBase, @@ -94,7 +95,7 @@ impl Game { character, class, interacting: None, - items: [const { None }; 2], + items: (0..self.data.hand_count).map(|_| None).collect(), communicate_persist: None, movement: MovementBase::new(position), }, @@ -116,15 +117,27 @@ impl Game { p.movement.rotation = rot; } } - PacketC::MoveItem { from, to } => { - *self.get_item(to) = self.get_item(from).take(); + if let Some(item) = self.get_item(to).map(|e| e.take()) { + if let Some(to) = self.get_item(from) { + *to = item; + } else { + // TODO perhaps restore to original position? + } + } } PacketC::SetItem { location, item } => { - *self.get_item(location) = item.map(|kind| Item { kind, active: None }); + let location = self.get_item(location); + if let Some(location) = location { + *location = item.map(|kind| Item { kind, active: None }); + } } PacketC::ClearProgress { item } => { - self.get_item(item).as_mut().unwrap().active = None; + if let Some(slot) = self.get_item(item) { + if let Some(item) = slot { + item.active = None; + } + } } PacketC::SetProgress { item, @@ -133,13 +146,17 @@ impl Game { speed, warn, } => { - self.get_item(item).as_mut().unwrap().active = Some(Involvement { - player, - speed, - warn, - position, - recipe: RecipeIndex(0), - }); + if let Some(slot) = self.get_item(item) { + if let Some(item) = slot { + item.active = Some(Involvement { + player, + speed, + warn, + position, + recipe: RecipeIndex(0), + }); + } + } } PacketC::UpdateMap { tile, @@ -224,11 +241,11 @@ impl Game { }); } - pub fn get_item(&mut self, location: ItemLocation) -> &mut Option<Item> { + pub fn get_item(&mut self, location: ItemLocation) -> Option<&mut Option<Item>> { match location { - ItemLocation::Tile(pos) => &mut self.tiles.get_mut(&pos).unwrap().item, + ItemLocation::Tile(pos) => Some(&mut self.tiles.get_mut(&pos)?.item), ItemLocation::Player(pid, hand) => { - &mut self.players.get_mut(&pid).unwrap().items[hand.index()] + Some(self.players.get_mut(&pid)?.items.get_mut(hand.0)?) } } } |