64 lines
1.4 KiB
C#
64 lines
1.4 KiB
C#
using System.Collections.Generic;
|
|
using Godot;
|
|
|
|
namespace AceFieldNewHorizon.Scripts.System;
|
|
|
|
public partial class BuildingRegistry : Node
|
|
{
|
|
private Dictionary<string, PackedScene> _registry = new();
|
|
|
|
[Export] public string JsonPath { get; set; } = "res://Data/Buildings.json";
|
|
|
|
public override void _Ready()
|
|
{
|
|
LoadFromJson(JsonPath);
|
|
}
|
|
|
|
public void LoadFromJson(string path)
|
|
{
|
|
var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
|
|
if (file == null)
|
|
{
|
|
GD.PrintErr($"[BuildingRegistry] Failed to open {path}");
|
|
return;
|
|
}
|
|
|
|
var text = file.GetAsText();
|
|
file.Close();
|
|
|
|
var json = new Json();
|
|
var error = json.Parse(text);
|
|
if (error != Error.Ok)
|
|
{
|
|
GD.PrintErr($"[BuildingRegistry] Failed to parse JSON: {json.GetErrorMessage()}");
|
|
return;
|
|
}
|
|
|
|
var dict = (Godot.Collections.Dictionary)json.Data;
|
|
|
|
foreach (string key in dict.Keys)
|
|
{
|
|
var scenePath = dict[key].AsString();
|
|
var scene = GD.Load<PackedScene>(scenePath);
|
|
|
|
if (scene != null)
|
|
{
|
|
_registry[key] = scene;
|
|
GD.Print($"[BuildingRegistry] Loaded building '{key}' from {scenePath}");
|
|
}
|
|
else
|
|
{
|
|
GD.PrintErr($"[BuildingRegistry] Failed to load scene for '{key}' at {scenePath}");
|
|
}
|
|
}
|
|
|
|
GD.Print($"[BuildingRegistry] Loaded {_registry.Count} buildings");
|
|
}
|
|
|
|
public PackedScene GetScene(string id)
|
|
{
|
|
_registry.TryGetValue(id, out var scene);
|
|
return scene;
|
|
}
|
|
}
|