51 lines
1.2 KiB
C#
51 lines
1.2 KiB
C#
|
|
namespace Ashwild.Inventory
|
|
{
|
|
[System.Serializable]
|
|
public class InventorySlot
|
|
{
|
|
private ItemData itemData;
|
|
private int quantity;
|
|
|
|
public ItemData ItemData => itemData;
|
|
public int Quantity => quantity;
|
|
public bool IsEmpty => itemData == null;
|
|
|
|
public void Set(ItemData data, int qty)
|
|
{
|
|
itemData = data;
|
|
quantity = qty;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
itemData = null;
|
|
quantity = 0;
|
|
}
|
|
|
|
public int AddQuantity(int amount)
|
|
{
|
|
if (itemData == null || !itemData.IsStackable)
|
|
return amount;
|
|
|
|
int space = itemData.MaxStackSize - quantity;
|
|
int toAdd = amount < space ? amount : space;
|
|
quantity += toAdd;
|
|
return amount - toAdd;
|
|
}
|
|
|
|
public void RemoveQuantity(int amount)
|
|
{
|
|
quantity -= amount;
|
|
if (quantity <= 0)
|
|
Clear();
|
|
}
|
|
|
|
public bool CanAccept(ItemData data)
|
|
{
|
|
if (IsEmpty) return true;
|
|
return itemData == data && itemData.IsStackable && quantity < itemData.MaxStackSize;
|
|
}
|
|
}
|
|
}
|