From 19926a2322c43578e74ac22d43d003471d50e8b8 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 15 May 2021 14:20:15 -0500 Subject: [PATCH] Swapping weapon sets is now functional. (Though very hacky?) --- common/src/comp/inventory/loadout.rs | 30 ++++++++++++++++++++++ common/src/comp/inventory/mod.rs | 2 ++ common/systems/src/character_behavior.rs | 32 +++--------------------- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/common/src/comp/inventory/loadout.rs b/common/src/comp/inventory/loadout.rs index 7b4ba29ffb..0d02b9c8d6 100644 --- a/common/src/comp/inventory/loadout.rs +++ b/common/src/comp/inventory/loadout.rs @@ -423,6 +423,36 @@ impl Loadout { _ => equip_slot.can_hold(item_kind), } } + + pub(super) fn swap_equipped_weapons(&mut self) { + // Checks if a given slot can hold an item right now, defaults to true if + // nothing is equipped in slot + let valid_slot = |equip_slot| { + self.equipped(equip_slot) + .map_or(true, |i| self.slot_can_hold(equip_slot, i.kind())) + }; + + if valid_slot(EquipSlot::ActiveMainhand) + && valid_slot(EquipSlot::ActiveOffhand) + && valid_slot(EquipSlot::InactiveMainhand) + && valid_slot(EquipSlot::InactiveOffhand) + { + // Get weapons from each slot + let active_mainhand = self.swap(EquipSlot::ActiveMainhand, None); + let active_offhand = self.swap(EquipSlot::ActiveOffhand, None); + let inactive_mainhand = self.swap(EquipSlot::InactiveMainhand, None); + let inactive_offhand = self.swap(EquipSlot::InactiveOffhand, None); + // Equip weapons into new slots + self.swap(EquipSlot::ActiveMainhand, inactive_mainhand) + .unwrap_none(); + self.swap(EquipSlot::ActiveOffhand, inactive_offhand) + .unwrap_none(); + self.swap(EquipSlot::InactiveMainhand, active_mainhand) + .unwrap_none(); + self.swap(EquipSlot::InactiveOffhand, active_offhand) + .unwrap_none(); + } + } } #[cfg(test)] diff --git a/common/src/comp/inventory/mod.rs b/common/src/comp/inventory/mod.rs index 5bc40e586b..08440a2a43 100644 --- a/common/src/comp/inventory/mod.rs +++ b/common/src/comp/inventory/mod.rs @@ -778,6 +778,8 @@ impl Inventory { pub fn equipped_items_of_kind(&self, item_kind: ItemKind) -> impl Iterator { self.loadout.equipped_items_of_kind(item_kind) } + + pub fn swap_equipped_weapons(&mut self) { self.loadout.swap_equipped_weapons() } } impl Component for Inventory { diff --git a/common/systems/src/character_behavior.rs b/common/systems/src/character_behavior.rs index 7b9b4cb62d..20590f825f 100644 --- a/common/systems/src/character_behavior.rs +++ b/common/systems/src/character_behavior.rs @@ -5,14 +5,9 @@ use specs::{ use common::{ comp::{ - self, - inventory::{ - item::MaterialStatManifest, - slot::{EquipSlot, Slot}, - }, - Beam, Body, CharacterState, Combo, Controller, Density, Energy, Health, Inventory, Mass, - Melee, Mounting, Ori, PhysicsState, Poise, PoiseState, Pos, SkillSet, StateUpdate, Stats, - Vel, + self, inventory::item::MaterialStatManifest, Beam, Body, CharacterState, Combo, Controller, + Density, Energy, Health, Inventory, Mass, Melee, Mounting, Ori, PhysicsState, Poise, + PoiseState, Pos, SkillSet, StateUpdate, Stats, Vel, }, event::{EventBus, LocalEvent, ServerEvent}, outcome::Outcome, @@ -48,26 +43,7 @@ fn incorporate_update(join: &mut JoinStruct, mut state_update: StateUpdate) { if state_update.swap_equipped_weapons { let mut inventory = join.inventory.get_mut_unchecked(); let inventory = &mut *inventory; - assert!( - inventory - .swap( - Slot::Equip(EquipSlot::ActiveMainhand), - Slot::Equip(EquipSlot::InactiveMainhand), - ) - .first() - .is_none(), - "Swapping active and inactive mainhand never results in leftover items", - ); - assert!( - inventory - .swap( - Slot::Equip(EquipSlot::ActiveOffhand), - Slot::Equip(EquipSlot::InactiveOffhand), - ) - .first() - .is_none(), - "Swapping active and inactive offhand never results in leftover items", - ); + inventory.swap_equipped_weapons(); } }