Toggle build

🐛 Fix world gen tiles didn't align with placed ones
This commit is contained in:
2025-08-29 13:46:18 +08:00
parent 8073ed23c0
commit 885d2c0075
3 changed files with 31 additions and 3 deletions

View File

@@ -376,14 +376,16 @@ public partial class NaturalResourceGenerator : Node2D
return false; return false;
} }
var instance = scene.Instantiate() as Node2D; if (scene.Instantiate() is not Node2D instance)
if (instance == null)
{ {
GD.PrintErr($"{LogPrefix} Failed to instantiate scene for: {tileType}"); GD.PrintErr($"{LogPrefix} Failed to instantiate scene for: {tileType}");
return false; 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; instance.ZIndex = (int)building.Layer;
AddChild(instance); AddChild(instance);
Grid.OccupyArea(cell, instance, building.Size, 0f, building.Layer); Grid.OccupyArea(cell, instance, building.Size, 0f, building.Layer);

View File

@@ -11,11 +11,15 @@ public partial class PlacementManager : Node2D
[Export] public GridManager Grid { get; set; } [Export] public GridManager Grid { get; set; }
[Export] public ResourceManager Inventory { get; set; } [Export] public ResourceManager Inventory { get; set; }
[Export] public BuildingRegistry Registry { get; set; } [Export] public BuildingRegistry Registry { get; set; }
[Export] public int MaxConcurrentBuilds { get; set; } = 6; // Make it adjustable in editor [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<string> BuildableTiles = ["wall", "miner"]; private static readonly List<string> BuildableTiles = ["wall", "miner"];
private readonly Dictionary<Node2D, BuildTask> _buildTasks = new(); private readonly Dictionary<Node2D, BuildTask> _buildTasks = new();
private AudioStreamPlayer _completionSound; private AudioStreamPlayer _completionSound;
private Node2D _currentGhost; // Keep track of the current ghost building
public override void _Ready() public override void _Ready()
{ {
@@ -153,6 +157,8 @@ public partial class PlacementManager : Node2D
public override void _Process(double delta) public override void _Process(double delta)
{ {
if (!Enabled) return;
// Snap mouse to grid // Snap mouse to grid
var mousePos = GetGlobalMousePosition(); var mousePos = GetGlobalMousePosition();
var newHoveredCell = GridUtils.WorldToGrid(mousePos); 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() public override void _ExitTree()
{ {
base._ExitTree(); base._ExitTree();

View File

@@ -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) "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] [rendering]