42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
// Assets/_Project/Scripts/Combat/EnemyDeathSound.cs
|
|
using TD.Audio;
|
|
using TD.Gameplay;
|
|
using UnityEngine;
|
|
|
|
namespace TD.Combat
|
|
{
|
|
/// <summary>
|
|
/// Local-only audio for enemy death. Subscribes to <see cref="EnemyHealth.OnDiedClient"/>
|
|
/// and plays a randomly chosen clip from <see cref="deathClips"/>.
|
|
/// Attach to any enemy prefab and assign its clips in the Inspector.
|
|
/// </summary>
|
|
[RequireComponent(typeof(EnemyHealth))]
|
|
public class EnemyDeathSound : MonoBehaviour
|
|
{
|
|
[SerializeField] private SoundConfig[] deathSounds;
|
|
|
|
private EnemyHealth enemyHealth;
|
|
|
|
private void Awake()
|
|
{
|
|
enemyHealth = GetComponent<EnemyHealth>();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (enemyHealth != null) enemyHealth.OnDiedClient += HandleDied;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (enemyHealth != null) enemyHealth.OnDiedClient -= HandleDied;
|
|
}
|
|
|
|
private void HandleDied()
|
|
{
|
|
if (deathSounds == null || deathSounds.Length == 0) return;
|
|
var sound = deathSounds[Random.Range(0, deathSounds.Length)];
|
|
AudioManager.Instance?.Play(sound.clip, AudioCategory.EnemyHitDeath, sound.RandomPitch(), sound.volume);
|
|
}
|
|
}
|
|
}
|