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