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
using System;
using Core.Extensions;
using UnityEngine;
 
namespace ActionGameFramework.Audio
{
    /// <summary>
    /// Weighted audio list
    /// </summary>
    [Serializable]
    public class WeightedAudioList
    {
        /// <summary>
        /// Items with their corresponding weights
        /// </summary>
        public WeightedAudioClip[] weightedItems;
 
        /// <summary>
        /// The sum of all items weights
        /// </summary>
        protected int m_WeightSum = -1;
 
        /// <summary>
        /// Gets the weight sum.
        /// </summary>
        /// <value>The weight sum.</value>
        public int weightSum
        {
            get
            {
                if (m_WeightSum < 0)
                {
                    CalculateWeightSum();
                }
 
                return m_WeightSum;
            }
        }
 
        /// <summary>
        /// Gets a random audio clip from the weighted list
        /// </summary>
        /// <returns>The selection.</returns>
        public AudioClip WeightedSelection()
        {
            if (weightedItems.Length == 0)
            {
                return null;
            }
 
            WeightedAudioClip item = weightedItems.WeightedSelection(weightSum, t => t.weight);
            return item.clip;
        }
 
        /// <summary>
        /// Calculates the sum of all item weights
        /// </summary>
        protected void CalculateWeightSum()
        {
            m_WeightSum = 0;
            int count = weightedItems.Length;
            for (int i = 0; i < count; i++)
            {
                m_WeightSum += weightedItems[i].weight;
            }
        }
    }
}