Added item stacking

This commit is contained in:
cblech
2025-04-17 16:10:12 +02:00
parent fb362e10ad
commit fb0c14c3f1
7 changed files with 119 additions and 32 deletions
@@ -12,6 +12,7 @@ public partial class InventoryInstance : Node
[Signal]
public delegate void SlotAmountChangedEventHandler();
[Signal]
public delegate void InventoryContentsChangedEventHandler();
@@ -32,54 +33,99 @@ public partial class InventoryInstance : Node
_slots.Add(new InventorySlot());
}
}
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)
{
// find empty slot
for (var i = slotSearch; i < _slots.Count; i++)
{
if (_slots[i].IsEmpty())
{
slotIndex = i;
break;
}
}
}
if (slotIndex < 0)
{
return InventoryActionResult.DestinationFull;
}
if (inventorySlot >= _slots.Count)
{
return InventoryActionResult.DestinationDoesNotExists;
}
_slots[inventorySlot].itemInstance = newItem;
EmitSignal(SignalName.InventoryContentsChanged);
return InventoryActionResult.Success;
var itemInstance = _slots[slotIndex].itemInstance ?? new ItemInstance { blueprint = newItem.blueprint, amount = 0 };
var maxStack = itemInstance!.blueprint.maxStack;
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)
{
itemInstance = null;
return InventoryActionResult.SourceDoesNotExists;
}
if (_slots[inventorySlot].IsEmpty())
{
itemInstance = null;
return InventoryActionResult.SourceIsEmpty;
}
itemInstance = _slots[inventorySlot].itemInstance;
_slots[inventorySlot].itemInstance = null;
EmitSignal(SignalName.InventoryContentsChanged);
return InventoryActionResult.Success;
}
public InventoryActionResult RemoveItem(int inventorySlot)
{
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;
}
}