💄 Optimize the unable to place sound plays

This commit is contained in:
2025-08-29 17:54:31 +08:00
parent 483773f042
commit 60b6d6f989

View File

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