✨ Tiles able to deconstruct
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
| @@ -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); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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] | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user