diff --git a/Scripts/System/GridManager.cs b/Scripts/System/GridManager.cs index 41f7e50..bf17121 100644 --- a/Scripts/System/GridManager.cs +++ b/Scripts/System/GridManager.cs @@ -1,3 +1,4 @@ +#nullable enable using System.Collections.Generic; using Godot; @@ -21,6 +22,11 @@ public partial class GridManager : Node { _grid.Remove(cell); } + + public Node2D? GetBuildingAtCell(Vector2I cell) + { + return _grid.GetValueOrDefault(cell); + } } public static class GridUtils diff --git a/Scripts/System/PlacementManager.cs b/Scripts/System/PlacementManager.cs index b3275d4..051ad38 100644 --- a/Scripts/System/PlacementManager.cs +++ b/Scripts/System/PlacementManager.cs @@ -44,13 +44,23 @@ public partial class PlacementManager : Node2D _ghostBuilding.SetGhostMode(canPlace); // Left click to place - if (!Input.IsActionPressed("build_tile") || !canPlace) return; + if (Input.IsActionPressed("build_tile") && canPlace) + { + _ghostBuilding.FinalizePlacement(); + Grid.OccupyCell(_hoveredCell, _ghostBuilding); - _ghostBuilding.FinalizePlacement(); - Grid.OccupyCell(_hoveredCell, _ghostBuilding); + _ghostBuilding = (BaseTile)BuildingScene.Instantiate(); + _ghostBuilding.SetGhostMode(true); + AddChild(_ghostBuilding); + } - _ghostBuilding = (BaseTile)BuildingScene.Instantiate(); - _ghostBuilding.SetGhostMode(true); - AddChild(_ghostBuilding); + if (Input.IsActionPressed("destroy_tile") && !Grid.IsCellFree(_hoveredCell)) + { + // Right click to destroy + var building = Grid.GetBuildingAtCell(_hoveredCell); + if (building == null) return; + building.QueueFree(); + Grid.FreeCell(_hoveredCell); + } } } \ No newline at end of file diff --git a/project.godot b/project.godot index f7e292b..fa875a5 100644 --- a/project.godot +++ b/project.godot @@ -50,6 +50,11 @@ build_tile={ "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(680, 19),"global_position":Vector2(699, 115),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null) ] } +destroy_tile={ +"deadzone": 0.2, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(481, 42),"global_position":Vector2(500, 138),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null) +] +} [rendering]