Added testing and save/load mechanic to the game. Game is now entering final phase.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user