yyl
昨天 5d3366f2e0f687995eb7ad2107c4379fe7acd4e8
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//--------------------------------------------------------
//    [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;
        }
    }
 
}