using System.Collections.Generic; using Core.Health; using UnityEngine; namespace ActionGameFramework.Audio { /// /// Health change audio source - a helper for playing sounds on Health Change /// [RequireComponent(typeof(AudioSource))] public class HealthChangeAudioSource : MonoBehaviour { /// /// The sound selector. A mechanism of specifying how sounds are selected based on HealthChangeInfo /// public HealthChangeSoundSelector soundSelector; /// /// The audio source /// protected AudioSource m_Source; /// /// Assign the required AudioSource reference at runtime /// protected virtual void Awake() { m_Source = GetComponent(); } /// /// Play the AudioSource /// public virtual void PlaySound() { m_Source.Play(); } /// /// Play a clip when certain health change requirements are met /// /// Uses to determine what clip to play public virtual void PlayHealthChangeSound(HealthChangeInfo info) { if (soundSelector != null && soundSelector.isSetUp) { AudioClip newClip = soundSelector.GetClipFromHealthChangeInfo(info); if (newClip != null) { m_Source.clip = newClip; } } m_Source.Play(); } /// /// Sorts the sound list /// public void Sort() { if (soundSelector.healthChangeSounds == null || soundSelector.healthChangeSounds.Count <= 0) { return; } soundSelector.healthChangeSounds.Sort(new HealthChangeSoundComparer()); } } /// /// Provides a way to compare 2 s /// public class HealthChangeSoundComparer : IComparer { /// /// Compares 2 /// public int Compare(HealthChangeSound first, HealthChangeSound second) { if (first.healthChange == second.healthChange) { return 0; } if (first.healthChange < second.healthChange) { return -1; } return 1; } } }