55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using Godot;
|
|
|
|
public class Inventory
|
|
{
|
|
public List<Item> items = new();
|
|
|
|
public int maxInventorySize = 8;
|
|
|
|
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)
|
|
{
|
|
GD.Print(items.IndexOf(inventoryItem) + ": " + inventoryItem.currentAmount);
|
|
inventoryItem.currentAmount += amount;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
if (items.Count < maxInventorySize)
|
|
{
|
|
items.Add(item);
|
|
items[items.Count - 1].currentAmount += amount;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool CanCraft(List<Ingredient> neededIngredients, int amount)
|
|
{
|
|
bool canCraft = true;
|
|
Item item;
|
|
foreach (Ingredient ingredient in neededIngredients)
|
|
{
|
|
item = items.Find(x => x.data.Id == ingredient.Item && x.currentAmount >= ingredient.Amount * amount);
|
|
if (item == null)
|
|
{
|
|
canCraft = false;
|
|
break;
|
|
}
|
|
}
|
|
return canCraft;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |