Added complete crafting to the game. Next step would be research and buildings. Updated DSL with Setup to work with dynamic loading.
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using Godot;
|
||||
|
||||
public class Ingredient
|
||||
{
|
||||
public string item;
|
||||
public int amount;
|
||||
[JsonPropertyName("item")]
|
||||
public string Item {get; set;}
|
||||
[JsonPropertyName("amount")]
|
||||
public int Amount {get;set;}
|
||||
}
|
||||
@@ -32,9 +32,9 @@ public class Inventory
|
||||
{
|
||||
bool canCraft = true;
|
||||
Item item;
|
||||
foreach(Ingredient ingredient in neededIngredients)
|
||||
foreach (Ingredient ingredient in neededIngredients)
|
||||
{
|
||||
item = items.Find(x => x.data.Id == ingredient.item && x.currentAmount >= ingredient.amount * amount);
|
||||
item = items.Find(x => x.data.Id == ingredient.Item && x.currentAmount >= ingredient.Amount * amount);
|
||||
if (item == null)
|
||||
{
|
||||
canCraft = false;
|
||||
@@ -43,4 +43,13 @@ public class Inventory
|
||||
}
|
||||
return canCraft;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ public partial class InventoryDisplay : PanelContainer
|
||||
{
|
||||
display = itemDisplayPrefab.Instantiate<ItemDisplay>();
|
||||
display.item = item;
|
||||
display.text.Text = item.GetReadableName();
|
||||
display.text.Text = item.data.GetReadableName();
|
||||
display.amount.Text = item.currentAmount.ToString();
|
||||
display.texture.Texture = ResourceLoader.LoadPath(item.data.Texture);
|
||||
itemList.AddChild(display);
|
||||
|
||||
+20
-20
@@ -11,30 +11,30 @@ public class Item
|
||||
|
||||
public CraftingResult Craft(int amount, double delta)
|
||||
{
|
||||
if (GameData.inventory.CanCraft(data.Inputs, amount - amountCrafted))
|
||||
elapsedCraftTime += delta;
|
||||
GD.Print(elapsedCraftTime);
|
||||
if (elapsedCraftTime >= data.CraftTime)
|
||||
{
|
||||
elapsedCraftTime += delta;
|
||||
if (elapsedCraftTime >= data.CraftTime)
|
||||
GD.Print("Crafted!");
|
||||
elapsedCraftTime -= data.CraftTime;
|
||||
if(!GameData.inventory.AddItem(this, 1))
|
||||
{
|
||||
elapsedCraftTime -= data.CraftTime;
|
||||
currentAmount += 1;
|
||||
amountCrafted++;
|
||||
if (amountCrafted >= amount)
|
||||
{
|
||||
amountCrafted = 0;
|
||||
elapsedCraftTime = 0;
|
||||
return CraftingResult.FINISHED;
|
||||
}
|
||||
return CraftingResult.FAILED;
|
||||
}
|
||||
foreach (Ingredient ingredient in data.Inputs)
|
||||
{
|
||||
GameData.inventory.RemoveItem(ingredient.Item, ingredient.Amount);
|
||||
}
|
||||
amountCrafted++;
|
||||
if (amountCrafted >= amount)
|
||||
{
|
||||
GD.Print("Finished!");
|
||||
amountCrafted = 0;
|
||||
elapsedCraftTime = 0;
|
||||
return CraftingResult.FINISHED;
|
||||
}
|
||||
return CraftingResult.CRAFTING;
|
||||
}
|
||||
return CraftingResult.FAILED;
|
||||
}
|
||||
|
||||
public string GetReadableName()
|
||||
{
|
||||
string noUnderscore = data.Id.Replace("_", " ").ToLower();
|
||||
return char.ToUpper(noUnderscore[0]) + noUnderscore.Substring(1);
|
||||
return CraftingResult.CRAFTING;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,4 +27,15 @@ public class ItemData
|
||||
|
||||
[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 GetIndex(string readable)
|
||||
{
|
||||
return readable.ToLower().Replace(" ", "_");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user