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;
}
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_b = self.swap(equip_slot_b, None);
// Check if items can go in the other slots
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| 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();
if let Some(hands_swapping) = hands_swapping {
match hands_swapping {
MainhandHand::MainhandA => {
if item_b
.as_ref()
.map_or(true, |i| self.slot_can_hold(equip_slot_a, &i.kind()))
&& item_a
.as_ref()
.map_or(true, |i| equip_slot_b.can_hold(&i.kind()))
{
// 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 {
// Otherwise put the items back
self.swap(equip_slot_a, item_a).unwrap_none();
self.swap(equip_slot_b, item_b).unwrap_none();
// Check if items can go in the other slots
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| 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::{
inventory::{
item::{armor, armor::ArmorKind, ItemKind},
item::{armor, armor::ArmorKind, tool, ItemKind},
loadout::LoadoutSlotId,
},
item,
@ -123,9 +123,9 @@ impl EquipSlot {
match (self, item_kind) {
(Self::Armor(slot), ItemKind::Armor(armor::Armor { kind, .. })) => slot.can_hold(kind),
(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::InactiveOffhand, ItemKind::Tool(_)) => true,
(Self::InactiveOffhand, ItemKind::Tool(tool)) => matches!(tool.hands, tool::Hands::One),
(Self::Lantern, ItemKind::Lantern(_)) => true,
(Self::Glider, ItemKind::Glider(_)) => true,
_ => false,