death sounds, build sounds, better audio management

This commit is contained in:
Ian Woods 2026-06-30 23:12:27 -07:00
parent a7b2d1854d
commit 9f25844657
26 changed files with 313 additions and 12 deletions

View file

@ -0,0 +1,42 @@
// 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);
}
}
}