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
+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;
}
}