Equipped items can be swapped by double-clicking, fixed inventories getting messed up when swapping multi-slot items fails (e.g. drag a jumpsuit on an equipped diving suit).

This commit is contained in:
Joonas Rikkonen
2018-03-02 13:45:58 +02:00
parent f1f190a997
commit 07d3d69040
13 changed files with 65 additions and 30 deletions
@@ -127,7 +127,6 @@ namespace Barotrauma
if (doubleClickedItem != null) if (doubleClickedItem != null)
{ {
bool wasPut = false; bool wasPut = false;
if (doubleClickedItem.ParentInventory != this) if (doubleClickedItem.ParentInventory != this)
{ {
wasPut = TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots, true); wasPut = TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots, true);
@@ -152,7 +151,12 @@ namespace Barotrauma
//not equipped -> attempt to equip //not equipped -> attempt to equip
if (IsInLimbSlot(doubleClickedItem, InvSlotType.Any)) if (IsInLimbSlot(doubleClickedItem, InvSlotType.Any))
{ {
wasPut = TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots.FindAll(i => i != InvSlotType.Any), true); for (int i = 0; i < capacity; i++)
{
if (limbSlots[i] == InvSlotType.Any || !doubleClickedItem.AllowedSlots.Any(a => a.HasFlag(limbSlots[i]))) continue;
wasPut = TryPutItem(doubleClickedItem, i, true, false, Character.Controlled, true);
if (wasPut) break;
}
} }
//equipped -> attempt to unequip //equipped -> attempt to unequip
else if (doubleClickedItem.AllowedSlots.Contains(InvSlotType.Any)) else if (doubleClickedItem.AllowedSlots.Contains(InvSlotType.Any))
@@ -162,6 +166,14 @@ namespace Barotrauma
} }
} }
if (wasPut)
{
for (int i = 0; i < capacity; i++)
{
if (Items[i] == doubleClickedItem) slots[i].ShowBorderHighlight(Color.Green, 0.1f, 0.9f);
}
}
draggingItem = null; draggingItem = null;
GUI.PlayUISound(wasPut ? GUISoundType.PickItem : GUISoundType.PickItemFail); GUI.PlayUISound(wasPut ? GUISoundType.PickItem : GUISoundType.PickItemFail);
} }
@@ -386,7 +386,7 @@ namespace Barotrauma
{ {
Inventory selectedInventory = selectedSlot.Inventory; Inventory selectedInventory = selectedSlot.Inventory;
int slotIndex = selectedSlot.SlotIndex; int slotIndex = selectedSlot.SlotIndex;
if (selectedInventory.TryPutItem(draggingItem, slotIndex, true, Character.Controlled)) if (selectedInventory.TryPutItem(draggingItem, slotIndex, true, true, Character.Controlled))
{ {
if (selectedInventory.slots != null) selectedInventory.slots[slotIndex].ShowBorderHighlight(Color.White, 0.1f, 0.4f); if (selectedInventory.slots != null) selectedInventory.slots[slotIndex].ShowBorderHighlight(Color.White, 0.1f, 0.4f);
GUI.PlayUISound(GUISoundType.PickItem); GUI.PlayUISound(GUISoundType.PickItem);
@@ -936,7 +936,7 @@ namespace Barotrauma
existingWire.Remove(); existingWire.Remove();
} }
dummyCharacter.Inventory.TryPutItem(wire, slotIndex, false, dummyCharacter); dummyCharacter.Inventory.TryPutItem(wire, slotIndex, false, false, dummyCharacter);
return true; return true;
@@ -50,7 +50,7 @@ namespace Barotrauma
//TODO: make sure the weapon is ready to use (projectiles/batteries loaded) //TODO: make sure the weapon is ready to use (projectiles/batteries loaded)
if (!character.SelectedItems.Contains(weapon)) if (!character.SelectedItems.Contains(weapon))
{ {
if (character.Inventory.TryPutItem(weapon, 3, false, character)) if (character.Inventory.TryPutItem(weapon, 3, false, false, character))
{ {
weapon.Equip(character); weapon.Equip(character);
} }
@@ -119,7 +119,7 @@ namespace Barotrauma
if (targetSlot > -1 && character.Inventory.IsInLimbSlot(targetItem, InvSlotType.Any)) if (targetSlot > -1 && character.Inventory.IsInLimbSlot(targetItem, InvSlotType.Any))
{ {
character.Inventory.TryPutItem(targetItem, targetSlot, false, character); character.Inventory.TryPutItem(targetItem, targetSlot, false, false, character);
} }
} }
else else
@@ -113,7 +113,7 @@ namespace Barotrauma
character.Inventory.Items[i].Drop(); character.Inventory.Items[i].Drop();
} }
} }
if (character.Inventory.TryPutItem(component.Item, i, true, character)) if (character.Inventory.TryPutItem(component.Item, i, true, false, character))
{ {
component.Item.Equip(character); component.Item.Equip(character);
break; break;
@@ -657,7 +657,7 @@ namespace Barotrauma
if (item == null) continue; if (item == null) continue;
item.TryInteract(this, true, true, true); item.TryInteract(this, true, true, true);
inventory.TryPutItem(item, i, false, null, false); inventory.TryPutItem(item, i, false, false, null, false);
} }
} }
} }
@@ -214,7 +214,7 @@ namespace Barotrauma
for (int i = 0; i < character.Inventory.Items.Length; i++) for (int i = 0; i < character.Inventory.Items.Length; i++)
{ {
if (character.Inventory.Items[i] == null) continue; if (character.Inventory.Items[i] == null) continue;
husk.Inventory.TryPutItem(character.Inventory.Items[i], i, true, null); husk.Inventory.TryPutItem(character.Inventory.Items[i], i, true, false, null);
} }
yield return CoroutineStatus.Success; yield return CoroutineStatus.Success;
@@ -113,7 +113,7 @@ namespace Barotrauma
bool free = true; bool free = true;
for (int i = 0; i < capacity; i++) for (int i = 0; i < capacity; i++)
{ {
if (allowedSlot.HasFlag(limbSlots[i]) && Items[i]!=null && Items[i]!=item) if (allowedSlot.HasFlag(limbSlots[i]) && Items[i] != null && Items[i] != item)
{ {
free = false; free = false;
#if CLIENT #if CLIENT
@@ -144,7 +144,7 @@ namespace Barotrauma
return placed; return placed;
} }
public override bool TryPutItem(Item item, int index, bool allowSwapping, Character user, bool createNetworkEvent = true) public override bool TryPutItem(Item item, int index, bool allowSwapping, bool allowCombine, Character user, bool createNetworkEvent = true)
{ {
//there's already an item in the slot //there's already an item in the slot
if (Items[index] != null) if (Items[index] != null)
@@ -152,7 +152,7 @@ namespace Barotrauma
if (Items[index] == item) return false; if (Items[index] == item) return false;
bool combined = false; bool combined = false;
if (Items[index].Combine(item)) if (allowCombine && Items[index].Combine(item))
{ {
System.Diagnostics.Debug.Assert(Items[index] != null); System.Diagnostics.Debug.Assert(Items[index] != null);
@@ -171,22 +171,45 @@ namespace Barotrauma
Item existingItem = Items[index]; Item existingItem = Items[index];
Items[currentIndex] = null; for (int i = 0; i < capacity; i++)
Items[index] = null;
//if the item in the slot can be moved to the slot of the moved item
if (TryPutItem(existingItem, currentIndex, false, user, createNetworkEvent) &&
TryPutItem(item, index, false, user, createNetworkEvent))
{ {
if (Items[i] == item || Items[i] == existingItem) Items[i] = null;
}
//if the item in the slot can be moved to the slot of the moved item
if (TryPutItem(existingItem, currentIndex, false, false, user, createNetworkEvent) &&
TryPutItem(item, index, false, false, user, createNetworkEvent))
{
#if CLIENT
for (int i = 0; i < capacity; i++)
{
if (Items[i] == item || Items[i] == existingItem)
{
slots[i].ShowBorderHighlight(Color.Green, 0.1f, 0.9f);
}
}
#endif
} }
else else
{ {
Items[currentIndex] = null; for (int i = 0; i < capacity; i++)
Items[index] = null; {
if (Items[i] == item || Items[i] == existingItem) Items[i] = null;
}
//swapping the items failed -> move them back to where they were //swapping the items failed -> move them back to where they were
TryPutItem(item, currentIndex, false, user, createNetworkEvent); TryPutItem(item, currentIndex, false, false, user, createNetworkEvent);
TryPutItem(existingItem, index, false, user, createNetworkEvent); TryPutItem(existingItem, index, false, false, user, createNetworkEvent);
#if CLIENT
for (int i = 0; i < capacity; i++)
{
if (Items[i] == existingItem)
{
slots[i].ShowBorderHighlight(Color.Red, 0.1f, 0.9f);
}
}
#endif
} }
} }
@@ -204,7 +204,7 @@ namespace Barotrauma.Items.Components
Item item = Entity.FindEntityByID(itemIds[i]) as Item; Item item = Entity.FindEntityByID(itemIds[i]) as Item;
if (item == null) continue; if (item == null) continue;
Inventory.TryPutItem(item, i, false, null, false); Inventory.TryPutItem(item, i, false, false, null, false);
} }
itemIds = null; itemIds = null;
@@ -95,7 +95,7 @@ namespace Barotrauma
return true; return true;
} }
public virtual bool TryPutItem(Item item, int i, bool allowSwapping, Character user, bool createNetworkEvent = true) public virtual bool TryPutItem(Item item, int i, bool allowSwapping, bool allowCombine, Character user, bool createNetworkEvent = true)
{ {
if (Owner == null) return false; if (Owner == null) return false;
if (CanBePut(item, i)) if (CanBePut(item, i))
@@ -224,7 +224,7 @@ namespace Barotrauma
{ {
if (!item.CanClientAccess(c)) continue; if (!item.CanClientAccess(c)) continue;
} }
TryPutItem(item, i, true, c.Character, false); TryPutItem(item, i, true, true, c.Character, false);
} }
} }
@@ -321,7 +321,7 @@ namespace Barotrauma
var item = Entity.FindEntityByID(receivedItemIDs[i]) as Item; var item = Entity.FindEntityByID(receivedItemIDs[i]) as Item;
if (item == null) continue; if (item == null) continue;
TryPutItem(item, i, true, null, false); TryPutItem(item, i, true, true, null, false);
} }
} }
@@ -1642,7 +1642,7 @@ namespace Barotrauma
if (inventory != null) if (inventory != null)
{ {
if (inventorySlotIndex >= 0 && inventorySlotIndex < 255 && if (inventorySlotIndex >= 0 && inventorySlotIndex < 255 &&
inventory.TryPutItem(item, inventorySlotIndex, false, null, false)) inventory.TryPutItem(item, inventorySlotIndex, false, false, null, false))
{ {
return null; return null;
} }
@@ -63,9 +63,9 @@ namespace Barotrauma
return wasPut; return wasPut;
} }
public override bool TryPutItem(Item item, int i, bool allowSwapping, Character user, bool createNetworkEvent = true) public override bool TryPutItem(Item item, int i, bool allowSwapping, bool allowCombine, Character user, bool createNetworkEvent = true)
{ {
bool wasPut = base.TryPutItem(item, i, allowSwapping, user, createNetworkEvent); bool wasPut = base.TryPutItem(item, i, allowSwapping, allowCombine, user, createNetworkEvent);
if (wasPut) if (wasPut)
{ {