Added testing and save/load mechanic to the game. Game is now entering final phase.

This commit is contained in:
2026-05-09 21:25:36 +02:00
parent e7de2433de
commit 7e70471227
18 changed files with 1073 additions and 46 deletions
+24
View File
@@ -19,6 +19,30 @@ public class GameResource
item = GameData.availableItems[name];
}
public static GameResource FromSaveData(ResourceSaveData saveData)
{
GameResource resource = new GameResource(saveData.Name);
resource.currentAmount = saveData.CurrentAmount;
resource.maxAmount = saveData.MaxAmount;
resource.isEndless = saveData.IsEndless;
resource.extractionSpeed = saveData.ExtractionSpeed;
resource.timeSinceLastExtraction = saveData.TimeSinceLastExtraction;
return resource;
}
public ResourceSaveData CreateSaveData()
{
return new ResourceSaveData
{
Name = name,
CurrentAmount = currentAmount,
MaxAmount = maxAmount,
IsEndless = isEndless,
ExtractionSpeed = extractionSpeed,
TimeSinceLastExtraction = timeSinceLastExtraction
};
}
public bool Extract(double delta)
{
timeSinceLastExtraction += delta;
+48 -13
View File
@@ -10,31 +10,49 @@ public class Inventory
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)
if (GetFreeCapacity(item.data.Id, item.data.StackSize) < amount)
{
inventoryItem.currentAmount += amount;
NotifyInventoryChanged();
return true;
return false;
}
if (items.Count < maxInventorySize * GameData.maxRobotCount)
int remainingAmount = amount;
foreach (Item inventoryItem in items)
{
items.Add(item);
items[items.Count - 1].currentAmount += amount;
NotifyInventoryChanged();
return true;
if (inventoryItem.data.Id != item.data.Id) continue;
if (inventoryItem.currentAmount >= inventoryItem.data.StackSize) continue;
int amountToAdd = Math.Min(
remainingAmount,
inventoryItem.data.StackSize - inventoryItem.currentAmount
);
inventoryItem.currentAmount += amountToAdd;
remainingAmount -= amountToAdd;
if (remainingAmount <= 0)
{
NotifyInventoryChanged();
return true;
}
}
return false;
while (remainingAmount > 0 && items.Count < maxInventorySize * GameData.maxRobotCount)
{
int amountToAdd = Math.Min(remainingAmount, item.data.StackSize);
items.Add(new Item { data = item.data, currentAmount = amountToAdd });
remainingAmount -= amountToAdd;
}
NotifyInventoryChanged();
return remainingAmount <= 0;
}
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)
if (GetItemAmount(ingredient.Item) < ingredient.Amount * amount)
{
return false;
}
@@ -93,4 +111,21 @@ public class Inventory
{
OnInventoryUpdate?.Invoke(this, EventArgs.Empty);
}
private int GetFreeCapacity(string id, int stackSize)
{
int freeCapacity = 0;
foreach (Item item in items)
{
if (item.data.Id == id)
{
freeCapacity += stackSize - item.currentAmount;
}
}
int freeSlots = maxInventorySize * GameData.maxRobotCount - items.Count;
freeCapacity += freeSlots * stackSize;
return freeCapacity;
}
}
+32 -1
View File
@@ -155,6 +155,37 @@ public partial class Robot : Node3D
currentMessage = "";
}
public RobotSaveData CreateSaveData()
{
return new RobotSaveData
{
Name = Name,
CurrentProgram = currentProgram,
CurrentMessage = currentMessage,
RobotType = robotType,
X = Position.X,
Y = Position.Y,
Z = Position.Z,
Heat = heat,
Maintenance = maintenance,
IsCoolingDown = isCoolingDown,
IsBroken = isBroken
};
}
public void LoadSaveData(RobotSaveData saveData)
{
Name = saveData.Name;
currentProgram = saveData.CurrentProgram;
currentMessage = saveData.CurrentMessage ?? "";
robotType = saveData.RobotType ?? "stone_robot";
Position = new Vector3(saveData.X, saveData.Y, saveData.Z);
heat = saveData.Heat;
maintenance = saveData.Maintenance;
isCoolingDown = saveData.IsCoolingDown;
isBroken = saveData.IsBroken;
}
private bool CanExecute(double delta)
{
if (GameData.survival.isDead)
@@ -238,4 +269,4 @@ public partial class Robot : Node3D
isCoolingDown = false;
}
}
}
}
+7 -7
View File
@@ -3,13 +3,13 @@ using System.Collections.Generic;
public class RobotStats
{
public readonly Dictionary<string, RobotTypeStats> RobotTypes = new()
public readonly Dictionary<string, RobotTypeStats> RobotTypes = new Dictionary<string, RobotTypeStats>
{
["stone_robot"] = new RobotTypeStats(0.75f, 0.60f, 0.80f, 0.80f, 1.40f, -0.10f),
["copper_robot"] = new RobotTypeStats(1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 0.00f),
["tin_robot"] = new RobotTypeStats(1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 0.00f),
["bronze_robot"] = new RobotTypeStats(1.15f, 1.10f, 0.90f, 1.10f, 0.80f, 0.05f),
["iron_robot"] = new RobotTypeStats(1.35f, 1.25f, 1.15f, 1.20f, 0.65f, 0.10f)
{ "stone_robot", new RobotTypeStats(0.75f, 0.60f, 0.80f, 0.80f, 1.40f, -0.10f) },
{ "copper_robot", new RobotTypeStats(1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 0.00f) },
{ "tin_robot", new RobotTypeStats(1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 0.00f) },
{ "bronze_robot", new RobotTypeStats(1.15f, 1.10f, 0.90f, 1.10f, 0.80f, 0.05f) },
{ "iron_robot", new RobotTypeStats(1.35f, 1.25f, 1.15f, 1.20f, 0.65f, 0.10f) }
};
private const float BaseMinimumEfficiency = 0.35f;
@@ -86,7 +86,7 @@ public class RobotStats
break;
case "robot_count_increase":
GameData.maxRobotCount += (int)effect.Value;
break;
break;
}
}
}