wangguan
2020-11-12 a7956acb794f93fc01e16963efe1f38743b110f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Collections.Generic;
using Core.Health;
using UnityEngine;
 
namespace ActionGameFramework.Audio
{
    /// <summary>
    /// Health change audio source - a helper for playing sounds on Health Change
    /// </summary>
    [RequireComponent(typeof(AudioSource))]
    public class HealthChangeAudioSource : MonoBehaviour
    {
        /// <summary>
        /// The sound selector. A mechanism of specifying how sounds are selected based on HealthChangeInfo
        /// </summary>
        public HealthChangeSoundSelector soundSelector;
 
        /// <summary>
        /// The audio source
        /// </summary>
        protected AudioSource m_Source;
 
        /// <summary>
        /// Assign the required AudioSource reference at runtime
        /// </summary>
        protected virtual void Awake()
        {
            m_Source = GetComponent<AudioSource>();
        }
 
        /// <summary>
        /// Play the AudioSource
        /// </summary>
        public virtual void PlaySound()
        {
            m_Source.Play();
        }
 
        /// <summary>
        /// Play a clip when certain health change requirements are met
        /// </summary>
        /// <param name="info">Uses <see cref="HealthChangeInfo"/> to determine what clip to play</param>
        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();
        }
 
        /// <summary>
        /// Sorts the <see cref="soundSelector"/> sound list
        /// </summary>
        public void Sort()
        {
            if (soundSelector.healthChangeSounds == null || soundSelector.healthChangeSounds.Count <= 0)
            {
                return;
            }
            soundSelector.healthChangeSounds.Sort(new HealthChangeSoundComparer());
        }
    }
 
    /// <summary>
    /// Provides a way to compare 2 <see cref="HealthChangeSound"/>s
    /// </summary>
    public class HealthChangeSoundComparer : IComparer<HealthChangeSound>
    {
        /// <summary>
        /// Compares 2 <see cref="HealthChangeSound"/>
        /// </summary>
        public int Compare(HealthChangeSound first, HealthChangeSound second)
        {
            if (first.healthChange == second.healthChange)
            {
                return 0;
            }
            if (first.healthChange < second.healthChange)
            {
                return -1;
            }
 
            return 1;
        }
    }
}