| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.SettingsCommon; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using AudioSettings = DCL.SettingsCommon.AudioSettings; |
| | 6 | |
|
| | 7 | | namespace DCLServices.EmotesService |
| | 8 | | { |
| | 9 | | public class EmoteVolumeHandler |
| | 10 | | { |
| | 11 | | private const float BASE_VOLUME = 0.2f; |
| | 12 | |
|
| 425 | 13 | | private readonly List<AudioSource> audioSources = new (); |
| | 14 | | private readonly ISettingsRepository<AudioSettings> audioSettings; |
| | 15 | |
|
| 425 | 16 | | public EmoteVolumeHandler() |
| | 17 | | { |
| 425 | 18 | | audioSettings = Settings.i.audioSettings; |
| 425 | 19 | | audioSettings.OnChanged += OnVolumeChanged; |
| 425 | 20 | | OnVolumeChanged(audioSettings.Data); |
| 425 | 21 | | } |
| | 22 | |
|
| | 23 | | public void AddAudioSource(AudioSource audioSource) |
| | 24 | | { |
| 0 | 25 | | audioSources.Add(audioSource); |
| 0 | 26 | | SetVolume(audioSource, GetVolume(audioSettings.Data)); |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | public void RemoveAudioSource(AudioSource audioSource) |
| | 30 | | { |
| 0 | 31 | | audioSources.Remove(audioSource); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | private void SetVolume(AudioSource audioSource, float volume) |
| | 35 | | { |
| 0 | 36 | | audioSource.volume = volume; |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | private void OnVolumeChanged(AudioSettings settings) |
| | 40 | | { |
| 427 | 41 | | float targetVolume = GetVolume(settings); |
| | 42 | |
|
| 854 | 43 | | foreach (AudioSource audioSource in audioSources) |
| 0 | 44 | | SetVolume(audioSource, targetVolume); |
| 427 | 45 | | } |
| | 46 | |
|
| | 47 | | private static float GetVolume(AudioSettings audioSettings) => |
| 427 | 48 | | BASE_VOLUME * Utils.ToVolumeCurve(audioSettings.avatarSFXVolume * audioSettings.masterVolume); |
| | 49 | | } |
| | 50 | | } |