Working on inventory and crafting system. Translated Excel-Itemlist to .json.

This commit is contained in:
2026-05-05 15:35:42 +02:00
parent 02ba36b665
commit fc26fa2a75
11 changed files with 829 additions and 10 deletions
+6
View File
@@ -0,0 +1,6 @@
public enum CraftingResult
{
FAILED,
CRAFTING,
FINISHED
}
+1
View File
@@ -0,0 +1 @@
uid://bd7t0os166lu3
+6 -4
View File
@@ -1,12 +1,11 @@
using Godot;
public class GameResource
public class GameResource : Ingredient
{
int maxAmount;
int currentAmount;
bool isEndless;
float extractionSpeed;
public string name;
double timeSinceLastExtraction;
public GameResource(string name)
{
@@ -17,8 +16,11 @@ public class GameResource
extractionSpeed = 1f;
}
public bool Extract()
public bool Extract(double delta)
{
timeSinceLastExtraction += delta;
if (timeSinceLastExtraction < extractionSpeed) return false;
timeSinceLastExtraction = 0;
if(isEndless) return true;
if (currentAmount > 0)
{
+8
View File
@@ -0,0 +1,8 @@
using Godot;
public abstract class Ingredient
{
public string name;
public int currentAmount;
public int stackSize;
}
+1
View File
@@ -0,0 +1 @@
uid://dital81qfptdr
+45
View File
@@ -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;
}
}
+1
View File
@@ -0,0 +1 @@
uid://dnbdfvox75hk8
+39 -1
View File
@@ -1,5 +1,43 @@
using System.Collections.Generic;
using Godot;
public class Item
public class Item : Ingredient
{
public Dictionary<Ingredient, int> ingredients;
public double craftTime;
public double elapsedCraftTime;
public int amountCrafted;
public Item(string name, Dictionary<Ingredient, int> ingredients, int stackSize, double craftTime)
{
this.name = name;
this.ingredients = ingredients;
currentAmount = 0;
this.stackSize = stackSize;
this.craftTime = craftTime;
amountCrafted = 0;
}
public CraftingResult Craft(int amount, double delta)
{
if (GameData.inventory.CanCraft(ingredients, amount-amountCrafted))
{
elapsedCraftTime += delta;
if (elapsedCraftTime >= craftTime)
{
elapsedCraftTime -= craftTime;
currentAmount += 1;
amountCrafted++;
if (amountCrafted >= amount)
{
amountCrafted = 0;
elapsedCraftTime = 0;
return CraftingResult.FINISHED;
}
}
return CraftingResult.CRAFTING;
}
return CraftingResult.FAILED;
}
}