✨ Building regisery instead of single building
This commit is contained in:
63
Scripts/System/BuildingRegistry.cs
Normal file
63
Scripts/System/BuildingRegistry.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
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;
|
||||
}
|
||||
}
|
1
Scripts/System/BuildingRegistry.cs.uid
Normal file
1
Scripts/System/BuildingRegistry.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cfbj72nm0eovg
|
@@ -5,11 +5,23 @@ namespace AceFieldNewHorizon.Scripts.System;
|
||||
|
||||
public partial class PlacementManager : Node2D
|
||||
{
|
||||
[Export] public PackedScene BuildingScene { get; set; }
|
||||
[Export] public GridManager Grid { get; set; }
|
||||
[Export] public BuildingRegistry Registry { get; set; }
|
||||
|
||||
private string _currentBuildingId = "wall";
|
||||
|
||||
private Vector2I _hoveredCell;
|
||||
private BaseTile _ghostBuilding;
|
||||
|
||||
public void SetCurrentBuilding(string buildingId)
|
||||
{
|
||||
_currentBuildingId = buildingId;
|
||||
|
||||
// Replace ghost immediately
|
||||
if (_ghostBuilding == null) return;
|
||||
_ghostBuilding.QueueFree();
|
||||
_ghostBuilding = null;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
@@ -19,7 +31,10 @@ public partial class PlacementManager : Node2D
|
||||
|
||||
if (_ghostBuilding == null)
|
||||
{
|
||||
_ghostBuilding = (BaseTile)BuildingScene.Instantiate();
|
||||
var scene = Registry.GetScene(_currentBuildingId);
|
||||
if (scene == null) return;
|
||||
|
||||
_ghostBuilding = (BaseTile)scene.Instantiate();
|
||||
_ghostBuilding.SetGhostMode(true);
|
||||
AddChild(_ghostBuilding);
|
||||
}
|
||||
@@ -46,10 +61,13 @@ public partial class PlacementManager : Node2D
|
||||
// Left click to place
|
||||
if (Input.IsActionPressed("build_tile") && canPlace)
|
||||
{
|
||||
var scene = Registry.GetScene(_currentBuildingId);
|
||||
if (scene == null) return;
|
||||
|
||||
_ghostBuilding.FinalizePlacement();
|
||||
Grid.OccupyCell(_hoveredCell, _ghostBuilding);
|
||||
|
||||
_ghostBuilding = (BaseTile)BuildingScene.Instantiate();
|
||||
_ghostBuilding = (BaseTile)scene.Instantiate();
|
||||
_ghostBuilding.SetGhostMode(true);
|
||||
AddChild(_ghostBuilding);
|
||||
}
|
||||
|
Reference in New Issue
Block a user