| | 1 | | using System; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.ECS7.InternalComponents; |
| | 4 | | using DCL.ECSRuntime; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Models; |
| | 7 | | using DCL.SettingsCommon; |
| | 8 | | using UnityEngine; |
| | 9 | | using AudioSettings = DCL.SettingsCommon.AudioSettings; |
| | 10 | |
|
| | 11 | | namespace DCL.ECSComponents |
| | 12 | | { |
| | 13 | | public class ECSAudioSourceComponentHandler : IECSComponentHandler<PBAudioSource> |
| | 14 | | { |
| | 15 | | internal AudioSource audioSource; |
| | 16 | | internal AssetPromise_AudioClip promiseAudioClip; |
| | 17 | |
|
| | 18 | | private bool isAudioClipReady = false; |
| 11 | 19 | | private bool isPlayerInsideScene = true; |
| 11 | 20 | | private bool isEntityInsideScene = true; |
| | 21 | |
|
| | 22 | | private PBAudioSource model; |
| | 23 | | private IParcelScene scene; |
| | 24 | | private AudioClip audioClip; |
| | 25 | |
|
| | 26 | | private readonly DataStore dataStore; |
| | 27 | | private readonly Settings settings; |
| | 28 | | private readonly AssetPromiseKeeper_AudioClip keeperAudioClip; |
| | 29 | | private readonly IntVariable sceneNumber; |
| | 30 | | private readonly IInternalECSComponent<InternalAudioSource> audioSourceInternalComponent; |
| | 31 | | private readonly IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent; |
| | 32 | |
|
| 11 | 33 | | public ECSAudioSourceComponentHandler( |
| | 34 | | DataStore dataStoreInstance, |
| | 35 | | Settings settingsInstance, |
| | 36 | | AssetPromiseKeeper_AudioClip keeperInstance, |
| | 37 | | IntVariable sceneNumber, |
| | 38 | | IInternalECSComponent<InternalAudioSource> audioSourceInternalComponent, |
| | 39 | | IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent) |
| | 40 | | { |
| 11 | 41 | | dataStore = dataStoreInstance; |
| 11 | 42 | | settings = settingsInstance; |
| 11 | 43 | | keeperAudioClip = keeperInstance; |
| 11 | 44 | | this.sceneNumber = sceneNumber; |
| 11 | 45 | | this.audioSourceInternalComponent = audioSourceInternalComponent; |
| 11 | 46 | | this.sbcInternalComponent = sbcInternalComponent; |
| 11 | 47 | | } |
| | 48 | |
|
| | 49 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) |
| | 50 | | { |
| 11 | 51 | | this.scene = scene; |
| 11 | 52 | | audioSource = entity.gameObject.AddComponent<AudioSource>(); |
| 11 | 53 | | audioSource.spatialBlend = 1; |
| 11 | 54 | | audioSource.dopplerLevel = 0.1f; |
| 11 | 55 | | audioSource.playOnAwake = false; |
| | 56 | |
|
| 11 | 57 | | if (settings != null) |
| 11 | 58 | | settings.audioSettings.OnChanged += OnAudioSettingsChanged; |
| | 59 | |
|
| 11 | 60 | | dataStore.virtualAudioMixer.sceneSFXVolume.OnChange += OnVirtualAudioMixerChangedValue; |
| 11 | 61 | | sceneNumber.OnChange += OnCurrentSceneChanged; |
| | 62 | |
|
| 11 | 63 | | audioSourceInternalComponent.PutFor(scene, entity, new InternalAudioSource() |
| | 64 | | { |
| | 65 | | audioSource = this.audioSource |
| | 66 | | }); |
| | 67 | |
|
| 11 | 68 | | sbcInternalComponent.RegisterOnSceneBoundsStateChangeCallback(scene, entity, OnSceneBoundsStateChange); |
| | 69 | |
|
| 11 | 70 | | isPlayerInsideScene = scene.sceneData.sceneNumber == sceneNumber.Get(); |
| 11 | 71 | | } |
| | 72 | |
|
| | 73 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 74 | | { |
| 12 | 75 | | audioSourceInternalComponent.RemoveFor(scene, entity, new InternalAudioSource() { audioSource = null }); |
| 12 | 76 | | Dispose(entity); |
| 12 | 77 | | } |
| | 78 | |
|
| | 79 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBAudioSource model) |
| | 80 | | { |
| 14 | 81 | | bool isSameClip = model.AudioClipUrl == this.model?.AudioClipUrl; |
| 14 | 82 | | this.model = model; |
| | 83 | |
|
| | 84 | | // If the clip has changed, we need to forget the old clip |
| 14 | 85 | | if (!isSameClip && promiseAudioClip != null) |
| | 86 | | { |
| 0 | 87 | | isAudioClipReady = false; |
| 0 | 88 | | if (audioClip != null) |
| 0 | 89 | | dataStore.sceneWorldObjects.RemoveAudioClip(scene.sceneData.sceneNumber, audioClip); |
| 0 | 90 | | DisposePromise(); |
| | 91 | | } |
| | 92 | |
|
| 14 | 93 | | ApplyCurrentModel(); |
| | 94 | |
|
| 14 | 95 | | if (!isAudioClipReady && !isSameClip) |
| | 96 | | { |
| 11 | 97 | | promiseAudioClip = new AssetPromise_AudioClip(model.AudioClipUrl, scene.contentProvider); |
| 11 | 98 | | promiseAudioClip.OnSuccessEvent += OnAudioClipLoadComplete; |
| 11 | 99 | | promiseAudioClip.OnFailEvent += OnAudioClipLoadFail; |
| | 100 | |
|
| 11 | 101 | | keeperAudioClip.Keep(promiseAudioClip); |
| | 102 | | } |
| 14 | 103 | | } |
| | 104 | |
|
| | 105 | | private void DisposePromise() |
| | 106 | | { |
| 12 | 107 | | if (promiseAudioClip == null) |
| 0 | 108 | | return; |
| | 109 | |
|
| 12 | 110 | | promiseAudioClip.OnSuccessEvent += OnAudioClipLoadComplete; |
| 12 | 111 | | promiseAudioClip.OnFailEvent += OnAudioClipLoadFail; |
| | 112 | |
|
| 12 | 113 | | keeperAudioClip.Forget(promiseAudioClip); |
| 12 | 114 | | } |
| | 115 | |
|
| | 116 | | private void Dispose(IDCLEntity entity) |
| | 117 | | { |
| 12 | 118 | | DisposePromise(); |
| | 119 | |
|
| 12 | 120 | | if (audioClip != null) |
| 0 | 121 | | dataStore.sceneWorldObjects.RemoveAudioClip(scene.sceneData.sceneNumber, audioClip); |
| | 122 | |
|
| 12 | 123 | | sceneNumber.OnChange -= OnCurrentSceneChanged; |
| | 124 | |
|
| 12 | 125 | | if (settings != null) |
| 12 | 126 | | settings.audioSettings.OnChanged -= OnAudioSettingsChanged; |
| | 127 | |
|
| 12 | 128 | | dataStore.virtualAudioMixer.sceneSFXVolume.OnChange -= OnVirtualAudioMixerChangedValue; |
| 12 | 129 | | if (audioSource != null) |
| | 130 | | { |
| 11 | 131 | | GameObject.Destroy(audioSource); |
| 11 | 132 | | audioSource = null; |
| | 133 | | } |
| | 134 | |
|
| 12 | 135 | | sbcInternalComponent.RemoveOnSceneBoundsStateChangeCallback(scene, entity, OnSceneBoundsStateChange); |
| 12 | 136 | | } |
| | 137 | |
|
| | 138 | | private void ApplyCurrentModel() |
| | 139 | | { |
| 17 | 140 | | if (audioSource == null) |
| | 141 | | { |
| 0 | 142 | | Debug.LogWarning("AudioSource or model is null!."); |
| 0 | 143 | | return; |
| | 144 | | } |
| | 145 | |
|
| 17 | 146 | | UpdateAudioSourceVolume(); |
| 17 | 147 | | audioSource.loop = model.Loop; |
| 17 | 148 | | audioSource.pitch = model.GetPitch(); |
| 17 | 149 | | audioSource.spatialBlend = 1; |
| 17 | 150 | | audioSource.dopplerLevel = 0.1f; |
| | 151 | |
|
| 17 | 152 | | if (model.Playing != audioSource.isPlaying) |
| | 153 | | { |
| 10 | 154 | | if (audioSource.isPlaying) |
| 4 | 155 | | audioSource.Stop(); |
| 6 | 156 | | else if (isAudioClipReady) |
| 3 | 157 | | audioSource.Play(); |
| | 158 | | } |
| 7 | 159 | | else if (audioSource.isPlaying) |
| | 160 | | { |
| 3 | 161 | | audioSource.Stop(); |
| | 162 | | } |
| 10 | 163 | | } |
| | 164 | |
|
| | 165 | | private void ApplyLoadedAudioClip(AudioClip clip) |
| | 166 | | { |
| 3 | 167 | | isAudioClipReady = true; |
| 3 | 168 | | if (audioSource.clip != clip) |
| 3 | 169 | | audioSource.clip = clip; |
| | 170 | |
|
| 3 | 171 | | ApplyCurrentModel(); |
| 3 | 172 | | } |
| | 173 | |
|
| | 174 | | private void OnAudioClipLoadComplete(Asset_AudioClip assetAudioClip) |
| | 175 | | { |
| 3 | 176 | | if (assetAudioClip.audioClip == null) |
| 0 | 177 | | return; |
| | 178 | |
|
| 3 | 179 | | audioClip = assetAudioClip.audioClip; |
| | 180 | |
|
| 3 | 181 | | dataStore.sceneWorldObjects.AddAudioClip(scene.sceneData.sceneNumber, audioClip); |
| | 182 | |
|
| 3 | 183 | | ApplyLoadedAudioClip(assetAudioClip.audioClip); |
| 3 | 184 | | } |
| | 185 | |
|
| | 186 | | private void OnAudioClipLoadFail(Asset_AudioClip assetAudioClip, Exception exception) |
| | 187 | | { |
| 0 | 188 | | Debug.LogError("Audio clip couldn't be loaded. Url: " +model.AudioClipUrl + " error: " + exception.Messa |
| 0 | 189 | | DisposePromise(); |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | private void OnAudioSettingsChanged(AudioSettings settings) |
| | 193 | | { |
| 0 | 194 | | UpdateAudioSourceVolume(); |
| 0 | 195 | | } |
| | 196 | |
|
| | 197 | | private void OnVirtualAudioMixerChangedValue(float currentValue, float previousValue) |
| | 198 | | { |
| 0 | 199 | | UpdateAudioSourceVolume(); |
| 0 | 200 | | } |
| | 201 | |
|
| | 202 | | private void UpdateAudioSourceVolume() |
| | 203 | | { |
| 30 | 204 | | if (model == null) return; |
| | 205 | |
|
| 22 | 206 | | if (!scene.isPersistent && (!isPlayerInsideScene || !isEntityInsideScene)) |
| | 207 | | { |
| 4 | 208 | | audioSource.volume = 0; |
| 4 | 209 | | return; |
| | 210 | | } |
| | 211 | |
|
| 18 | 212 | | AudioSettings audioSettingsData = |
| | 213 | | settings != null ? settings.audioSettings.Data : new AudioSettings(); |
| 18 | 214 | | float newVolume = model.GetVolume() * Utils.ToVolumeCurve( |
| | 215 | | dataStore.virtualAudioMixer.sceneSFXVolume.Get() * audioSettingsData.sceneSFXVolume * |
| | 216 | | audioSettingsData.masterVolume); |
| | 217 | |
|
| 18 | 218 | | audioSource.volume = newVolume; |
| 18 | 219 | | } |
| | 220 | |
|
| | 221 | | private void OnCurrentSceneChanged(int currentSceneNumber, int previousSceneNumber) |
| | 222 | | { |
| 9 | 223 | | isPlayerInsideScene = scene.isPersistent || scene.sceneData.sceneNumber == currentSceneNumber; |
| 9 | 224 | | UpdateAudioSourceVolume(); |
| 9 | 225 | | } |
| | 226 | |
|
| | 227 | | public void OnSceneBoundsStateChange(bool isInsideSceneBounds) |
| | 228 | | { |
| 0 | 229 | | if (isEntityInsideScene == isInsideSceneBounds) return; |
| | 230 | |
|
| 0 | 231 | | isEntityInsideScene = isInsideSceneBounds; |
| 0 | 232 | | UpdateAudioSourceVolume(); |
| 0 | 233 | | } |
| | 234 | | } |
| | 235 | | } |