✨ Building duartion
This commit is contained in:
@@ -5,7 +5,14 @@ namespace AceFieldNewHorizon.Scripts.System;
|
||||
|
||||
public partial class BuildingRegistry : Node
|
||||
{
|
||||
private Dictionary<string, PackedScene> _registry = new();
|
||||
public record BuildingData(
|
||||
PackedScene Scene,
|
||||
Dictionary<string, int> Cost,
|
||||
int Durability,
|
||||
float BuildTime
|
||||
);
|
||||
|
||||
private Dictionary<string, BuildingData> _registry = new();
|
||||
|
||||
[Export] public string JsonPath { get; set; } = "res://Data/Buildings.json";
|
||||
|
||||
@@ -38,26 +45,87 @@ public partial class BuildingRegistry : Node
|
||||
|
||||
foreach (string key in dict.Keys)
|
||||
{
|
||||
var scenePath = dict[key].AsString();
|
||||
var scene = GD.Load<PackedScene>(scenePath);
|
||||
// Each entry is a Dictionary with keys: "scene", "cost", "durability", "buildTime"
|
||||
var buildingDict = dict[key].AsGodotDictionary();
|
||||
|
||||
if (scene != null)
|
||||
// Parse scene
|
||||
var scenePath = buildingDict.ContainsKey("scene") ? buildingDict["scene"].AsString() : null;
|
||||
if (string.IsNullOrEmpty(scenePath))
|
||||
{
|
||||
_registry[key] = scene;
|
||||
GD.Print($"[BuildingRegistry] Loaded building '{key}' from {scenePath}");
|
||||
GD.PrintErr($"[BuildingRegistry] No scene path for '{key}'");
|
||||
continue;
|
||||
}
|
||||
else
|
||||
|
||||
var scene = GD.Load<PackedScene>(scenePath);
|
||||
if (scene == null)
|
||||
{
|
||||
GD.PrintErr($"[BuildingRegistry] Failed to load scene for '{key}' at {scenePath}");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Parse cost
|
||||
Dictionary<string, int> cost = new();
|
||||
if (buildingDict.TryGetValue("cost", out var value))
|
||||
{
|
||||
var costDict = value.AsGodotDictionary();
|
||||
foreach (string mat in costDict.Keys)
|
||||
{
|
||||
int val;
|
||||
var obj = costDict[mat];
|
||||
if (obj.VariantType == Variant.Type.PackedInt64Array)
|
||||
val = (int)obj.AsInt64();
|
||||
else if (obj.VariantType == Variant.Type.PackedInt32Array)
|
||||
val = obj.AsInt32();
|
||||
else
|
||||
int.TryParse(obj.ToString(), out val);
|
||||
cost[mat] = val;
|
||||
}
|
||||
}
|
||||
|
||||
// Parse durability
|
||||
var durability = 0;
|
||||
if (buildingDict.TryGetValue("durability", out var dObj))
|
||||
{
|
||||
if (dObj.VariantType == Variant.Type.PackedInt64Array)
|
||||
durability = (int)dObj.AsInt64();
|
||||
else if (dObj.VariantType == Variant.Type.PackedInt32Array)
|
||||
durability = dObj.AsInt32();
|
||||
else
|
||||
int.TryParse(dObj.ToString(), out durability);
|
||||
}
|
||||
|
||||
// Parse buildTime
|
||||
var buildTime = 0f;
|
||||
if (buildingDict.TryGetValue("buildTime", out var bObj))
|
||||
{
|
||||
switch (bObj.VariantType)
|
||||
{
|
||||
case Variant.Type.PackedFloat32Array or Variant.Type.PackedFloat64Array:
|
||||
buildTime = (float)bObj.AsDouble();
|
||||
break;
|
||||
case Variant.Type.PackedInt64Array:
|
||||
buildTime = bObj.AsInt64();
|
||||
break;
|
||||
case Variant.Type.PackedInt32Array:
|
||||
buildTime = bObj.AsInt32();
|
||||
break;
|
||||
default:
|
||||
float.TryParse(bObj.ToString(), out buildTime);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var buildingData = new BuildingData(scene, cost, durability, buildTime);
|
||||
_registry[key] = buildingData;
|
||||
GD.Print($"[BuildingRegistry] Loaded building '{key}' from {scenePath}");
|
||||
}
|
||||
|
||||
|
||||
GD.Print($"[BuildingRegistry] Loaded {_registry.Count} buildings");
|
||||
}
|
||||
|
||||
public PackedScene GetScene(string id)
|
||||
public BuildingData GetBuilding(string id)
|
||||
{
|
||||
_registry.TryGetValue(id, out var scene);
|
||||
return scene;
|
||||
_registry.TryGetValue(id, out var data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user