Added research display to the game
This commit is contained in:
@@ -12,10 +12,8 @@ public class Item
|
||||
public CraftingResult Craft(int amount, double delta)
|
||||
{
|
||||
elapsedCraftTime += delta;
|
||||
GD.Print(elapsedCraftTime);
|
||||
if (elapsedCraftTime >= data.CraftTime)
|
||||
{
|
||||
GD.Print("Crafted!");
|
||||
elapsedCraftTime -= data.CraftTime;
|
||||
if(!GameData.inventory.AddItem(this, 1))
|
||||
{
|
||||
@@ -28,7 +26,6 @@ public class Item
|
||||
amountCrafted++;
|
||||
if (amountCrafted >= amount)
|
||||
{
|
||||
GD.Print("Finished!");
|
||||
amountCrafted = 0;
|
||||
elapsedCraftTime = 0;
|
||||
return CraftingResult.FINISHED;
|
||||
|
||||
@@ -19,6 +19,7 @@ public partial class GameData
|
||||
public static float tileWidth = 6;
|
||||
public static float tileHeight = 4;
|
||||
public static SortedDictionary<string, ItemData> availableItems = ResourceLoader.LoadItems();
|
||||
public static Dictionary<string, ResearchData> availableResearch = ResourceLoader.LoadResearch();
|
||||
|
||||
//--- PLAYER ADJUSTABLE VALUES ---
|
||||
//Color used in primary objects (e.g. Robots)
|
||||
|
||||
@@ -101,4 +101,21 @@ public partial class ResourceLoader
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Dictionary<string, ResearchData> LoadResearch()
|
||||
{
|
||||
|
||||
FileAccess file = FileAccess.Open("res://Assets/Research.json", FileAccess.ModeFlags.Read);
|
||||
string json = file.GetAsText();
|
||||
|
||||
Dictionary<string, ResearchData> result = new();
|
||||
|
||||
List<ResearchData> researches = JsonSerializer.Deserialize<List<ResearchData>>(json);
|
||||
foreach (ResearchData research in researches)
|
||||
{
|
||||
result.Add(research.Id, research);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
using Godot;
|
||||
|
||||
public class Research
|
||||
{
|
||||
public ResearchData data;
|
||||
public bool isResearched = false;
|
||||
public double elapsedResearchTime = 0;
|
||||
public bool paidResources = false;
|
||||
|
||||
public ResearchResult Execute(double delta)
|
||||
{
|
||||
if (!paidResources)
|
||||
{
|
||||
foreach (Ingredient ingredient in data.Inputs)
|
||||
{
|
||||
GameData.inventory.RemoveItem(ingredient.Item, ingredient.Amount);
|
||||
}
|
||||
paidResources = true;
|
||||
}
|
||||
elapsedResearchTime += delta;
|
||||
if (elapsedResearchTime >= data.CraftTime)
|
||||
{
|
||||
isResearched = true;
|
||||
return ResearchResult.FINISHED;
|
||||
}
|
||||
return ResearchResult.RESEARCHING;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://dj2epbu26v1nv
|
||||
@@ -0,0 +1,32 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json.Serialization;
|
||||
using Godot;
|
||||
|
||||
public class ResearchData
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public string Id { get; set; }
|
||||
|
||||
[JsonPropertyName("inputs")]
|
||||
public List<Ingredient> Inputs { get; set; }
|
||||
|
||||
[JsonPropertyName("research")]
|
||||
public string Research { get; set; }
|
||||
|
||||
[JsonPropertyName("crafttime")]
|
||||
public double CraftTime { get; set; }
|
||||
|
||||
[JsonPropertyName("texture")]
|
||||
public string Texture { 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(" ", "_");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://duwl6bhiry6uh
|
||||
@@ -0,0 +1 @@
|
||||
uid://pxegmc5nenad
|
||||
@@ -0,0 +1,57 @@
|
||||
using Godot;
|
||||
|
||||
public partial class ResearchList : PanelContainer
|
||||
{
|
||||
[Export] GraphEdit researchGraph;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
foreach (ResearchData research in GameData.availableResearch.Values)
|
||||
{
|
||||
researchGraph.AddChild(CreateItemNode(research.Id, research.Texture));
|
||||
}
|
||||
foreach (ResearchData research in GameData.availableResearch.Values)
|
||||
{
|
||||
researchGraph.ConnectNode(
|
||||
research.Research,
|
||||
0,
|
||||
research.Id,
|
||||
0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private GraphNode CreateItemNode(string id, string texturePath)
|
||||
{
|
||||
TextureRect icon = new TextureRect
|
||||
{
|
||||
Texture = GD.Load<Texture2D>(texturePath),
|
||||
StretchMode = TextureRect.StretchModeEnum.KeepAspectCentered
|
||||
};
|
||||
GraphNode node = new GraphNode
|
||||
{
|
||||
Name = id,
|
||||
Title = id,
|
||||
PositionOffset = new Vector2(GameData.availableResearch.Count * -icon.Texture.GetWidth() + researchGraph.GetChildCount() * icon.Texture.GetWidth() * 10, 0)
|
||||
};
|
||||
|
||||
node.SetSlot(
|
||||
0,
|
||||
true,
|
||||
0,
|
||||
Colors.White,
|
||||
true,
|
||||
0,
|
||||
Colors.White
|
||||
);
|
||||
|
||||
node.AddChild(icon);
|
||||
|
||||
return node;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://drscsrkfphpy7
|
||||
@@ -0,0 +1,6 @@
|
||||
public enum ResearchResult
|
||||
{
|
||||
FAILED,
|
||||
RESEARCHING,
|
||||
FINISHED
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://cuav7j7m1td2h
|
||||
Reference in New Issue
Block a user