Improved recipes and added resources to it. Changed how ingredients and items work.

This commit is contained in:
=
2026-05-05 19:26:31 +02:00
parent fc26fa2a75
commit a320a86a2f
9 changed files with 248 additions and 135 deletions
+6 -1
View File
@@ -1,11 +1,14 @@
using Godot;
public class GameResource : Ingredient
public class GameResource
{
public string name;
int currentAmount;
int maxAmount;
bool isEndless;
float extractionSpeed;
double timeSinceLastExtraction;
ItemData item;
public GameResource(string name)
{
@@ -14,6 +17,7 @@ public class GameResource : Ingredient
currentAmount = maxAmount;
isEndless = false;
extractionSpeed = 1f;
item = GameData.availableItems[name];
}
public bool Extract(double delta)
@@ -25,6 +29,7 @@ public class GameResource : Ingredient
if (currentAmount > 0)
{
currentAmount--;
return true;
}
return false;
+3 -4
View File
@@ -1,8 +1,7 @@
using Godot;
public abstract class Ingredient
public class Ingredient
{
public string name;
public int currentAmount;
public int stackSize;
public string item;
public int amount;
}
+11 -11
View File
@@ -3,23 +3,23 @@ using Godot;
public class Inventory
{
public List<Ingredient> inventory = new();
public List<Item> inventory = new();
public int maxInventorySize = 8;
public bool AddIngredient(Ingredient ingredient, int amount)
public bool AddItem(Item item, int amount)
{
Ingredient ing = inventory.Find(x => x.name == ingredient.name && x.currentAmount + amount <= x.stackSize);
if (ing != null)
Item inventoryItem = inventory.Find(x => x.data.Id == item.data.Id && x.currentAmount + amount <= x.data.StackSize);
if (inventoryItem != null)
{
ing.currentAmount += amount;
inventoryItem.currentAmount += amount;
return true;
}
else
{
if (inventory.Count < maxInventorySize)
{
inventory.Add(ingredient);
inventory.Add(item);
inventory[inventory.Count - 1].currentAmount += amount;
return true;
}
@@ -27,14 +27,14 @@ public class Inventory
return false;
}
public bool CanCraft(Dictionary<Ingredient, int> neededIngredients, int amount)
public bool CanCraft(List<Ingredient> neededIngredients, int amount)
{
bool canCraft = true;
Ingredient ingredient;
foreach(Ingredient key in neededIngredients.Keys)
Item item;
foreach(Ingredient ingredient in neededIngredients)
{
ingredient = inventory.Find(x => x.name == key.name && x.currentAmount >= neededIngredients[key] * amount);
if (ingredient == null)
item = inventory.Find(x => x.data.Id == ingredient.item && x.currentAmount >= ingredient.amount * amount);
if (item == null)
{
canCraft = false;
break;
+12 -20
View File
@@ -1,32 +1,23 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Godot;
public class Item : Ingredient
public class Item
{
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 ItemData data;
public int currentAmount = 0;
public double elapsedCraftTime = 0;
public int amountCrafted = 0;
public Texture2D display;
public CraftingResult Craft(int amount, double delta)
{
if (GameData.inventory.CanCraft(ingredients, amount-amountCrafted))
if (GameData.inventory.CanCraft(data.Inputs, amount - amountCrafted))
{
elapsedCraftTime += delta;
if (elapsedCraftTime >= craftTime)
if (elapsedCraftTime >= data.CraftTime)
{
elapsedCraftTime -= craftTime;
elapsedCraftTime -= data.CraftTime;
currentAmount += 1;
amountCrafted++;
if (amountCrafted >= amount)
@@ -40,4 +31,5 @@ public class Item : Ingredient
}
return CraftingResult.FAILED;
}
}
}
+30
View File
@@ -0,0 +1,30 @@
using Godot;
using System.Collections.Generic;
using System.Text.Json.Serialization;
public class ItemData
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("inputs")]
public List<Ingredient> Inputs { get; set; }
[JsonPropertyName("output")]
public Ingredient Output { get; set; }
[JsonPropertyName("workstation")]
public string Workstation { get; set; }
[JsonPropertyName("texture")]
public string Texture { get; set; }
[JsonPropertyName("research")]
public string Research { get; set; }
[JsonPropertyName("crafttime")]
public double CraftTime { get; set; }
[JsonPropertyName("stacksize")]
public int StackSize { get; set; }
}
+1
View File
@@ -0,0 +1 @@
uid://cj8yubkrsdq70
+1 -1
View File
@@ -17,7 +17,7 @@ public partial class GameData
public static float robotSpeed = 20f;
public static float tileWidth = 6;
public static float tileHeight = 4;
public static Dictionary<Ingredient, Texture2D> availableIngredients = ResourceLoader.LoadIngredients();
public static Dictionary<string, ItemData> availableItems = ResourceLoader.LoadItems();
//--- PLAYER ADJUSTABLE VALUES ---
//Color used in primary objects (e.g. Robots)
+29 -20
View File
@@ -2,6 +2,7 @@ using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
public partial class ResourceLoader
{
@@ -19,7 +20,7 @@ public partial class ResourceLoader
{
return GD.Load<PackedScene>($"res://Prefabs/Robot/RobotDisplay.tscn");
}
public static Dictionary<string, MeshInstance3D> LoadTiles()
{
Dictionary<string, MeshInstance3D> tileMeshes = new Dictionary<string, MeshInstance3D>();
@@ -49,37 +50,45 @@ public partial class ResourceLoader
public static Dictionary<ProgramNode, PackedScene> LoadDSLNodes()
{
Dictionary<ProgramNode, PackedScene> nodes = new()
{
{ new MoveNode(), GD.Load<PackedScene>($"res://Prefabs/DSL/MoveNode.tscn") },
{
{ new MoveNode(), GD.Load<PackedScene>($"res://Prefabs/DSL/MoveNode.tscn") },
{ new HarvestNode(), GD.Load<PackedScene>($"res://Prefabs/DSL/HarvestNode.tscn") },
{ new CraftNode(), GD.Load<PackedScene>($"res://Prefabs/DSL/CraftNode.tscn") },
{ new ExploreNode(), GD.Load<PackedScene>($"res://Prefabs/DSL/ExploreNode.tscn") }
};
};
return nodes;
}
public static Dictionary<string, Texture2D> LoadResourceSymbols()
{
Dictionary<string, Texture2D> symbols = new()
{
{ "Iron ore", GD.Load<Texture2D>($"res://Assets/Images/Resources/IronSymbol.png") },
{ "Tin ore", GD.Load<Texture2D>($"res://Assets/Images/Resources/TinSymbol.png") },
{ "Copper ore", GD.Load<Texture2D>($"res://Assets/Images/Resources/CopperSymbol.png") },
{ "Mushroom", GD.Load<Texture2D>($"res://Assets/Images/Resources/MushroomSymbol.png") },
{ "Spiderweb", GD.Load<Texture2D>($"res://Assets/Images/Resources/SpiderSilkSymbol.png") },
{ "Coal", GD.Load<Texture2D>($"res://Assets/Images/Resources/CoalSymbol.png") },
{ "Water", GD.Load<Texture2D>($"res://Assets/Images/Resources/WaterSymbol.png") },
{ "Stone", GD.Load<Texture2D>($"res://Assets/Images/Resources/StoneSymbol.png") },
};
{
{ "iron_ore", GD.Load<Texture2D>($"res://Assets/Images/Resources/IronSymbol.png") },
{ "tin_ore", GD.Load<Texture2D>($"res://Assets/Images/Resources/TinSymbol.png") },
{ "copper_ore", GD.Load<Texture2D>($"res://Assets/Images/Resources/CopperSymbol.png") },
{ "mushroom", GD.Load<Texture2D>($"res://Assets/Images/Resources/MushroomSymbol.png") },
{ "spider_silk", GD.Load<Texture2D>($"res://Assets/Images/Resources/SpiderSilkSymbol.png") },
{ "coal", GD.Load<Texture2D>($"res://Assets/Images/Resources/CoalSymbol.png") },
{ "water", GD.Load<Texture2D>($"res://Assets/Images/Resources/WaterSymbol.png") },
{ "stone", GD.Load<Texture2D>($"res://Assets/Images/Resources/StoneSymbol.png") },
};
return symbols;
}
public static Dictionary<Ingredient, Texture2D> LoadIngredients()
public static Dictionary<string, ItemData> LoadItems()
{
Dictionary<Ingredient, Texture2D> ingredients = new()
{
};
return ingredients;
FileAccess file = FileAccess.Open("res://Assets/Recipes.json", FileAccess.ModeFlags.Read);
string json = file.GetAsText();
Dictionary<string, ItemData> result = new();
List<ItemData> items = JsonSerializer.Deserialize<List<ItemData>>(json);
foreach (ItemData item in items)
{
result.Add(item.Id, item);
}
return result;
}
}