|
|
|
@ -12,6 +12,7 @@ public partial class InventoryInstance : Node
|
|
|
|
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
[Signal]
|
|
|
|
public delegate void SlotAmountChangedEventHandler();
|
|
|
|
public delegate void SlotAmountChangedEventHandler();
|
|
|
|
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
[Signal]
|
|
|
|
public delegate void InventoryContentsChangedEventHandler();
|
|
|
|
public delegate void InventoryContentsChangedEventHandler();
|
|
|
|
|
|
|
|
|
|
|
|
@ -32,33 +33,65 @@ public partial class InventoryInstance : Node
|
|
|
|
_slots.Add(new InventorySlot());
|
|
|
|
_slots.Add(new InventorySlot());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EmitSignal(SignalName.SlotAmountChanged);
|
|
|
|
EmitSignal(SignalName.SlotAmountChanged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public InventoryActionResult AddItem(ItemInstance newItem, int inventorySlot = -1)
|
|
|
|
public InventoryActionResult AddItem(ItemInstance newItem)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (inventorySlot < 0)
|
|
|
|
var result = AddItemAndStackRecursive(newItem, 0);
|
|
|
|
|
|
|
|
EmitSignal(SignalName.InventoryContentsChanged);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private InventoryActionResult AddItemAndStackRecursive(ItemInstance newItem, int slotSearch)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (newItem.blueprint == null || newItem.amount == 0)
|
|
|
|
|
|
|
|
return InventoryActionResult.SourceDoesNotExists;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var slotIndex = -1;
|
|
|
|
|
|
|
|
// find stackable slot
|
|
|
|
|
|
|
|
for (var i = slotSearch; i < _slots.Count; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
inventorySlot = _slots.FindIndex(slot => slot.IsEmpty());
|
|
|
|
if (_slots[i].itemInstance?.blueprint == newItem.blueprint)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
slotIndex = i;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (inventorySlot < 0 || !_slots[inventorySlot].IsEmpty())
|
|
|
|
if (slotIndex < 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return InventoryActionResult.DestinationFull;
|
|
|
|
// find empty slot
|
|
|
|
|
|
|
|
for (var i = slotSearch; i < _slots.Count; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_slots[i].IsEmpty())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
slotIndex = i;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (inventorySlot >= _slots.Count)
|
|
|
|
if (slotIndex < 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return InventoryActionResult.DestinationDoesNotExists;
|
|
|
|
return InventoryActionResult.DestinationFull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_slots[inventorySlot].itemInstance = newItem;
|
|
|
|
var itemInstance = _slots[slotIndex].itemInstance ?? new ItemInstance { blueprint = newItem.blueprint, amount = 0 };
|
|
|
|
EmitSignal(SignalName.InventoryContentsChanged);
|
|
|
|
var maxStack = itemInstance!.blueprint.maxStack;
|
|
|
|
return InventoryActionResult.Success;
|
|
|
|
var freeOnStack = maxStack - itemInstance.amount;
|
|
|
|
|
|
|
|
var moveAmount = Math.Min(freeOnStack, newItem.amount);
|
|
|
|
|
|
|
|
itemInstance.amount += moveAmount;
|
|
|
|
|
|
|
|
newItem.amount -= moveAmount;
|
|
|
|
|
|
|
|
_slots[slotIndex].itemInstance = itemInstance;
|
|
|
|
|
|
|
|
return newItem.amount <= 0
|
|
|
|
|
|
|
|
? InventoryActionResult.Success
|
|
|
|
|
|
|
|
: AddItemAndStackRecursive(newItem, slotIndex + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public InventoryActionResult RemoveItem(int inventorySlot, out ItemInstance? itemInstance )
|
|
|
|
public InventoryActionResult RemoveItem(int inventorySlot, out ItemInstance? itemInstance)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (inventorySlot < 0 || inventorySlot >= _slots.Count)
|
|
|
|
if (inventorySlot < 0 || inventorySlot >= _slots.Count)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -82,4 +115,17 @@ public partial class InventoryInstance : Node
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return RemoveItem(inventorySlot, out _);
|
|
|
|
return RemoveItem(inventorySlot, out _);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public InventoryActionResult AddItemToSlot(ItemInstance itemInstance, int destinationSlot)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (destinationSlot < 0 || destinationSlot >= _slots.Count)
|
|
|
|
|
|
|
|
return InventoryActionResult.DestinationDoesNotExists;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!_slots[destinationSlot].IsEmpty())
|
|
|
|
|
|
|
|
return InventoryActionResult.DestinationFull;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_slots[destinationSlot].itemInstance = itemInstance;
|
|
|
|
|
|
|
|
EmitSignal(SignalName.InventoryContentsChanged);
|
|
|
|
|
|
|
|
return InventoryActionResult.Success;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|