Now allows weapons to be swapped between the mainhand and offhand slot. (Probably hacky?)

This commit is contained in:
Sam 2021-05-15 14:01:37 -05:00
parent 6b153bcf47
commit 23cc3d671b
2 changed files with 77 additions and 17 deletions

View File

@ -167,24 +167,84 @@ impl Loadout {
return; return;
} }
enum MainhandHand {
MainhandA,
MainhandB,
}
let hands_swapping = match (equip_slot_a, equip_slot_b) {
(EquipSlot::ActiveMainhand, EquipSlot::ActiveOffhand) => Some(MainhandHand::MainhandA),
(EquipSlot::ActiveOffhand, EquipSlot::ActiveMainhand) => Some(MainhandHand::MainhandB),
(EquipSlot::InactiveMainhand, EquipSlot::InactiveOffhand) => {
Some(MainhandHand::MainhandA)
},
(EquipSlot::InactiveOffhand, EquipSlot::InactiveMainhand) => {
Some(MainhandHand::MainhandB)
},
_ => None,
};
let item_a = self.swap(equip_slot_a, None); let item_a = self.swap(equip_slot_a, None);
let item_b = self.swap(equip_slot_b, None); let item_b = self.swap(equip_slot_b, None);
// Check if items can go in the other slots if let Some(hands_swapping) = hands_swapping {
if item_a match hands_swapping {
.as_ref() MainhandHand::MainhandA => {
.map_or(true, |i| self.slot_can_hold(equip_slot_b, &i.kind())) if item_b
&& item_b .as_ref()
.as_ref() .map_or(true, |i| self.slot_can_hold(equip_slot_a, &i.kind()))
.map_or(true, |i| self.slot_can_hold(equip_slot_a, &i.kind())) && item_a
{ .as_ref()
// Swap .map_or(true, |i| equip_slot_b.can_hold(&i.kind()))
self.swap(equip_slot_b, item_a).unwrap_none(); {
self.swap(equip_slot_a, item_b).unwrap_none(); // Checks that item b (from offhand) can go into equip slot a (mainhand
// slot) and that item a (from mainhand) is a valid item to insert into
// equip slot b (offhand slot) Swap
self.swap(equip_slot_a, item_b).unwrap_none();
self.swap(equip_slot_b, item_a).unwrap_none();
} else {
// Otherwise put the items back
self.swap(equip_slot_a, item_a).unwrap_none();
self.swap(equip_slot_b, item_b).unwrap_none();
}
},
MainhandHand::MainhandB => {
if item_a
.as_ref()
.map_or(true, |i| self.slot_can_hold(equip_slot_b, &i.kind()))
&& item_b
.as_ref()
.map_or(true, |i| equip_slot_b.can_hold(&i.kind()))
{
// Checks that item a (from offhand) can go into equip slot b (mainhand
// slot) and that item b (from mainhand) is a valid item to insert into
// equip slot a (offhand slot) Swap
self.swap(equip_slot_b, item_a).unwrap_none();
self.swap(equip_slot_a, item_b).unwrap_none();
} else {
// Otherwise put the items back
self.swap(equip_slot_a, item_a).unwrap_none();
self.swap(equip_slot_b, item_b).unwrap_none();
}
},
}
} else { } else {
// Otherwise put the items back // Check if items can go in the other slots
self.swap(equip_slot_a, item_a).unwrap_none(); if item_a
self.swap(equip_slot_b, item_b).unwrap_none(); .as_ref()
.map_or(true, |i| self.slot_can_hold(equip_slot_b, &i.kind()))
&& item_b
.as_ref()
.map_or(true, |i| self.slot_can_hold(equip_slot_a, &i.kind()))
{
// Swap
self.swap(equip_slot_b, item_a).unwrap_none();
self.swap(equip_slot_a, item_b).unwrap_none();
} else {
// Otherwise put the items back
self.swap(equip_slot_a, item_a).unwrap_none();
self.swap(equip_slot_b, item_b).unwrap_none();
}
} }
} }

View File

@ -3,7 +3,7 @@ use std::{cmp::Ordering, convert::TryFrom};
use crate::comp::{ use crate::comp::{
inventory::{ inventory::{
item::{armor, armor::ArmorKind, ItemKind}, item::{armor, armor::ArmorKind, tool, ItemKind},
loadout::LoadoutSlotId, loadout::LoadoutSlotId,
}, },
item, item,
@ -123,9 +123,9 @@ impl EquipSlot {
match (self, item_kind) { match (self, item_kind) {
(Self::Armor(slot), ItemKind::Armor(armor::Armor { kind, .. })) => slot.can_hold(kind), (Self::Armor(slot), ItemKind::Armor(armor::Armor { kind, .. })) => slot.can_hold(kind),
(Self::ActiveMainhand, ItemKind::Tool(_)) => true, (Self::ActiveMainhand, ItemKind::Tool(_)) => true,
(Self::ActiveOffhand, ItemKind::Tool(_)) => true, (Self::ActiveOffhand, ItemKind::Tool(tool)) => matches!(tool.hands, tool::Hands::One),
(Self::InactiveMainhand, ItemKind::Tool(_)) => true, (Self::InactiveMainhand, ItemKind::Tool(_)) => true,
(Self::InactiveOffhand, ItemKind::Tool(_)) => true, (Self::InactiveOffhand, ItemKind::Tool(tool)) => matches!(tool.hands, tool::Hands::One),
(Self::Lantern, ItemKind::Lantern(_)) => true, (Self::Lantern, ItemKind::Lantern(_)) => true,
(Self::Glider, ItemKind::Glider(_)) => true, (Self::Glider, ItemKind::Glider(_)) => true,
_ => false, _ => false,