< Summary

Class:BuilderInWorldAudioHandler
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/BuilderInWorldAudioHandler.cs
Covered lines:53
Uncovered lines:54
Coverable lines:107
Total lines:272
Line coverage:49.5% (53 of 107)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuilderInWorldAudioHandler()0%110100%
Start()0%110100%
Initialize(...)0%110100%
AddListeners()0%2.032080%
EnterEditMode(...)0%4.024088.89%
ExitEditMode()0%4.024088.89%
OnAssetSpawn()0%2100%
OnAssetDelete(...)0%12300%
OnAssetSelect()0%2100%
OnAssetDeselect(...)0%12300%
OnCatalogItemSelected(...)0%2100%
OnTutorialEnabled()0%12300%
OnTutorialDisabled()0%12300%
StartBuilderMusic()0%15.555025%
OnChangedEditModeState(...)0%20400%
OnEntityBoundsCheckerStatusChanged(...)0%30500%
UpdateEntityCount()0%110100%
EntityHasBeenAddedSinceLastUpdate()0%2100%
OnDestroy()0%2.062075%
Dispose()0%330100%
RemoveListeners()0%2.032080%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/BuilderInWorldAudioHandler.cs

#LineLine coverage
 1using DCL.Controllers;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL;
 5using DCL.Builder;
 6using UnityEngine;
 7
 8public 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
 1148    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
 1058    private void Start() { playPlacementSoundOnDeselect = false; }
 59
 60    public void Initialize(IContext context)
 61    {
 1062        this.context = context;
 1063        creatorController = context.editorContext.creatorController;
 1064        entityHandler = context.editorContext.entityHandler;
 1065        modeController = context.editorContext.modeController;
 66
 1067        AddListeners();
 1068    }
 69
 70    private void AddListeners()
 71    {
 1072        creatorController.OnCatalogItemPlaced += OnAssetSpawn;
 1073        entityHandler.OnDeleteSelectedEntities += OnAssetDelete;
 1074        modeController.OnChangedEditModeState += OnChangedEditModeState;
 1075        DCL.Environment.i.world.sceneBoundsChecker.OnEntityBoundsCheckerStatusChanged += OnEntityBoundsCheckerStatusChan
 76
 1077        if (DCL.Tutorial.TutorialController.i != null)
 78        {
 079            DCL.Tutorial.TutorialController.i.OnTutorialEnabled += OnTutorialEnabled;
 080            DCL.Tutorial.TutorialController.i.OnTutorialDisabled += OnTutorialDisabled;
 81        }
 82
 1083        entityHandler.OnEntityDeselected += OnAssetDeselect;
 1084        entityHandler.OnEntitySelected += OnAssetSelect;
 1085    }
 86
 87    public void EnterEditMode(IParcelScene scene)
 88    {
 789        UpdateEntityCount();
 90
 791        if (eventBuilderMusic.source.gameObject.activeSelf)
 92        {
 693            if (startBuilderMusicCoroutine != null)
 094                CoroutineStarter.Stop(startBuilderMusicCoroutine);
 95
 696            startBuilderMusicCoroutine = CoroutineStarter.Start(StartBuilderMusic());
 97        }
 98
 799        if ( context.editorContext.editorHUD != null)
 7100            context.editorContext.editorHUD.OnCatalogItemSelected += OnCatalogItemSelected;
 101
 7102        gameObject.SetActive(true);
 7103    }
 104
 105    public void ExitEditMode()
 106    {
 2107        eventBuilderExit.Play();
 2108        if (eventBuilderMusic.source.gameObject.activeSelf)
 109        {
 2110            if (fadeOutCoroutine != null)
 0111                CoroutineStarter.Stop(fadeOutCoroutine);
 2112            fadeOutCoroutine = CoroutineStarter.Start(eventBuilderMusic.FadeOut(MUSIC_FADE_OUT_TIME_ON_EXIT));
 113        }
 114
 2115        if ( context.editorContext.editorHUD != null)
 2116            context.editorContext.editorHUD.OnCatalogItemSelected -= OnCatalogItemSelected;
 117
 2118        gameObject.SetActive(false);
 2119    }
 120
 0121    private void OnAssetSpawn() { eventAssetSpawn.Play(); }
 122
 123    private void OnAssetDelete(List<BIWEntity> entities)
 124    {
 0125        foreach (BIWEntity deletedEntity in entities)
 126        {
 0127            if (entitiesOutOfBounds.Contains(deletedEntity.rootEntity.entityId))
 128            {
 0129                entitiesOutOfBounds.Remove(deletedEntity.rootEntity.entityId);
 130            }
 131        }
 132
 0133        eventAssetDelete.Play();
 0134    }
 135
 0136    private void OnAssetSelect() { eventAssetSelect.Play(); }
 137
 138    private void OnAssetDeselect(BIWEntity entity)
 139    {
 0140        if (playPlacementSoundOnDeselect)
 141        {
 0142            eventAssetPlace.Play();
 0143            playPlacementSoundOnDeselect = false;
 0144        }
 145        else
 0146            eventAssetDeselect.Play();
 147
 0148        UpdateEntityCount();
 149
 0150        if (entitiesOutOfBounds.Contains(entity.rootEntity.entityId))
 151        {
 0152            eventBuilderOutOfBoundsPlaced.Play();
 153        }
 0154    }
 155
 0156    private void OnCatalogItemSelected(CatalogItem catalogItem) { playPlacementSoundOnDeselect = true; }
 157
 158    private void OnTutorialEnabled()
 159    {
 0160        if (gameObject.activeInHierarchy)
 161        {
 0162            if (fadeOutCoroutine != null)
 0163                CoroutineStarter.Stop(fadeOutCoroutine);
 0164            fadeOutCoroutine =  CoroutineStarter.Start(eventBuilderMusic.FadeOut(MUSIC_FADE_OUT_TIME_ON_TUTORIAL));
 165        }
 0166    }
 167
 168    private void OnTutorialDisabled()
 169    {
 0170        if (gameObject.activeInHierarchy)
 171        {
 0172            if (startBuilderMusicCoroutine != null)
 0173                CoroutineStarter.Stop(startBuilderMusicCoroutine);
 0174            startBuilderMusicCoroutine = CoroutineStarter.Start(StartBuilderMusic());
 175        }
 0176    }
 177
 178    private IEnumerator StartBuilderMusic()
 179    {
 6180        yield return new WaitForSeconds(MUSIC_DELAY_TIME_ON_START);
 181
 0182        if (gameObject != null && gameObject.activeInHierarchy)
 0183            eventBuilderMusic.Play();
 0184    }
 185
 186    private void OnChangedEditModeState(IBIWModeController.EditModeState previous, IBIWModeController.EditModeState curr
 187    {
 0188        state = current;
 0189        if (previous != IBIWModeController.EditModeState.Inactive)
 190        {
 191            switch (current)
 192            {
 193                case IBIWModeController.EditModeState.FirstPerson:
 0194                    AudioScriptableObjects.cameraFadeIn.Play();
 0195                    break;
 196                case IBIWModeController.EditModeState.GodMode:
 0197                    AudioScriptableObjects.cameraFadeOut.Play();
 198                    break;
 199                default:
 200                    break;
 201            }
 202        }
 0203    }
 204
 205    private void OnEntityBoundsCheckerStatusChanged(DCL.Models.IDCLEntity entity, bool isInsideBoundaries)
 206    {
 0207        if (state == IBIWModeController.EditModeState.Inactive)
 0208            return;
 209
 0210        if (!isInsideBoundaries)
 211        {
 0212            if (!entitiesOutOfBounds.Contains(entity.entityId))
 213            {
 0214                entitiesOutOfBounds.Add(entity.entityId);
 0215                eventBuilderOutOfBounds.Play();
 216            }
 0217        }
 218        else
 219        {
 0220            if (entitiesOutOfBounds.Contains(entity.entityId))
 221            {
 0222                entitiesOutOfBounds.Remove(entity.entityId);
 223            }
 224        }
 0225    }
 226
 14227    private void UpdateEntityCount() { entityCount = entityHandler.GetCurrentSceneEntityCount(); }
 228
 0229    private bool EntityHasBeenAddedSinceLastUpdate() { return (entityHandler.GetCurrentSceneEntityCount() > entityCount)
 230
 231    private void OnDestroy()
 232    {
 233#if UNITY_STANDALONE || UNITY_EDITOR
 10234        if (DataStore.i.common.isApplicationQuitting.Get())
 0235            return;
 236#endif
 237
 10238        Dispose();
 10239    }
 240
 241    public void Dispose()
 242    {
 20243        if (startBuilderMusicCoroutine != null)
 6244            CoroutineStarter.Stop(startBuilderMusicCoroutine);
 245
 20246        if (fadeOutCoroutine != null)
 2247            CoroutineStarter.Stop(fadeOutCoroutine);
 248
 20249        startBuilderMusicCoroutine = null;
 20250        fadeOutCoroutine = null;
 251
 20252        RemoveListeners();
 20253    }
 254
 255    private void RemoveListeners()
 256    {
 20257        creatorController.OnCatalogItemPlaced -= OnAssetSpawn;
 20258        entityHandler.OnDeleteSelectedEntities -= OnAssetDelete;
 20259        modeController.OnChangedEditModeState -= OnChangedEditModeState;
 260
 20261        DCL.Environment.i.world.sceneBoundsChecker.OnEntityBoundsCheckerStatusChanged -= OnEntityBoundsCheckerStatusChan
 262
 20263        if (DCL.Tutorial.TutorialController.i != null)
 264        {
 0265            DCL.Tutorial.TutorialController.i.OnTutorialEnabled -= OnTutorialEnabled;
 0266            DCL.Tutorial.TutorialController.i.OnTutorialDisabled -= OnTutorialDisabled;
 267        }
 268
 20269        entityHandler.OnEntityDeselected -= OnAssetDeselect;
 20270        entityHandler.OnEntitySelected -= OnAssetSelect;
 20271    }
 272}