46 lines
1.7 KiB
C#
46 lines
1.7 KiB
C#
// Assets/_Project/Scripts/Combat/TowerFallingSound.cs
|
|
using TD.Audio;
|
|
using TD.Gameplay;
|
|
using UnityEngine;
|
|
|
|
namespace TD.Combat
|
|
{
|
|
/// <summary>
|
|
/// Local-only audio for the tower falling from the sky. Attached to
|
|
/// <see cref="BuildSiteVisual"/> (not <see cref="TowerInstance"/>, which doesn't exist
|
|
/// yet) and polled every frame: plays a sound on every peer once construction has
|
|
/// <see cref="triggerSecondsBeforeCompletion"/> seconds or less remaining, so the whoosh
|
|
/// leads the tower's actual fall/land animation (see <see cref="TowerLandingVisual"/>,
|
|
/// which starts once <see cref="BuildSiteVisual"/> is replaced by the real
|
|
/// <see cref="TowerInstance"/> at construction-complete).
|
|
/// </summary>
|
|
[RequireComponent(typeof(BuildSiteVisual))]
|
|
public class TowerFallingSound : MonoBehaviour
|
|
{
|
|
[SerializeField] private SoundConfig fallingSound;
|
|
|
|
[Tooltip("Sound fires once remaining construction time drops to this many seconds " +
|
|
"or less.")]
|
|
[SerializeField] private float triggerSecondsBeforeCompletion = 1f;
|
|
|
|
private BuildSiteVisual buildSiteVisual;
|
|
private bool played;
|
|
|
|
private void Awake()
|
|
{
|
|
buildSiteVisual = GetComponent<BuildSiteVisual>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (played || buildSiteVisual == null) return;
|
|
if (buildSiteVisual.CurrentStage != BuildStage.Constructing) return;
|
|
|
|
if (buildSiteVisual.ComputeRemainingSeconds() <= triggerSecondsBeforeCompletion)
|
|
{
|
|
played = true;
|
|
AudioManager.Instance?.Play(fallingSound.clip, AudioCategory.UI, fallingSound.RandomPitch(), fallingSound.volume);
|
|
}
|
|
}
|
|
}
|
|
}
|