Cleaned up project with better structure.
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
public class Building
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://cl2yvllo35qbb
|
||||
@@ -0,0 +1,6 @@
|
||||
public enum CraftingResult
|
||||
{
|
||||
FAILED,
|
||||
CRAFTING,
|
||||
FINISHED
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://bd7t0os166lu3
|
||||
@@ -0,0 +1,43 @@
|
||||
public class GameResource
|
||||
{
|
||||
public string name;
|
||||
public ItemData item;
|
||||
|
||||
private int currentAmount;
|
||||
private int maxAmount;
|
||||
private bool isEndless;
|
||||
private float extractionSpeed;
|
||||
private double timeSinceLastExtraction;
|
||||
|
||||
public GameResource(string name)
|
||||
{
|
||||
this.name = name;
|
||||
maxAmount = GameData.rand.Next(1000, 10000);
|
||||
currentAmount = maxAmount;
|
||||
isEndless = false;
|
||||
extractionSpeed = 1f;
|
||||
item = GameData.availableItems[name];
|
||||
}
|
||||
|
||||
public bool Extract(double delta)
|
||||
{
|
||||
timeSinceLastExtraction += delta;
|
||||
if (timeSinceLastExtraction < extractionSpeed) return false;
|
||||
|
||||
timeSinceLastExtraction = 0;
|
||||
if (isEndless) return true;
|
||||
|
||||
if (currentAmount > 0)
|
||||
{
|
||||
currentAmount--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool CanExtract()
|
||||
{
|
||||
return (isEndless || currentAmount > 0) && GameData.availableResearch[item.Research].state == ResearchState.RESEARCHED;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://bh4m8ufiv5kt8
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public class Ingredient
|
||||
{
|
||||
[JsonPropertyName("item")]
|
||||
public string Item { get; set; }
|
||||
|
||||
[JsonPropertyName("amount")]
|
||||
public int Amount { get; set; }
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://dital81qfptdr
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Inventory
|
||||
{
|
||||
public List<Item> items = new List<Item>();
|
||||
|
||||
public int maxInventorySize = 8;
|
||||
public event EventHandler OnInventoryUpdate;
|
||||
|
||||
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)
|
||||
{
|
||||
inventoryItem.currentAmount += amount;
|
||||
NotifyInventoryChanged();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (items.Count < maxInventorySize)
|
||||
{
|
||||
items.Add(item);
|
||||
items[items.Count - 1].currentAmount += amount;
|
||||
NotifyInventoryChanged();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void RemoveItem(string id, int amount)
|
||||
{
|
||||
Item item = items.Find(x => x.data.Id == id && x.currentAmount >= amount);
|
||||
if (item != null)
|
||||
{
|
||||
item.currentAmount -= amount;
|
||||
NotifyInventoryChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private void NotifyInventoryChanged()
|
||||
{
|
||||
OnInventoryUpdate?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://dnbdfvox75hk8
|
||||
@@ -0,0 +1,35 @@
|
||||
public class Item
|
||||
{
|
||||
public ItemData data;
|
||||
public int currentAmount = 0;
|
||||
public double elapsedCraftTime = 0;
|
||||
public int amountCrafted = 0;
|
||||
|
||||
public CraftingResult Craft(int amount, double delta)
|
||||
{
|
||||
elapsedCraftTime += delta;
|
||||
if (elapsedCraftTime >= data.CraftTime)
|
||||
{
|
||||
elapsedCraftTime -= data.CraftTime;
|
||||
if (!GameData.inventory.AddItem(this, 1))
|
||||
{
|
||||
return CraftingResult.FAILED;
|
||||
}
|
||||
|
||||
foreach (Ingredient ingredient in data.Inputs)
|
||||
{
|
||||
GameData.inventory.RemoveItem(ingredient.Item, ingredient.Amount);
|
||||
}
|
||||
|
||||
amountCrafted++;
|
||||
if (amountCrafted >= amount)
|
||||
{
|
||||
amountCrafted = 0;
|
||||
elapsedCraftTime = 0;
|
||||
return CraftingResult.FINISHED;
|
||||
}
|
||||
}
|
||||
|
||||
return CraftingResult.CRAFTING;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://dpq18vk7hbakh
|
||||
@@ -0,0 +1,61 @@
|
||||
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; }
|
||||
|
||||
public string GetReadableName()
|
||||
{
|
||||
string noUnderscore = Id.Replace("_", " ").ToLower();
|
||||
return char.ToUpper(noUnderscore[0]) + noUnderscore.Substring(1);
|
||||
}
|
||||
|
||||
public static string GetReadableName(string input)
|
||||
{
|
||||
string noUnderscore = input.Replace("_", " ").ToLower();
|
||||
return char.ToUpper(noUnderscore[0]) + noUnderscore.Substring(1);
|
||||
}
|
||||
|
||||
public static string GetIndex(string readable)
|
||||
{
|
||||
return readable.ToLower().Replace(" ", "_");
|
||||
}
|
||||
|
||||
public string GetCraftingDisplay()
|
||||
{
|
||||
string result = GetReadableName() + ": \r";
|
||||
|
||||
foreach (Ingredient ingredient in Inputs)
|
||||
{
|
||||
result += $"{GetReadableName(ingredient.Item)} ({ingredient.Amount}),\r";
|
||||
}
|
||||
|
||||
if (Inputs.Count <= 0) return result;
|
||||
|
||||
result = result.Remove(result.Length - 2);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://cj8yubkrsdq70
|
||||
Reference in New Issue
Block a user