summaryrefslogtreecommitdiff
path: root/Client/Assembly-CSharp/SoundManager.cs
blob: 4d035739ddcdd06d91ee1b041baa7b1bafbc8da7 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;

public class SoundManager : MonoBehaviour
{
	public static SoundManager Instance
	{
		get
		{
			if (!SoundManager._Instance)
			{
				SoundManager._Instance = (UnityEngine.Object.FindObjectOfType<SoundManager>() ?? new GameObject("SoundManager").AddComponent<SoundManager>());
			}
			return SoundManager._Instance;
		}
	}

	private static SoundManager _Instance;

	public AudioMixerGroup musicMixer;

	public AudioMixerGroup sfxMixer;

	public static float MusicVolume = 1f;

	public static float SfxVolume = 1f;

	private Dictionary<AudioClip, AudioSource> allSources = new Dictionary<AudioClip, AudioSource>();

	private List<ISoundPlayer> soundPlayers = new List<ISoundPlayer>();

	public void Start()
	{
		if (SoundManager._Instance && SoundManager._Instance != this)
		{
			UnityEngine.Object.Destroy(base.gameObject);
			return;
		}
		SoundManager._Instance = this;
		this.UpdateVolume();
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
	}

	public void Update()
	{
		for (int i = 0; i < this.soundPlayers.Count; i++)
		{
			this.soundPlayers[i].Update(Time.deltaTime);
		}
	}

	private void UpdateVolume()
	{
		this.ChangeSfxVolume(SaveManager.SfxVolume);
		this.ChangeMusicVolume(SaveManager.MusicVolume);
	}

	public void ChangeSfxVolume(float volume)
	{
		if (volume <= 0f)
		{
			SoundManager.SfxVolume = -80f;
		}
		else
		{
			SoundManager.SfxVolume = Mathf.Log10(volume) * 20f;
		}
		this.musicMixer.audioMixer.SetFloat("SfxVolume", SoundManager.SfxVolume);
	}

	public void ChangeMusicVolume(float volume)
	{
		if (volume <= 0f)
		{
			SoundManager.MusicVolume = -80f;
		}
		else
		{
			SoundManager.MusicVolume = Mathf.Log10(volume) * 20f;
		}
		this.musicMixer.audioMixer.SetFloat("MusicVolume", SoundManager.MusicVolume);
	}

	public void StopSound(AudioClip clip)
	{
		AudioSource audioSource;
		if (this.allSources.TryGetValue(clip, out audioSource))
		{
			this.allSources.Remove(clip);
			audioSource.Stop();
			UnityEngine.Object.Destroy(audioSource);
		}
		for (int i = 0; i < this.soundPlayers.Count; i++)
		{
			ISoundPlayer soundPlayer = this.soundPlayers[i];
			if (soundPlayer.Player.clip == clip)
			{
				UnityEngine.Object.Destroy(soundPlayer.Player);
				this.soundPlayers.RemoveAt(i);
				return;
			}
		}
	}

	public void StopAllSound()
	{
		for (int i = 0; i < this.soundPlayers.Count; i++)
		{
			UnityEngine.Object.Destroy(this.soundPlayers[i].Player);
		}
		this.soundPlayers.Clear();
		foreach (KeyValuePair<AudioClip, AudioSource> keyValuePair in this.allSources)
		{
			AudioSource value = keyValuePair.Value;
			value.volume = 0f;
			value.Stop();
			UnityEngine.Object.Destroy(keyValuePair.Value);
		}
		this.allSources.Clear();
	}

	public void PlayDynamicSound(string name, AudioClip clip, bool loop, DynamicSound.GetDynamicsFunction volumeFunc, bool playAsSfx = false)
	{
		DynamicSound dynamicSound = null;
		for (int i = 0; i < this.soundPlayers.Count; i++)
		{
			ISoundPlayer soundPlayer = this.soundPlayers[i];
			if (soundPlayer.Name == name && soundPlayer is DynamicSound)
			{
				dynamicSound = (DynamicSound)soundPlayer;
				break;
			}
		}
		if (dynamicSound == null)
		{
			dynamicSound = new DynamicSound();
			dynamicSound.Name = name;
			dynamicSound.Player = base.gameObject.AddComponent<AudioSource>();
			dynamicSound.Player.outputAudioMixerGroup = ((loop && !playAsSfx) ? this.musicMixer : this.sfxMixer);
			dynamicSound.Player.playOnAwake = false;
			this.soundPlayers.Add(dynamicSound);
		}
		dynamicSound.Player.loop = loop;
		dynamicSound.SetTarget(clip, volumeFunc);
	}

	public void CrossFadeSound(string name, AudioClip clip, float maxVolume, float duration = 1.5f)
	{
		CrossFader crossFader = null;
		for (int i = 0; i < this.soundPlayers.Count; i++)
		{
			ISoundPlayer soundPlayer = this.soundPlayers[i];
			if (soundPlayer.Name == name && soundPlayer is CrossFader)
			{
				crossFader = (CrossFader)soundPlayer;
				break;
			}
		}
		if (crossFader == null)
		{
			crossFader = new CrossFader();
			crossFader.Name = name;
			crossFader.MaxVolume = maxVolume;
			crossFader.Player = base.gameObject.AddComponent<AudioSource>();
			crossFader.Player.outputAudioMixerGroup = this.musicMixer;
			crossFader.Player.playOnAwake = false;
			crossFader.Player.loop = true;
			this.soundPlayers.Add(crossFader);
		}
		crossFader.SetTarget(clip);
	}

	public AudioSource PlaySoundImmediate(AudioClip clip, bool loop, float volume = 1f, float pitch = 1f)
	{
		if (clip == null)
		{
			Debug.LogWarning("Missing audio clip");
			return null;
		}
		AudioSource audioSource;
		if (this.allSources.TryGetValue(clip, out audioSource))
		{
			audioSource.pitch = pitch;
			audioSource.loop = loop;
			audioSource.Play();
		}
		else
		{
			audioSource = base.gameObject.AddComponent<AudioSource>();
			audioSource.outputAudioMixerGroup = (loop ? this.musicMixer : this.sfxMixer);
			audioSource.playOnAwake = false;
			audioSource.volume = volume;
			audioSource.pitch = pitch;
			audioSource.loop = loop;
			audioSource.clip = clip;
			audioSource.Play();
			this.allSources.Add(clip, audioSource);
		}
		return audioSource;
	}

	public bool SoundIsPlaying(AudioClip clip)
	{
		AudioSource audioSource;
		return this.allSources.TryGetValue(clip, out audioSource) && !audioSource.isPlaying;
	}

	public AudioSource PlaySound(AudioClip clip, bool loop, float volume = 1f)
	{
		if (clip == null)
		{
			Debug.LogWarning("Missing audio clip");
			return null;
		}
		AudioSource audioSource;
		if (this.allSources.TryGetValue(clip, out audioSource))
		{
			if (!audioSource.isPlaying)
			{
				audioSource.loop = loop;
				audioSource.Play();
			}
		}
		else
		{
			audioSource = base.gameObject.AddComponent<AudioSource>();
			audioSource.outputAudioMixerGroup = (loop ? this.musicMixer : this.sfxMixer);
			audioSource.playOnAwake = false;
			audioSource.volume = volume;
			audioSource.loop = loop;
			audioSource.clip = clip;
			audioSource.Play();
			this.allSources.Add(clip, audioSource);
		}
		return audioSource;
	}
}