Working on inventory and crafting system. Translated Excel-Itemlist to .json.
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
public class Inventory
|
||||
{
|
||||
public List<Ingredient> inventory = new();
|
||||
|
||||
public int maxInventorySize = 8;
|
||||
|
||||
public bool AddIngredient(Ingredient ingredient, int amount)
|
||||
{
|
||||
Ingredient ing = inventory.Find(x => x.name == ingredient.name && x.currentAmount + amount <= x.stackSize);
|
||||
if (ing != null)
|
||||
{
|
||||
ing.currentAmount += amount;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (inventory.Count < maxInventorySize)
|
||||
{
|
||||
inventory.Add(ingredient);
|
||||
inventory[inventory.Count - 1].currentAmount += amount;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool CanCraft(Dictionary<Ingredient, int> neededIngredients, int amount)
|
||||
{
|
||||
bool canCraft = true;
|
||||
Ingredient ingredient;
|
||||
foreach(Ingredient key in neededIngredients.Keys)
|
||||
{
|
||||
ingredient = inventory.Find(x => x.name == key.name && x.currentAmount >= neededIngredients[key] * amount);
|
||||
if (ingredient == null)
|
||||
{
|
||||
canCraft = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return canCraft;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user