💄 Optimize the unable to place sound plays
This commit is contained in:
@@ -223,6 +223,20 @@ public partial class PlacementManager : Node2D
|
|||||||
return;
|
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
|
// Consume resources first
|
||||||
if (!ConsumeBuildingResources(_currentBuildingId))
|
if (!ConsumeBuildingResources(_currentBuildingId))
|
||||||
{
|
{
|
||||||
@@ -230,7 +244,7 @@ public partial class PlacementManager : Node2D
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the building instance first
|
// Create the building instance
|
||||||
var scene = building.Scene;
|
var scene = building.Scene;
|
||||||
var buildingInstance = (BaseTile)scene.Instantiate();
|
var buildingInstance = (BaseTile)scene.Instantiate();
|
||||||
buildingInstance.Grid = Grid;
|
buildingInstance.Grid = Grid;
|
||||||
@@ -239,15 +253,6 @@ public partial class PlacementManager : Node2D
|
|||||||
buildingInstance.Position = _ghostBuilding.Position;
|
buildingInstance.Position = _ghostBuilding.Position;
|
||||||
AddChild(buildingInstance);
|
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
|
// If we get here, area is free, so we can safely occupy it
|
||||||
Grid.OccupyArea(_hoveredCell, buildingInstance, building.Size, _currentRotation, building.Layer);
|
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
|
// Right click to destroy from current layer
|
||||||
var building = Grid.GetBuildingAtCell(_hoveredCell);
|
var building = Grid.GetBuildingAtCell(_hoveredCell);
|
||||||
if (building == null) return;
|
if (building == null) return;
|
||||||
|
|
||||||
// Find all cells occupied by this building
|
// Find all cells occupied by this building
|
||||||
var buildingInfo = Grid.GetBuildingInfoAtCell(_hoveredCell, GridLayer.Building);
|
var buildingInfo = Grid.GetBuildingInfoAtCell(_hoveredCell, GridLayer.Building);
|
||||||
if (buildingInfo == null) return;
|
if (buildingInfo == null) return;
|
||||||
|
|
||||||
// Check if this building is in the build tasks (under construction)
|
// Check if this building is in the build tasks (under construction)
|
||||||
if (_buildTasks.TryGetValue(building, out var buildTask))
|
if (_buildTasks.TryGetValue(building, out var buildTask))
|
||||||
{
|
{
|
||||||
@@ -300,13 +305,13 @@ public partial class PlacementManager : Node2D
|
|||||||
_buildTasks.Remove(building);
|
_buildTasks.Remove(building);
|
||||||
_canceledSound.Play();
|
_canceledSound.Play();
|
||||||
if (buildingTile == null) return;
|
if (buildingTile == null) return;
|
||||||
|
|
||||||
// Refund resources for canceled build
|
// Refund resources for canceled build
|
||||||
var buildingData = Registry.GetBuilding(buildingTile.TileId);
|
var buildingData = Registry.GetBuilding(buildingTile.TileId);
|
||||||
if (buildingData != null)
|
if (buildingData != null)
|
||||||
RefundBuildingResources(buildingTile.TileId);
|
RefundBuildingResources(buildingTile.TileId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up the building and grid
|
// Clean up the building and grid
|
||||||
building.QueueFree();
|
building.QueueFree();
|
||||||
Grid.FreeArea(buildingInfo.Value.Position, buildingInfo.Value.Size, buildingInfo.Value.Rotation);
|
Grid.FreeArea(buildingInfo.Value.Position, buildingInfo.Value.Size, buildingInfo.Value.Rotation);
|
||||||
|
Reference in New Issue
Block a user