diff options
author | metamuffin <metamuffin@noreply.codeberg.org> | 2024-12-25 19:05:05 +0000 |
---|---|---|
committer | metamuffin <metamuffin@noreply.codeberg.org> | 2024-12-25 19:05:05 +0000 |
commit | cc6b50debb9d5b740adbe6f803755413c972659a (patch) | |
tree | b34a0a5669707992f1334f88a1959d5b1e120415 /server/client-lib/src/lib.rs | |
parent | 2ceeea0e5fc245602618ec47f6ff1f91a094e130 (diff) | |
parent | 53cf167c08986caf346957d1f357cefaee1bd6b5 (diff) | |
download | hurrycurry-cc6b50debb9d5b740adbe6f803755413c972659a.tar hurrycurry-cc6b50debb9d5b740adbe6f803755413c972659a.tar.bz2 hurrycurry-cc6b50debb9d5b740adbe6f803755413c972659a.tar.zst |
Merge pull request 'Two-handed players' (#236) from two-handed into master
Reviewed-on: https://codeberg.org/hurrycurry/hurrycurry/pulls/236
Diffstat (limited to 'server/client-lib/src/lib.rs')
-rw-r--r-- | server/client-lib/src/lib.rs | 62 |
1 files changed, 41 insertions, 21 deletions
diff --git a/server/client-lib/src/lib.rs b/server/client-lib/src/lib.rs index 5d5e55d5..a40eafc1 100644 --- a/server/client-lib/src/lib.rs +++ b/server/client-lib/src/lib.rs @@ -20,7 +20,7 @@ pub mod network; pub mod spatial_index; use hurrycurry_protocol::{ - glam::IVec2, movement::MovementBase, Gamedata, ItemIndex, ItemLocation, Message, + glam::IVec2, movement::MovementBase, Gamedata, Hand, ItemIndex, ItemLocation, Message, MessageTimeout, PacketC, PlayerClass, PlayerID, RecipeIndex, Score, TileIndex, }; use spatial_index::SpatialIndex; @@ -54,8 +54,8 @@ pub struct Player { pub name: String, pub class: PlayerClass, pub character: i32, - pub interacting: Option<IVec2>, - pub item: Option<Item>, + pub interacting: Option<(IVec2, Hand)>, + pub items: Vec<Option<Item>>, pub communicate_persist: Option<(Message, MessageTimeout)>, pub movement: MovementBase, @@ -95,7 +95,7 @@ impl Game { character, class, interacting: None, - item: None, + items: (0..self.data.hand_count).map(|_| None).collect(), communicate_persist: None, movement: MovementBase::new(position), }, @@ -117,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, @@ -134,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, @@ -200,9 +216,11 @@ impl Game { } for player in self.players.values_mut() { - if let Some(item) = &mut player.item { - if let Some(active) = &mut item.active { - active.position += active.speed; + for item in &mut player.items { + if let Some(item) = item { + if let Some(active) = &mut item.active { + active.position += active.speed; + } } } } @@ -223,10 +241,12 @@ 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::Player(pid) => &mut self.players.get_mut(&pid).unwrap().item, + ItemLocation::Tile(pos) => Some(&mut self.tiles.get_mut(&pos)?.item), + ItemLocation::Player(pid, hand) => { + Some(self.players.get_mut(&pid)?.items.get_mut(hand.0)?) + } } } } |