From 60b6d6f98916364d51ea4a4a969245f66d5e95f8 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 29 Aug 2025 17:54:31 +0800 Subject: [PATCH] :lipstick: Optimize the unable to place sound plays --- Scripts/System/PlacementManager.cs | 33 +++++++++++++++++------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/Scripts/System/PlacementManager.cs b/Scripts/System/PlacementManager.cs index fa159ba..d47b5da 100644 --- a/Scripts/System/PlacementManager.cs +++ b/Scripts/System/PlacementManager.cs @@ -223,6 +223,20 @@ public partial class PlacementManager : Node2D return; } + // First check if area is free + if (!IsAreaFree(_hoveredCell, building.Size, _currentRotation, building.Layer)) + { + // Check if the area is occupied by under-construction tiles + var occupiedCells = GridUtils.GetOccupiedCells(_hoveredCell, building.Size, _currentRotation); + var isUnderConstruction = occupiedCells.Any(cell => + Grid.GetBuildingAtCell(cell, building.Layer) is BaseTile { IsConstructing: true }); + + if (!isUnderConstruction) + _cannotDeploySound.Play(); + + return; + } + // Consume resources first if (!ConsumeBuildingResources(_currentBuildingId)) { @@ -230,7 +244,7 @@ public partial class PlacementManager : Node2D return; } - // Create the building instance first + // Create the building instance var scene = building.Scene; var buildingInstance = (BaseTile)scene.Instantiate(); buildingInstance.Grid = Grid; @@ -239,15 +253,6 @@ public partial class PlacementManager : Node2D buildingInstance.Position = _ghostBuilding.Position; AddChild(buildingInstance); - // First check if area is free - if (!IsAreaFree(_hoveredCell, building.Size, _currentRotation, building.Layer)) - { - _cannotDeploySound.Play(); - RefundBuildingResources(_currentBuildingId); - buildingInstance.QueueFree(); - return; - } - // If we get here, area is free, so we can safely occupy it Grid.OccupyArea(_hoveredCell, buildingInstance, building.Size, _currentRotation, building.Layer); @@ -286,11 +291,11 @@ public partial class PlacementManager : Node2D // Right click to destroy from current layer var building = Grid.GetBuildingAtCell(_hoveredCell); if (building == null) return; - + // Find all cells occupied by this building var buildingInfo = Grid.GetBuildingInfoAtCell(_hoveredCell, GridLayer.Building); if (buildingInfo == null) return; - + // Check if this building is in the build tasks (under construction) if (_buildTasks.TryGetValue(building, out var buildTask)) { @@ -300,13 +305,13 @@ public partial class PlacementManager : Node2D _buildTasks.Remove(building); _canceledSound.Play(); if (buildingTile == null) return; - + // Refund resources for canceled build var buildingData = Registry.GetBuilding(buildingTile.TileId); if (buildingData != null) RefundBuildingResources(buildingTile.TileId); } - + // Clean up the building and grid building.QueueFree(); Grid.FreeArea(buildingInfo.Value.Position, buildingInfo.Value.Size, buildingInfo.Value.Rotation);