From 84b908ef51ac4e37486d98e00738ca4b25b45427 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 27 Aug 2025 17:37:31 +0800 Subject: [PATCH] :sparkles: Rotate building tiles --- Scripts/System/PlacementManager.cs | 29 ++++++++++++++++++++++++++--- project.godot | 15 +++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Scripts/System/PlacementManager.cs b/Scripts/System/PlacementManager.cs index a8be86b..7dd4021 100644 --- a/Scripts/System/PlacementManager.cs +++ b/Scripts/System/PlacementManager.cs @@ -12,7 +12,7 @@ public partial class PlacementManager : Node2D private Vector2I _hoveredCell; private BaseTile _ghostBuilding; - + public void SetCurrentBuilding(string buildingId) { _currentBuildingId = buildingId; @@ -23,19 +23,39 @@ public partial class PlacementManager : Node2D _ghostBuilding = null; } + // Add this field to PlacementManager + private float _currentRotation = 0f; + + // Add this method to handle rotation + private void RotateGhost(bool reverse = false) + { + if (_ghostBuilding == null) return; + if (reverse) + _currentRotation = (_currentRotation - 90f) % 360f; + else + _currentRotation = (_currentRotation + 90f) % 360f; + _ghostBuilding.RotationDegrees = _currentRotation; + } + public override void _Process(double delta) { // Snap mouse to grid var mousePos = GetGlobalMousePosition(); _hoveredCell = GridUtils.WorldToGrid(mousePos); + if (Input.IsActionJustPressed("rotate_tile_reverse")) + RotateGhost(reverse: true); + else if (Input.IsActionJustPressed("rotate_tile")) + RotateGhost(); + if (_ghostBuilding == null) { var scene = Registry.GetBuilding(_currentBuildingId)?.Scene; if (scene == null) return; - + _ghostBuilding = (BaseTile)scene.Instantiate(); _ghostBuilding.SetGhostMode(true); + _ghostBuilding.RotationDegrees = _currentRotation; AddChild(_ghostBuilding); } @@ -58,13 +78,15 @@ public partial class PlacementManager : Node2D _ghostBuilding.Position = placementPos; _ghostBuilding.SetGhostMode(canPlace); + // Left click to place if (Input.IsActionPressed("build_tile") && canPlace) { var scene = Registry.GetBuilding(_currentBuildingId)?.Scene; if (scene == null) return; - + _ghostBuilding.FinalizePlacement(); + _ghostBuilding.RotationDegrees = _currentRotation; var buildingData = Registry.GetBuilding(_currentBuildingId); Grid.OccupyCell(_hoveredCell, _ghostBuilding); @@ -74,6 +96,7 @@ public partial class PlacementManager : Node2D _ghostBuilding = (BaseTile)scene.Instantiate(); _ghostBuilding.SetGhostMode(true); + _ghostBuilding.RotationDegrees = _currentRotation; AddChild(_ghostBuilding); } diff --git a/project.godot b/project.godot index fa875a5..e102079 100644 --- a/project.godot +++ b/project.godot @@ -55,6 +55,21 @@ destroy_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":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) ] } +rotate_tile={ +"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":79,"key_label":0,"unicode":111,"location":0,"echo":false,"script":null) +] +} +rotate_tile_reverse={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":true,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":79,"key_label":0,"unicode":79,"location":0,"echo":false,"script":null) +] +} +move_sprint={ +"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":4194325,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +] +} [rendering]