Building task queue and remove duplicated construction complete sound

This commit is contained in:
2025-08-28 22:45:51 +08:00
parent af31914ada
commit 0d3c84491b
2 changed files with 84 additions and 20 deletions

View File

@@ -10,8 +10,68 @@ public partial class PlacementManager : Node2D
{
[Export] public GridManager Grid { get; set; }
[Export] public BuildingRegistry Registry { get; set; }
[Export] public int MaxConcurrentBuilds { get; set; } = 6; // Make it adjustable in editor
private static readonly List<string> BuildableTiles = ["wall", "miner"];
private readonly List<BuildTask> _activeBuilds = new();
private AudioStreamPlayer _completionSound;
public override void _Ready()
{
base._Ready();
// Setup completion sound
_completionSound = new AudioStreamPlayer();
AddChild(_completionSound);
var sound = GD.Load<AudioStream>("res://Sounds/Events/ConstructionComplete.wav");
if (sound != null)
{
_completionSound.Stream = sound;
}
}
private void OnBuildCompleted()
{
// Remove all completed builds
_activeBuilds.RemoveAll(task => task.IsCompleted);
// If no builds left, play the completion sound
if (_activeBuilds.Count == 0)
{
_completionSound.Play();
}
}
// Call this when starting a new build
private bool CanStartNewBuild()
{
// Remove completed builds
_activeBuilds.RemoveAll(task => task.IsCompleted);
return _activeBuilds.Count < MaxConcurrentBuilds;
}
private class BuildTask : IDisposable
{
private readonly Action _onCompleted;
public bool IsCompleted { get; private set; }
public BuildTask(Action onCompleted)
{
_onCompleted = onCompleted;
}
public void Complete()
{
if (IsCompleted) return;
IsCompleted = true;
_onCompleted?.Invoke();
}
public void Dispose()
{
Complete();
}
}
private string _currentBuildingId = "wall";
private Vector2I _hoveredCell;
@@ -121,6 +181,12 @@ public partial class PlacementManager : Node2D
{
var building = Registry.GetBuilding(_currentBuildingId);
if (building == null) return;
if (!CanStartNewBuild())
{
// Optionally show feedback to player that build queue is full
return;
}
var scene = building.Scene;
var buildingInstance = (BaseTile)scene.Instantiate();
@@ -132,7 +198,11 @@ public partial class PlacementManager : Node2D
Grid.OccupyArea(_hoveredCell, buildingInstance, building.Size, _currentRotation, building.Layer);
if (building.BuildTime > 0f)
buildingInstance.StartConstruction(building.BuildTime);
{
var buildTask = new BuildTask(OnBuildCompleted);
_activeBuilds.Add(buildTask);
buildingInstance.StartConstruction(building.BuildTime, buildTask.Complete);
}
}
if (Input.IsActionPressed("destroy_tile") &&