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 GridManager Grid { get; set; }
[Export] public BuildingRegistry Registry { 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 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 string _currentBuildingId = "wall";
private Vector2I _hoveredCell; private Vector2I _hoveredCell;
@@ -122,6 +182,12 @@ public partial class PlacementManager : Node2D
var building = Registry.GetBuilding(_currentBuildingId); var building = Registry.GetBuilding(_currentBuildingId);
if (building == null) return; if (building == null) return;
if (!CanStartNewBuild())
{
// Optionally show feedback to player that build queue is full
return;
}
var scene = building.Scene; var scene = building.Scene;
var buildingInstance = (BaseTile)scene.Instantiate(); var buildingInstance = (BaseTile)scene.Instantiate();
buildingInstance.RotationDegrees = _currentRotation; buildingInstance.RotationDegrees = _currentRotation;
@@ -132,7 +198,11 @@ public partial class PlacementManager : Node2D
Grid.OccupyArea(_hoveredCell, buildingInstance, building.Size, _currentRotation, building.Layer); Grid.OccupyArea(_hoveredCell, buildingInstance, building.Size, _currentRotation, building.Layer);
if (building.BuildTime > 0f) 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") && if (Input.IsActionPressed("destroy_tile") &&

View File

@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using AceFieldNewHorizon.Scripts.System; using AceFieldNewHorizon.Scripts.System;
using Godot; using Godot;
@@ -12,7 +13,7 @@ public partial class BaseTile : Node2D
private CollisionShape2D _collisionShape; private CollisionShape2D _collisionShape;
private Sprite2D _sprite; private Sprite2D _sprite;
private ColorRect _progressOverlay; private ColorRect _progressOverlay;
private AudioStreamPlayer _completionSound; private Action _onConstructionComplete;
public override void _Ready() public override void _Ready()
{ {
@@ -21,15 +22,6 @@ public partial class BaseTile : Node2D
_progressOverlay = GetNodeOrNull<ColorRect>("ProgressOverlay"); _progressOverlay = GetNodeOrNull<ColorRect>("ProgressOverlay");
if (_progressOverlay != null) if (_progressOverlay != null)
_progressOverlay.Visible = false; _progressOverlay.Visible = false;
// Setup audio player
_completionSound = new AudioStreamPlayer();
AddChild(_completionSound);
var sound = GD.Load<AudioStream>("res://Sounds/Events/ConstructionComplete.wav");
if (sound != null)
{
_completionSound.Stream = sound;
}
} }
public void SetGhostMode(bool canPlace) public void SetGhostMode(bool canPlace)
@@ -52,10 +44,15 @@ public partial class BaseTile : Node2D
} }
// Building progress visualization // Building progress visualization
public void StartConstruction(float buildTime) public void StartConstruction(float buildTime, Action onComplete = null)
{ {
if (_progressOverlay == null || _sprite?.Texture == null) return; if (_progressOverlay == null || _sprite?.Texture == null)
{
onComplete?.Invoke();
return;
}
_onConstructionComplete = onComplete;
var texSize = new Vector2(GridUtils.TileSize, GridUtils.TileSize); var texSize = new Vector2(GridUtils.TileSize, GridUtils.TileSize);
_progressOverlay.Visible = true; _progressOverlay.Visible = true;
@@ -80,8 +77,11 @@ public partial class BaseTile : Node2D
); );
} }
// Fade out the overlay instead of making it instantly disappear // Fade out the overlay
await FadeOutOverlay(0.5f); await FadeOutOverlay(0.5f);
// Notify completion
_onConstructionComplete?.Invoke();
} }
RunProgress(); RunProgress();
@@ -92,12 +92,6 @@ public partial class BaseTile : Node2D
var elapsed = 0f; var elapsed = 0f;
var startAlpha = _progressOverlay.Modulate.A; var startAlpha = _progressOverlay.Modulate.A;
// Play completion sound
if (_completionSound?.Stream != null)
{
_completionSound.Play();
}
while (elapsed < duration) while (elapsed < duration)
{ {
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame); await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);