Finished first EA Version #1
@@ -1,7 +1,10 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
using Godot;
|
using Godot;
|
||||||
|
|
||||||
public class Ingredient
|
public class Ingredient
|
||||||
{
|
{
|
||||||
public string item;
|
[JsonPropertyName("item")]
|
||||||
public int amount;
|
public string Item {get; set;}
|
||||||
|
[JsonPropertyName("amount")]
|
||||||
|
public int Amount {get;set;}
|
||||||
}
|
}
|
||||||
@@ -32,9 +32,9 @@ public class Inventory
|
|||||||
{
|
{
|
||||||
bool canCraft = true;
|
bool canCraft = true;
|
||||||
Item item;
|
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)
|
if (item == null)
|
||||||
{
|
{
|
||||||
canCraft = false;
|
canCraft = false;
|
||||||
@@ -43,4 +43,13 @@ public class Inventory
|
|||||||
}
|
}
|
||||||
return canCraft;
|
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 = itemDisplayPrefab.Instantiate<ItemDisplay>();
|
||||||
display.item = item;
|
display.item = item;
|
||||||
display.text.Text = item.GetReadableName();
|
display.text.Text = item.data.GetReadableName();
|
||||||
display.amount.Text = item.currentAmount.ToString();
|
display.amount.Text = item.currentAmount.ToString();
|
||||||
display.texture.Texture = ResourceLoader.LoadPath(item.data.Texture);
|
display.texture.Texture = ResourceLoader.LoadPath(item.data.Texture);
|
||||||
itemList.AddChild(display);
|
itemList.AddChild(display);
|
||||||
|
|||||||
+11
-11
@@ -10,17 +10,25 @@ public class Item
|
|||||||
public int amountCrafted = 0;
|
public int amountCrafted = 0;
|
||||||
|
|
||||||
public CraftingResult Craft(int amount, double delta)
|
public CraftingResult Craft(int amount, double delta)
|
||||||
{
|
|
||||||
if (GameData.inventory.CanCraft(data.Inputs, amount - amountCrafted))
|
|
||||||
{
|
{
|
||||||
elapsedCraftTime += delta;
|
elapsedCraftTime += delta;
|
||||||
|
GD.Print(elapsedCraftTime);
|
||||||
if (elapsedCraftTime >= data.CraftTime)
|
if (elapsedCraftTime >= data.CraftTime)
|
||||||
{
|
{
|
||||||
|
GD.Print("Crafted!");
|
||||||
elapsedCraftTime -= data.CraftTime;
|
elapsedCraftTime -= data.CraftTime;
|
||||||
currentAmount += 1;
|
if(!GameData.inventory.AddItem(this, 1))
|
||||||
|
{
|
||||||
|
return CraftingResult.FAILED;
|
||||||
|
}
|
||||||
|
foreach (Ingredient ingredient in data.Inputs)
|
||||||
|
{
|
||||||
|
GameData.inventory.RemoveItem(ingredient.Item, ingredient.Amount);
|
||||||
|
}
|
||||||
amountCrafted++;
|
amountCrafted++;
|
||||||
if (amountCrafted >= amount)
|
if (amountCrafted >= amount)
|
||||||
{
|
{
|
||||||
|
GD.Print("Finished!");
|
||||||
amountCrafted = 0;
|
amountCrafted = 0;
|
||||||
elapsedCraftTime = 0;
|
elapsedCraftTime = 0;
|
||||||
return CraftingResult.FINISHED;
|
return CraftingResult.FINISHED;
|
||||||
@@ -28,13 +36,5 @@ public class Item
|
|||||||
}
|
}
|
||||||
return CraftingResult.CRAFTING;
|
return CraftingResult.CRAFTING;
|
||||||
}
|
}
|
||||||
return CraftingResult.FAILED;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string GetReadableName()
|
|
||||||
{
|
|
||||||
string noUnderscore = data.Id.Replace("_", " ").ToLower();
|
|
||||||
return char.ToUpper(noUnderscore[0]) + noUnderscore.Substring(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,4 +27,15 @@ public class ItemData
|
|||||||
|
|
||||||
[JsonPropertyName("stacksize")]
|
[JsonPropertyName("stacksize")]
|
||||||
public int StackSize { get; set; }
|
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(" ", "_");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -12,14 +12,9 @@ public partial class NodeDisplay : PanelContainer
|
|||||||
this.node = node;
|
this.node = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetupUIElement()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
|
node.Setup(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _Process(double delta)
|
public override void _Process(double delta)
|
||||||
|
|||||||
@@ -2,31 +2,74 @@ using Godot;
|
|||||||
|
|
||||||
public class CraftNode : ProgramNode
|
public class CraftNode : ProgramNode
|
||||||
{
|
{
|
||||||
|
Item selectedItem;
|
||||||
|
int amount;
|
||||||
public CraftNode()
|
public CraftNode()
|
||||||
{
|
{
|
||||||
DisplayText = "Craft";
|
DisplayText = "Craft";
|
||||||
}
|
}
|
||||||
public override NodeResult Execute(Robot robot, double delta)
|
public override NodeResult Execute(Robot robot, double delta)
|
||||||
{
|
{
|
||||||
GD.Print("Craft");
|
if (selectedItem == null)
|
||||||
if (nextNode != null)
|
|
||||||
{
|
{
|
||||||
return nextNode.Execute(robot, delta);
|
lastExecutionMessage = "No Item selected";
|
||||||
|
return NodeResult.FAILURE;
|
||||||
}
|
}
|
||||||
else
|
if (amount <= 0)
|
||||||
{
|
{
|
||||||
|
lastExecutionMessage = "Amount has to be atleast 1";
|
||||||
|
return NodeResult.FAILURE;
|
||||||
|
}
|
||||||
|
if (!GameData.inventory.CanCraft(selectedItem.data.Inputs, amount))
|
||||||
|
{
|
||||||
|
lastExecutionMessage = "Not enough items to craft this";
|
||||||
|
return NodeResult.FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (selectedItem.Craft(amount, delta))
|
||||||
|
{
|
||||||
|
case CraftingResult.FAILED:
|
||||||
|
lastExecutionMessage = "Not enough space to add item to inventory";
|
||||||
|
return NodeResult.FAILURE;
|
||||||
|
case CraftingResult.FINISHED:
|
||||||
return NodeResult.SUCCESS;
|
return NodeResult.SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return NodeResult.RUNNING;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void ReadParameters(NodeDisplay display)
|
public override void ReadParameters(NodeDisplay display)
|
||||||
{
|
{
|
||||||
//
|
HBoxContainer valueContainer = display.GetNode<HBoxContainer>("./EditorDisplay/HBoxContainer/");
|
||||||
|
OptionButton items = valueContainer.GetNode<OptionButton>("./Item");
|
||||||
|
string readableItem = items.GetItemText(items.GetSelectedId());
|
||||||
|
if (GameData.availableItems.ContainsKey(ItemData.GetIndex(readableItem)))
|
||||||
|
{
|
||||||
|
selectedItem = new Item { data = GameData.availableItems[ItemData.GetIndex(readableItem)]};
|
||||||
|
}
|
||||||
|
amount = (int)valueContainer.GetNode<SpinBox>("./Amount").Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override ProgramNode Duplicate()
|
public override ProgramNode Duplicate()
|
||||||
{
|
{
|
||||||
CraftNode duplicate = new CraftNode();
|
CraftNode duplicate = new CraftNode()
|
||||||
|
{
|
||||||
|
selectedItem = selectedItem,
|
||||||
|
amount = amount
|
||||||
|
};
|
||||||
return duplicate;
|
return duplicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void Setup(NodeDisplay display)
|
||||||
|
{
|
||||||
|
OptionButton options = display.GetNode<OptionButton>("./EditorDisplay/HBoxContainer/Item");
|
||||||
|
options.AddItem("Select item...");
|
||||||
|
foreach (ItemData item in GameData.availableItems.Values)
|
||||||
|
{
|
||||||
|
if (item.Inputs.Count > 0)
|
||||||
|
{
|
||||||
|
options.AddItem(item.GetReadableName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -87,4 +87,9 @@ public class ExploreNode : ProgramNode
|
|||||||
{
|
{
|
||||||
//Currently does nothing
|
//Currently does nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void Setup(NodeDisplay display)
|
||||||
|
{
|
||||||
|
//Currently does nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -36,7 +36,7 @@ public class HarvestNode : ProgramNode
|
|||||||
|
|
||||||
public override void ReadParameters(NodeDisplay display)
|
public override void ReadParameters(NodeDisplay display)
|
||||||
{
|
{
|
||||||
//
|
//Currently does nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
public override ProgramNode Duplicate()
|
public override ProgramNode Duplicate()
|
||||||
@@ -44,4 +44,9 @@ public class HarvestNode : ProgramNode
|
|||||||
HarvestNode duplicate = new HarvestNode();
|
HarvestNode duplicate = new HarvestNode();
|
||||||
return duplicate;
|
return duplicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void Setup(NodeDisplay display)
|
||||||
|
{
|
||||||
|
//Currently does nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -72,4 +72,9 @@ public class MoveNode : ProgramNode
|
|||||||
};
|
};
|
||||||
return duplicate;
|
return duplicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void Setup(NodeDisplay display)
|
||||||
|
{
|
||||||
|
//Currently does nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ public abstract class ProgramNode
|
|||||||
public string DisplayText;
|
public string DisplayText;
|
||||||
public string lastExecutionMessage;
|
public string lastExecutionMessage;
|
||||||
|
|
||||||
|
public abstract void Setup(NodeDisplay display);
|
||||||
public abstract NodeResult Execute(Robot robot, double delta);
|
public abstract NodeResult Execute(Robot robot, double delta);
|
||||||
public abstract void ReadParameters(NodeDisplay display);
|
public abstract void ReadParameters(NodeDisplay display);
|
||||||
public abstract ProgramNode Duplicate();
|
public abstract ProgramNode Duplicate();
|
||||||
|
|||||||
Reference in New Issue
Block a user