From 885d2c0075186016a0e74121acfd106872574238 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 29 Aug 2025 13:46:18 +0800 Subject: [PATCH] :sparkles: Toggle build :bug: Fix world gen tiles didn't align with placed ones --- Scripts/System/NaturalResourceGenerator.cs | 8 +++++--- Scripts/System/PlacementManager.cs | 21 +++++++++++++++++++++ project.godot | 5 +++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Scripts/System/NaturalResourceGenerator.cs b/Scripts/System/NaturalResourceGenerator.cs index 5fb2d49..54d0e05 100644 --- a/Scripts/System/NaturalResourceGenerator.cs +++ b/Scripts/System/NaturalResourceGenerator.cs @@ -376,14 +376,16 @@ public partial class NaturalResourceGenerator : Node2D return false; } - var instance = scene.Instantiate() as Node2D; - if (instance == null) + if (scene.Instantiate() is not Node2D instance) { GD.PrintErr($"{LogPrefix} Failed to instantiate scene for: {tileType}"); return false; } - instance.GlobalPosition = GridUtils.GridToWorld(cell); + // Use the same positioning logic as PlacementManager + var rotatedSize = building.GetRotatedSize(0f); // 0f for no rotation + var offset = GridUtils.GetCenterOffset(rotatedSize, 0f); // 0f for no rotation + instance.GlobalPosition = GridUtils.GridToWorld(cell) + offset; instance.ZIndex = (int)building.Layer; AddChild(instance); Grid.OccupyArea(cell, instance, building.Size, 0f, building.Layer); diff --git a/Scripts/System/PlacementManager.cs b/Scripts/System/PlacementManager.cs index d96c459..7a1a645 100644 --- a/Scripts/System/PlacementManager.cs +++ b/Scripts/System/PlacementManager.cs @@ -11,11 +11,15 @@ public partial class PlacementManager : Node2D [Export] public GridManager Grid { get; set; } [Export] public ResourceManager Inventory { get; set; } [Export] public BuildingRegistry Registry { get; set; } + [Export] public int MaxConcurrentBuilds { get; set; } = 6; // Make it adjustable in editor + [Export] public bool Enabled { get; set; } = true; + [Export] public StringName ToggleBuildAction { get; set; } = "toggle_build"; private static readonly List BuildableTiles = ["wall", "miner"]; private readonly Dictionary _buildTasks = new(); private AudioStreamPlayer _completionSound; + private Node2D _currentGhost; // Keep track of the current ghost building public override void _Ready() { @@ -153,6 +157,8 @@ public partial class PlacementManager : Node2D public override void _Process(double delta) { + if (!Enabled) return; + // Snap mouse to grid var mousePos = GetGlobalMousePosition(); var newHoveredCell = GridUtils.WorldToGrid(mousePos); @@ -267,6 +273,21 @@ public partial class PlacementManager : Node2D } } + public override void _Input(InputEvent @event) + { + if (@event.IsActionPressed(ToggleBuildAction)) + { + Enabled = !Enabled; + + // Hide ghost building when disabling + if (!Enabled && _ghostBuilding != null && _ghostBuilding.IsInsideTree()) + { + _ghostBuilding.QueueFree(); + _ghostBuilding = null; + } + } + } + public override void _ExitTree() { base._ExitTree(); diff --git a/project.godot b/project.godot index 90957f0..38d968e 100644 --- a/project.godot +++ b/project.godot @@ -75,6 +75,11 @@ switch_tile={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":93,"key_label":0,"unicode":93,"location":0,"echo":false,"script":null) ] } +toggle_build={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":66,"key_label":0,"unicode":98,"location":0,"echo":false,"script":null) +] +} [rendering]