Rotate building tiles

This commit is contained in:
2025-08-27 17:37:31 +08:00
parent c078204e60
commit 84b908ef51
2 changed files with 41 additions and 3 deletions

View File

@@ -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);
}

View File

@@ -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]