//-------------------------------------------------------- 
 | 
//    [Author]:           玩个游戏 
 | 
//    [  Date ]:           Tuesday, January 30, 2018 
 | 
//-------------------------------------------------------- 
 | 
using UnityEngine; 
 | 
using System.Collections; 
 | 
using UnityEngine.UI; 
 | 
using System; 
 | 
  
 | 
using System.Collections.Generic; 
 | 
  
 | 
  
 | 
public class SoundUtility 
 | 
{ 
 | 
    static DateTime lastHitAudioPlayTime = DateTime.Now; 
 | 
    static int hitAudioTimeInterval = 50;//毫秒 
 | 
  
 | 
    static int hitAudioPoolMax = 2; 
 | 
    static int hitAudioCountPool = 2; 
 | 
  
 | 
    static Dictionary<int, List<int>> jobHitAudio = new Dictionary<int, List<int>>() { 
 | 
        { 1,new List<int>(){101,102,103,104} }, 
 | 
        { 2,new List<int>(){146,147,148,149} }, 
 | 
        { 3,new List<int>(){101,102,103,104} }, 
 | 
    }; 
 | 
  
 | 
    public static void PlayHitAudio(AudioSource _audioSource) 
 | 
    { 
 | 
        if (_audioSource == null) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        if (hitAudioCountPool < hitAudioPoolMax) 
 | 
        { 
 | 
            hitAudioCountPool = Mathf.Clamp(hitAudioCountPool + (int)((DateTime.Now - lastHitAudioPlayTime).TotalMilliseconds / hitAudioTimeInterval), 0, hitAudioPoolMax); 
 | 
        } 
 | 
  
 | 
        if (hitAudioCountPool > 0) 
 | 
        { 
 | 
            var job = PlayerDatas.Instance.baseData.Job; 
 | 
            if (jobHitAudio.ContainsKey(job)) 
 | 
            { 
 | 
                var audios = jobHitAudio[job]; 
 | 
                var index = UnityEngine.Random.Range(0, audios.Count); 
 | 
                SoundPlayer.Instance.PlayAudio(_audioSource, audios[index]); 
 | 
                lastHitAudioPlayTime = DateTime.Now; 
 | 
                hitAudioCountPool--; 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    static Dictionary<int, int[]> fightRoars = new Dictionary<int, int[]>() { 
 | 
        {  1,new int[]{ 97,98,99 } }, 
 | 
        {  2,new int[]{ 142,143,144 } }, 
 | 
        }; 
 | 
  
 | 
    public static void PlayFightRoar(AudioSource _audioSource, int _job) 
 | 
    { 
 | 
        var ableIndex = UnityEngine.Random.Range(0, 3); 
 | 
        if (ableIndex != 1) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        int[] audioList = null; 
 | 
        if (fightRoars.ContainsKey(_job)) 
 | 
        { 
 | 
            audioList = fightRoars[_job]; 
 | 
        } 
 | 
  
 | 
        if (audioList == null) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        var randomIndex = UnityEngine.Random.Range(0, audioList.Length); 
 | 
        var audio = audioList[randomIndex]; 
 | 
  
 | 
        SoundPlayer.Instance.PlayAudio(_audioSource, audio); 
 | 
  
 | 
    } 
 | 
  
 | 
    static Dictionary<int, int> deadAudios = new Dictionary<int, int>() 
 | 
    { 
 | 
        { 1,100}, 
 | 
        { 2,145}, 
 | 
        { 3,100}, 
 | 
    }; 
 | 
  
 | 
    public static void PlayDeadAudio(AudioSource _audioSource, int _job) 
 | 
    { 
 | 
        SoundPlayer.Instance.PlayAudio(_audioSource, deadAudios[_job]); 
 | 
    } 
 | 
  
 | 
    static Dictionary<int, int[]> stateFootAudios = new Dictionary<int, int[]>() 
 | 
    { 
 | 
        { 1,new int[]{ 130, 131, 132,133} }, 
 | 
        { 2,new int[]{ 134, 135, 136, 137 } }, 
 | 
        { 3,new int[]{ 138, 139, 140, 141 } }, 
 | 
    }; 
 | 
  
 | 
    static Dictionary<int, int[]> rideAudios = new Dictionary<int, int[]>(); 
 | 
  
 | 
    static FootAudioType currentState = FootAudioType.Walk; 
 | 
    static int audioIndex = 0; 
 | 
    static Dictionary<int, DateTime> stateFootAudioLastPlayTimes = new Dictionary<int, DateTime>(); 
 | 
  
 | 
    const float overTime = 2f; 
 | 
  
 | 
    public static void PlayFootAudio(AudioSource _audioSource, FootAudioType _state, int _horseId = 0) 
 | 
    { 
 | 
        var stateInt = (int)_state; 
 | 
        if (!stateFootAudios.ContainsKey(stateInt)) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        if (currentState != _state) 
 | 
        { 
 | 
            audioIndex = 0; 
 | 
            currentState = _state; 
 | 
        } 
 | 
  
 | 
        if (stateFootAudioLastPlayTimes.ContainsKey(stateInt)) 
 | 
        { 
 | 
            var lastPlayTime = stateFootAudioLastPlayTimes[stateInt]; 
 | 
            if ((DateTime.Now - lastPlayTime).TotalSeconds > 2f) 
 | 
            { 
 | 
                audioIndex = 0; 
 | 
            } 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            audioIndex = 0; 
 | 
        } 
 | 
  
 | 
        int[] audios = null; 
 | 
        switch (stateInt) 
 | 
        { 
 | 
            case 1: 
 | 
            case 3: 
 | 
                audios = stateFootAudios[(int)_state]; 
 | 
                break; 
 | 
            // case 2: 
 | 
            //     if (rideAudios.ContainsKey(_horseId)) 
 | 
            //     { 
 | 
            //         audios = rideAudios[_horseId]; 
 | 
            //     } 
 | 
            //     else 
 | 
            //     { 
 | 
            //         var config = HorseConfig.Get(_horseId); 
 | 
            //         audios = new int[config.RideAudios.Length]; 
 | 
            //         Array.Copy(config.RideAudios, audios, config.RideAudios.Length); 
 | 
            //         rideAudios[_horseId] = audios; 
 | 
            //     } 
 | 
            //     break; 
 | 
            default: 
 | 
                break; 
 | 
        } 
 | 
  
 | 
  
 | 
        if (audioIndex < audios.Length) 
 | 
        { 
 | 
            var audio = audios[audioIndex]; 
 | 
            SoundPlayer.Instance.PlayAudio(_audioSource, audio); 
 | 
            stateFootAudioLastPlayTimes[stateInt] = DateTime.Now; 
 | 
            audioIndex++; 
 | 
        } 
 | 
  
 | 
        if (audioIndex >= audios.Length) 
 | 
        { 
 | 
            audioIndex = 0; 
 | 
        } 
 | 
    } 
 | 
  
 | 
} 
 |