From 4f92df9865e414b7c34d36f1304c62ef9edd93ba Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 26 Aug 2025 22:35:23 +0800 Subject: [PATCH] :sparkles: Building regisery instead of single building --- Data/Buildings.json | 5 ++ Scenes/Root.tscn | 11 +++-- Scripts/System/BuildingRegistry.cs | 63 ++++++++++++++++++++++++++ Scripts/System/BuildingRegistry.cs.uid | 1 + Scripts/System/PlacementManager.cs | 24 ++++++++-- 5 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 Data/Buildings.json create mode 100644 Scripts/System/BuildingRegistry.cs create mode 100644 Scripts/System/BuildingRegistry.cs.uid diff --git a/Data/Buildings.json b/Data/Buildings.json new file mode 100644 index 0000000..dda5e12 --- /dev/null +++ b/Data/Buildings.json @@ -0,0 +1,5 @@ +{ + "wall": "res://Scenes/Tiles/WallTile.tscn", + "farm": "res://Scenes/Tiles/FarmTile.tscn", + "tower": "res://Scenes/Tiles/TowerTile.tscn" +} diff --git a/Scenes/Root.tscn b/Scenes/Root.tscn index 6fe2e0d..1a1828a 100644 --- a/Scenes/Root.tscn +++ b/Scenes/Root.tscn @@ -1,21 +1,22 @@ [gd_scene load_steps=5 format=3 uid="uid://c22aprj452aha"] [ext_resource type="Script" uid="uid://cudpc3w17mbsw" path="res://Scripts/System/GridManager.cs" id="1_knkkn"] +[ext_resource type="Script" uid="uid://cfbj72nm0eovg" path="res://Scripts/System/BuildingRegistry.cs" id="1_sxhdm"] [ext_resource type="Script" uid="uid://bx1wj7gn6vrqe" path="res://Scripts/System/PlacementManager.cs" id="2_sxhdm"] [ext_resource type="PackedScene" uid="uid://doxy60afddg1m" path="res://Scenes/Entities/Player.tscn" id="3_oss8w"] -[ext_resource type="PackedScene" uid="uid://d1pudmkg5nnhj" path="res://Scenes/Tiles/WallTile.tscn" id="3_sxhdm"] [node name="Root" type="Node2D"] +[node name="BuildingRegistry" type="Node" parent="."] +script = ExtResource("1_sxhdm") + [node name="GridSystem" type="Node2D" parent="."] script = ExtResource("1_knkkn") -[node name="PlacementSystem" type="Node2D" parent="." node_paths=PackedStringArray("Grid")] +[node name="PlacementSystem" type="Node2D" parent="." node_paths=PackedStringArray("Grid", "Registry")] script = ExtResource("2_sxhdm") -BuildingScene = ExtResource("3_sxhdm") Grid = NodePath("../GridSystem") - -[node name="WallTile" parent="." instance=ExtResource("3_sxhdm")] +Registry = NodePath("../BuildingRegistry") [node name="Player" parent="." instance=ExtResource("3_oss8w")] position = Vector2(602, 324) diff --git a/Scripts/System/BuildingRegistry.cs b/Scripts/System/BuildingRegistry.cs new file mode 100644 index 0000000..2ba7f61 --- /dev/null +++ b/Scripts/System/BuildingRegistry.cs @@ -0,0 +1,63 @@ +using System.Collections.Generic; +using Godot; + +namespace AceFieldNewHorizon.Scripts.System; + +public partial class BuildingRegistry : Node +{ + private Dictionary _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(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; + } +} diff --git a/Scripts/System/BuildingRegistry.cs.uid b/Scripts/System/BuildingRegistry.cs.uid new file mode 100644 index 0000000..025153c --- /dev/null +++ b/Scripts/System/BuildingRegistry.cs.uid @@ -0,0 +1 @@ +uid://cfbj72nm0eovg diff --git a/Scripts/System/PlacementManager.cs b/Scripts/System/PlacementManager.cs index 051ad38..d58b27d 100644 --- a/Scripts/System/PlacementManager.cs +++ b/Scripts/System/PlacementManager.cs @@ -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); }