| | 1 | | using DCL.Controllers; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Builder; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | public class BuilderInWorldAudioHandler : MonoBehaviour |
| | 9 | | { |
| | 10 | | const float MUSIC_DELAY_TIME_ON_START = 4f; |
| | 11 | | const float MUSIC_FADE_OUT_TIME_ON_EXIT = 5f; |
| | 12 | | const float MUSIC_FADE_OUT_TIME_ON_TUTORIAL = 3f; |
| | 13 | |
|
| | 14 | | private IBIWCreatorController creatorController; |
| | 15 | |
|
| | 16 | | private IBIWEntityHandler entityHandler; |
| | 17 | |
|
| | 18 | | private IBIWModeController modeController; |
| | 19 | |
|
| | 20 | | [Header("Audio Events")] |
| | 21 | | [SerializeField] |
| | 22 | | AudioEvent eventAssetSpawn; |
| | 23 | |
|
| | 24 | | [SerializeField] |
| | 25 | | AudioEvent eventAssetPlace; |
| | 26 | |
|
| | 27 | | [SerializeField] |
| | 28 | | AudioEvent eventAssetSelect; |
| | 29 | |
|
| | 30 | | [SerializeField] |
| | 31 | | AudioEvent eventAssetDeselect; |
| | 32 | |
|
| | 33 | | [SerializeField] |
| | 34 | | AudioEvent eventBuilderOutOfBounds; |
| | 35 | |
|
| | 36 | | [SerializeField] |
| | 37 | | AudioEvent eventBuilderOutOfBoundsPlaced; |
| | 38 | |
|
| | 39 | | [SerializeField] |
| | 40 | | AudioEvent eventAssetDelete; |
| | 41 | |
|
| | 42 | | [SerializeField] |
| | 43 | | AudioEvent eventBuilderExit; |
| | 44 | |
|
| | 45 | | [SerializeField] |
| | 46 | | AudioEvent eventBuilderMusic; |
| | 47 | |
|
| 11 | 48 | | private List<string> entitiesOutOfBounds = new List<string>(); |
| | 49 | | private int entityCount; |
| | 50 | | bool playPlacementSoundOnDeselect; |
| | 51 | | private IBIWModeController.EditModeState state = IBIWModeController.EditModeState.Inactive; |
| | 52 | |
|
| | 53 | | private Coroutine fadeOutCoroutine; |
| | 54 | | private Coroutine startBuilderMusicCoroutine; |
| | 55 | |
|
| | 56 | | private IContext context; |
| | 57 | |
|
| 10 | 58 | | private void Start() { playPlacementSoundOnDeselect = false; } |
| | 59 | |
|
| | 60 | | public void Initialize(IContext context) |
| | 61 | | { |
| 10 | 62 | | this.context = context; |
| 10 | 63 | | creatorController = context.editorContext.creatorController; |
| 10 | 64 | | entityHandler = context.editorContext.entityHandler; |
| 10 | 65 | | modeController = context.editorContext.modeController; |
| | 66 | |
|
| 10 | 67 | | AddListeners(); |
| 10 | 68 | | } |
| | 69 | |
|
| | 70 | | private void AddListeners() |
| | 71 | | { |
| 10 | 72 | | creatorController.OnCatalogItemPlaced += OnAssetSpawn; |
| 10 | 73 | | entityHandler.OnDeleteSelectedEntities += OnAssetDelete; |
| 10 | 74 | | modeController.OnChangedEditModeState += OnChangedEditModeState; |
| 10 | 75 | | DCL.Environment.i.world.sceneBoundsChecker.OnEntityBoundsCheckerStatusChanged += OnEntityBoundsCheckerStatusChan |
| | 76 | |
|
| 10 | 77 | | if (DCL.Tutorial.TutorialController.i != null) |
| | 78 | | { |
| 0 | 79 | | DCL.Tutorial.TutorialController.i.OnTutorialEnabled += OnTutorialEnabled; |
| 0 | 80 | | DCL.Tutorial.TutorialController.i.OnTutorialDisabled += OnTutorialDisabled; |
| | 81 | | } |
| | 82 | |
|
| 10 | 83 | | entityHandler.OnEntityDeselected += OnAssetDeselect; |
| 10 | 84 | | entityHandler.OnEntitySelected += OnAssetSelect; |
| 10 | 85 | | } |
| | 86 | |
|
| | 87 | | public void EnterEditMode(IParcelScene scene) |
| | 88 | | { |
| 7 | 89 | | UpdateEntityCount(); |
| | 90 | |
|
| 7 | 91 | | if (eventBuilderMusic.source.gameObject.activeSelf) |
| | 92 | | { |
| 6 | 93 | | if (startBuilderMusicCoroutine != null) |
| 0 | 94 | | CoroutineStarter.Stop(startBuilderMusicCoroutine); |
| | 95 | |
|
| 6 | 96 | | startBuilderMusicCoroutine = CoroutineStarter.Start(StartBuilderMusic()); |
| | 97 | | } |
| | 98 | |
|
| 7 | 99 | | if ( context.editorContext.editorHUD != null) |
| 7 | 100 | | context.editorContext.editorHUD.OnCatalogItemSelected += OnCatalogItemSelected; |
| | 101 | |
|
| 7 | 102 | | gameObject.SetActive(true); |
| 7 | 103 | | } |
| | 104 | |
|
| | 105 | | public void ExitEditMode() |
| | 106 | | { |
| 2 | 107 | | eventBuilderExit.Play(); |
| 2 | 108 | | if (eventBuilderMusic.source.gameObject.activeSelf) |
| | 109 | | { |
| 2 | 110 | | if (fadeOutCoroutine != null) |
| 0 | 111 | | CoroutineStarter.Stop(fadeOutCoroutine); |
| 2 | 112 | | fadeOutCoroutine = CoroutineStarter.Start(eventBuilderMusic.FadeOut(MUSIC_FADE_OUT_TIME_ON_EXIT)); |
| | 113 | | } |
| | 114 | |
|
| 2 | 115 | | if ( context.editorContext.editorHUD != null) |
| 2 | 116 | | context.editorContext.editorHUD.OnCatalogItemSelected -= OnCatalogItemSelected; |
| | 117 | |
|
| 2 | 118 | | gameObject.SetActive(false); |
| 2 | 119 | | } |
| | 120 | |
|
| 0 | 121 | | private void OnAssetSpawn() { eventAssetSpawn.Play(); } |
| | 122 | |
|
| | 123 | | private void OnAssetDelete(List<BIWEntity> entities) |
| | 124 | | { |
| 0 | 125 | | foreach (BIWEntity deletedEntity in entities) |
| | 126 | | { |
| 0 | 127 | | if (entitiesOutOfBounds.Contains(deletedEntity.rootEntity.entityId)) |
| | 128 | | { |
| 0 | 129 | | entitiesOutOfBounds.Remove(deletedEntity.rootEntity.entityId); |
| | 130 | | } |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | eventAssetDelete.Play(); |
| 0 | 134 | | } |
| | 135 | |
|
| 0 | 136 | | private void OnAssetSelect() { eventAssetSelect.Play(); } |
| | 137 | |
|
| | 138 | | private void OnAssetDeselect(BIWEntity entity) |
| | 139 | | { |
| 0 | 140 | | if (playPlacementSoundOnDeselect) |
| | 141 | | { |
| 0 | 142 | | eventAssetPlace.Play(); |
| 0 | 143 | | playPlacementSoundOnDeselect = false; |
| 0 | 144 | | } |
| | 145 | | else |
| 0 | 146 | | eventAssetDeselect.Play(); |
| | 147 | |
|
| 0 | 148 | | UpdateEntityCount(); |
| | 149 | |
|
| 0 | 150 | | if (entitiesOutOfBounds.Contains(entity.rootEntity.entityId)) |
| | 151 | | { |
| 0 | 152 | | eventBuilderOutOfBoundsPlaced.Play(); |
| | 153 | | } |
| 0 | 154 | | } |
| | 155 | |
|
| 0 | 156 | | private void OnCatalogItemSelected(CatalogItem catalogItem) { playPlacementSoundOnDeselect = true; } |
| | 157 | |
|
| | 158 | | private void OnTutorialEnabled() |
| | 159 | | { |
| 0 | 160 | | if (gameObject.activeInHierarchy) |
| | 161 | | { |
| 0 | 162 | | if (fadeOutCoroutine != null) |
| 0 | 163 | | CoroutineStarter.Stop(fadeOutCoroutine); |
| 0 | 164 | | fadeOutCoroutine = CoroutineStarter.Start(eventBuilderMusic.FadeOut(MUSIC_FADE_OUT_TIME_ON_TUTORIAL)); |
| | 165 | | } |
| 0 | 166 | | } |
| | 167 | |
|
| | 168 | | private void OnTutorialDisabled() |
| | 169 | | { |
| 0 | 170 | | if (gameObject.activeInHierarchy) |
| | 171 | | { |
| 0 | 172 | | if (startBuilderMusicCoroutine != null) |
| 0 | 173 | | CoroutineStarter.Stop(startBuilderMusicCoroutine); |
| 0 | 174 | | startBuilderMusicCoroutine = CoroutineStarter.Start(StartBuilderMusic()); |
| | 175 | | } |
| 0 | 176 | | } |
| | 177 | |
|
| | 178 | | private IEnumerator StartBuilderMusic() |
| | 179 | | { |
| 6 | 180 | | yield return new WaitForSeconds(MUSIC_DELAY_TIME_ON_START); |
| | 181 | |
|
| 0 | 182 | | if (gameObject != null && gameObject.activeInHierarchy) |
| 0 | 183 | | eventBuilderMusic.Play(); |
| 0 | 184 | | } |
| | 185 | |
|
| | 186 | | private void OnChangedEditModeState(IBIWModeController.EditModeState previous, IBIWModeController.EditModeState curr |
| | 187 | | { |
| 0 | 188 | | state = current; |
| 0 | 189 | | if (previous != IBIWModeController.EditModeState.Inactive) |
| | 190 | | { |
| | 191 | | switch (current) |
| | 192 | | { |
| | 193 | | case IBIWModeController.EditModeState.FirstPerson: |
| 0 | 194 | | AudioScriptableObjects.cameraFadeIn.Play(); |
| 0 | 195 | | break; |
| | 196 | | case IBIWModeController.EditModeState.GodMode: |
| 0 | 197 | | AudioScriptableObjects.cameraFadeOut.Play(); |
| | 198 | | break; |
| | 199 | | default: |
| | 200 | | break; |
| | 201 | | } |
| | 202 | | } |
| 0 | 203 | | } |
| | 204 | |
|
| | 205 | | private void OnEntityBoundsCheckerStatusChanged(DCL.Models.IDCLEntity entity, bool isInsideBoundaries) |
| | 206 | | { |
| 0 | 207 | | if (state == IBIWModeController.EditModeState.Inactive) |
| 0 | 208 | | return; |
| | 209 | |
|
| 0 | 210 | | if (!isInsideBoundaries) |
| | 211 | | { |
| 0 | 212 | | if (!entitiesOutOfBounds.Contains(entity.entityId)) |
| | 213 | | { |
| 0 | 214 | | entitiesOutOfBounds.Add(entity.entityId); |
| 0 | 215 | | eventBuilderOutOfBounds.Play(); |
| | 216 | | } |
| 0 | 217 | | } |
| | 218 | | else |
| | 219 | | { |
| 0 | 220 | | if (entitiesOutOfBounds.Contains(entity.entityId)) |
| | 221 | | { |
| 0 | 222 | | entitiesOutOfBounds.Remove(entity.entityId); |
| | 223 | | } |
| | 224 | | } |
| 0 | 225 | | } |
| | 226 | |
|
| 14 | 227 | | private void UpdateEntityCount() { entityCount = entityHandler.GetCurrentSceneEntityCount(); } |
| | 228 | |
|
| 0 | 229 | | private bool EntityHasBeenAddedSinceLastUpdate() { return (entityHandler.GetCurrentSceneEntityCount() > entityCount) |
| | 230 | |
|
| | 231 | | private void OnDestroy() |
| | 232 | | { |
| | 233 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 10 | 234 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 235 | | return; |
| | 236 | | #endif |
| | 237 | |
|
| 10 | 238 | | Dispose(); |
| 10 | 239 | | } |
| | 240 | |
|
| | 241 | | public void Dispose() |
| | 242 | | { |
| 20 | 243 | | if (startBuilderMusicCoroutine != null) |
| 6 | 244 | | CoroutineStarter.Stop(startBuilderMusicCoroutine); |
| | 245 | |
|
| 20 | 246 | | if (fadeOutCoroutine != null) |
| 2 | 247 | | CoroutineStarter.Stop(fadeOutCoroutine); |
| | 248 | |
|
| 20 | 249 | | startBuilderMusicCoroutine = null; |
| 20 | 250 | | fadeOutCoroutine = null; |
| | 251 | |
|
| 20 | 252 | | RemoveListeners(); |
| 20 | 253 | | } |
| | 254 | |
|
| | 255 | | private void RemoveListeners() |
| | 256 | | { |
| 20 | 257 | | creatorController.OnCatalogItemPlaced -= OnAssetSpawn; |
| 20 | 258 | | entityHandler.OnDeleteSelectedEntities -= OnAssetDelete; |
| 20 | 259 | | modeController.OnChangedEditModeState -= OnChangedEditModeState; |
| | 260 | |
|
| 20 | 261 | | DCL.Environment.i.world.sceneBoundsChecker.OnEntityBoundsCheckerStatusChanged -= OnEntityBoundsCheckerStatusChan |
| | 262 | |
|
| 20 | 263 | | if (DCL.Tutorial.TutorialController.i != null) |
| | 264 | | { |
| 0 | 265 | | DCL.Tutorial.TutorialController.i.OnTutorialEnabled -= OnTutorialEnabled; |
| 0 | 266 | | DCL.Tutorial.TutorialController.i.OnTutorialDisabled -= OnTutorialDisabled; |
| | 267 | | } |
| | 268 | |
|
| 20 | 269 | | entityHandler.OnEntityDeselected -= OnAssetDeselect; |
| 20 | 270 | | entityHandler.OnEntitySelected -= OnAssetSelect; |
| 20 | 271 | | } |
| | 272 | | } |