61 lines
1.5 KiB
C#
61 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
public class Inventory
|
|
{
|
|
public List<Item> items = new List<Item>();
|
|
|
|
public int maxInventorySize = 8;
|
|
public event EventHandler OnInventoryUpdate;
|
|
|
|
public bool AddItem(Item item, int amount)
|
|
{
|
|
Item inventoryItem = items.Find(x => x.data.Id == item.data.Id && x.currentAmount + amount <= x.data.StackSize);
|
|
if (inventoryItem != null)
|
|
{
|
|
inventoryItem.currentAmount += amount;
|
|
NotifyInventoryChanged();
|
|
return true;
|
|
}
|
|
|
|
if (items.Count < maxInventorySize)
|
|
{
|
|
items.Add(item);
|
|
items[items.Count - 1].currentAmount += amount;
|
|
NotifyInventoryChanged();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool CanCraft(List<Ingredient> neededIngredients, int amount)
|
|
{
|
|
foreach (Ingredient ingredient in neededIngredients)
|
|
{
|
|
Item item = items.Find(x => x.data.Id == ingredient.Item && x.currentAmount >= ingredient.Amount * amount);
|
|
if (item == null)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public void RemoveItem(string id, int amount)
|
|
{
|
|
Item item = items.Find(x => x.data.Id == id && x.currentAmount >= amount);
|
|
if (item != null)
|
|
{
|
|
item.currentAmount -= amount;
|
|
NotifyInventoryChanged();
|
|
}
|
|
}
|
|
|
|
private void NotifyInventoryChanged()
|
|
{
|
|
OnInventoryUpdate?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|