| | 1 | | using DCL.Controllers; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public class BuilderInWorldAudioHandler : BIWController |
| | 7 | | { |
| | 8 | | const float MUSIC_DELAY_TIME_ON_START = 4f; |
| | 9 | | const float MUSIC_FADE_OUT_TIME_ON_EXIT = 5f; |
| | 10 | | const float MUSIC_FADE_OUT_TIME_ON_TUTORIAL = 3f; |
| | 11 | |
|
| | 12 | | [SerializeField] |
| | 13 | | BIWCreatorController creatorController; |
| | 14 | |
|
| | 15 | | [SerializeField] |
| | 16 | | GameObject builderInWorldModesParent; |
| | 17 | |
|
| | 18 | | [SerializeField] |
| | 19 | | BuilderInWorldController inWorldController; |
| | 20 | |
|
| | 21 | | [SerializeField] |
| | 22 | | BuilderInWorldEntityHandler entityHandler; |
| | 23 | |
|
| | 24 | | [SerializeField] |
| | 25 | | BIWModeController modeController; |
| | 26 | |
|
| | 27 | | [Header("Audio Events")] |
| | 28 | | [SerializeField] |
| | 29 | | AudioEvent eventAssetSpawn; |
| | 30 | |
|
| | 31 | | [SerializeField] |
| | 32 | | AudioEvent eventAssetPlace; |
| | 33 | |
|
| | 34 | | [SerializeField] |
| | 35 | | AudioEvent eventAssetSelect; |
| | 36 | |
|
| | 37 | | [SerializeField] |
| | 38 | | AudioEvent eventAssetDeselect; |
| | 39 | |
|
| | 40 | | [SerializeField] |
| | 41 | | AudioEvent eventBuilderOutOfBounds; |
| | 42 | |
|
| | 43 | | [SerializeField] |
| | 44 | | AudioEvent eventBuilderOutOfBoundsPlaced; |
| | 45 | |
|
| | 46 | | [SerializeField] |
| | 47 | | AudioEvent eventAssetDelete; |
| | 48 | |
|
| | 49 | | [SerializeField] |
| | 50 | | AudioEvent eventBuilderExit; |
| | 51 | |
|
| | 52 | | [SerializeField] |
| | 53 | | AudioEvent eventBuilderMusic; |
| | 54 | |
|
| 297 | 55 | | private List<string> entitiesOutOfBounds = new List<string>(); |
| | 56 | | private int entityCount; |
| | 57 | | bool playPlacementSoundOnDeselect; |
| | 58 | | private BIWModeController.EditModeState state = BIWModeController.EditModeState.Inactive; |
| | 59 | |
|
| | 60 | | private void Start() |
| | 61 | | { |
| 0 | 62 | | playPlacementSoundOnDeselect = false; |
| | 63 | |
|
| 0 | 64 | | AddListeners(); |
| 0 | 65 | | } |
| | 66 | |
|
| 0 | 67 | | private void OnDestroy() { RemoveListeners(); } |
| | 68 | |
|
| | 69 | | private void AddListeners() |
| | 70 | | { |
| 0 | 71 | | creatorController.OnCatalogItemPlaced += OnAssetSpawn; |
| 0 | 72 | | entityHandler.OnDeleteSelectedEntities += OnAssetDelete; |
| 0 | 73 | | modeController.OnChangedEditModeState += OnChangedEditModeState; |
| 0 | 74 | | DCL.Environment.i.world.sceneBoundsChecker.OnEntityBoundsCheckerStatusChanged += OnEntityBoundsCheckerStatusChan |
| | 75 | |
|
| 0 | 76 | | DCL.Tutorial.TutorialController.i.OnTutorialEnabled += OnTutorialEnabled; |
| 0 | 77 | | DCL.Tutorial.TutorialController.i.OnTutorialDisabled += OnTutorialDisabled; |
| | 78 | |
|
| 0 | 79 | | entityHandler.OnEntityDeselected += OnAssetDeselect; |
| 0 | 80 | | entityHandler.OnEntitySelected += OnAssetSelect; |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | private void RemoveListeners() |
| | 84 | | { |
| 0 | 85 | | creatorController.OnCatalogItemPlaced -= OnAssetSpawn; |
| 0 | 86 | | entityHandler.OnDeleteSelectedEntities -= OnAssetDelete; |
| 0 | 87 | | modeController.OnChangedEditModeState -= OnChangedEditModeState; |
| 0 | 88 | | DCL.Environment.i.world.sceneBoundsChecker.OnEntityBoundsCheckerStatusChanged -= OnEntityBoundsCheckerStatusChan |
| | 89 | |
|
| 0 | 90 | | DCL.Tutorial.TutorialController.i.OnTutorialEnabled -= OnTutorialEnabled; |
| 0 | 91 | | DCL.Tutorial.TutorialController.i.OnTutorialDisabled -= OnTutorialDisabled; |
| | 92 | |
|
| 0 | 93 | | entityHandler.OnEntityDeselected -= OnAssetDeselect; |
| 0 | 94 | | entityHandler.OnEntitySelected -= OnAssetSelect; |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public override void EnterEditMode(ParcelScene scene) |
| | 98 | | { |
| 0 | 99 | | base.EnterEditMode(scene); |
| | 100 | |
|
| 0 | 101 | | UpdateEntityCount(); |
| 0 | 102 | | CoroutineStarter.Start(StartBuilderMusic()); |
| 0 | 103 | | if (HUDController.i.builderInWorldMainHud != null) |
| 0 | 104 | | HUDController.i.builderInWorldMainHud.OnCatalogItemSelected += OnCatalogItemSelected; |
| | 105 | |
|
| 0 | 106 | | gameObject.SetActive(true); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | public override void ExitEditMode() |
| | 110 | | { |
| 0 | 111 | | base.ExitEditMode(); |
| | 112 | |
|
| 0 | 113 | | eventBuilderExit.Play(); |
| 0 | 114 | | CoroutineStarter.Start(eventBuilderMusic.FadeOut(MUSIC_FADE_OUT_TIME_ON_EXIT)); |
| 0 | 115 | | if (HUDController.i.builderInWorldMainHud != null) |
| 0 | 116 | | HUDController.i.builderInWorldMainHud.OnCatalogItemSelected -= OnCatalogItemSelected; |
| | 117 | |
|
| 0 | 118 | | gameObject.SetActive(false); |
| 0 | 119 | | } |
| | 120 | |
|
| 0 | 121 | | private void OnAssetSpawn() { eventAssetSpawn.Play(); } |
| | 122 | |
|
| | 123 | | private void OnAssetDelete(List<DCLBuilderInWorldEntity> entities) |
| | 124 | | { |
| 0 | 125 | | foreach (DCLBuilderInWorldEntity 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(DCLBuilderInWorldEntity 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 (inWorldController.isBuilderInWorldActivated) |
| 0 | 161 | | CoroutineStarter.Start(eventBuilderMusic.FadeOut(MUSIC_FADE_OUT_TIME_ON_TUTORIAL)); |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | private void OnTutorialDisabled() |
| | 165 | | { |
| 0 | 166 | | if (inWorldController.isBuilderInWorldActivated) |
| 0 | 167 | | CoroutineStarter.Start(StartBuilderMusic()); |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | private IEnumerator StartBuilderMusic() |
| | 171 | | { |
| 0 | 172 | | yield return new WaitForSeconds(MUSIC_DELAY_TIME_ON_START); |
| | 173 | |
|
| 0 | 174 | | if (inWorldController.isBuilderInWorldActivated) |
| 0 | 175 | | eventBuilderMusic.Play(); |
| 0 | 176 | | } |
| | 177 | |
|
| | 178 | | private void OnChangedEditModeState(BIWModeController.EditModeState previous, BIWModeController.EditModeState curren |
| | 179 | | { |
| 0 | 180 | | state = current; |
| 0 | 181 | | if (previous != BIWModeController.EditModeState.Inactive) |
| | 182 | | { |
| | 183 | | switch (current) |
| | 184 | | { |
| | 185 | | case BIWModeController.EditModeState.FirstPerson: |
| 0 | 186 | | AudioScriptableObjects.cameraFadeIn.Play(); |
| 0 | 187 | | break; |
| | 188 | | case BIWModeController.EditModeState.GodMode: |
| 0 | 189 | | AudioScriptableObjects.cameraFadeOut.Play(); |
| | 190 | | break; |
| | 191 | | default: |
| | 192 | | break; |
| | 193 | | } |
| | 194 | | } |
| 0 | 195 | | } |
| | 196 | |
|
| | 197 | | private void OnEntityBoundsCheckerStatusChanged(DCL.Models.IDCLEntity entity, bool isInsideBoundaries) |
| | 198 | | { |
| 0 | 199 | | if (state == BIWModeController.EditModeState.Inactive) |
| 0 | 200 | | return; |
| | 201 | |
|
| 0 | 202 | | if (!isInsideBoundaries) |
| | 203 | | { |
| 0 | 204 | | if (!entitiesOutOfBounds.Contains(entity.entityId)) |
| | 205 | | { |
| 0 | 206 | | entitiesOutOfBounds.Add(entity.entityId); |
| 0 | 207 | | eventBuilderOutOfBounds.Play(); |
| | 208 | | } |
| 0 | 209 | | } |
| | 210 | | else |
| | 211 | | { |
| 0 | 212 | | if (entitiesOutOfBounds.Contains(entity.entityId)) |
| | 213 | | { |
| 0 | 214 | | entitiesOutOfBounds.Remove(entity.entityId); |
| | 215 | | } |
| | 216 | | } |
| 0 | 217 | | } |
| | 218 | |
|
| 0 | 219 | | private void UpdateEntityCount() { entityCount = entityHandler.GetCurrentSceneEntityCount(); } |
| | 220 | |
|
| 0 | 221 | | private bool EntityHasBeenAddedSinceLastUpdate() { return (entityHandler.GetCurrentSceneEntityCount() > entityCount) |
| | 222 | | } |