< Summary

Class:BuilderInWorldAudioHandler
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/BuilderInWorldAudioHandler.cs
Covered lines:53
Uncovered lines:45
Coverable lines:98
Total lines:246
Line coverage:54% (53 of 98)
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%330100%
ExitEditMode()0%330100%
OnAssetSpawn()0%2100%
OnAssetDelete(...)0%12300%
OnAssetSelect()0%2100%
OnAssetDeselect(...)0%12300%
OnCatalogItemSelected(...)0%2100%
OnTutorialEnabled()0%6200%
OnTutorialDisabled()0%6200%
StartBuilderMusic()0%15.555025%
OnChangedEditModeState(...)0%64050%
OnEntityBoundsCheckerStatusChanged(...)0%30500%
UpdateEntityCount()0%110100%
EntityHasBeenAddedSinceLastUpdate()0%2100%
OnDestroy()0%110100%
Dispose()0%4.014090.91%
RemoveListeners()0%2.032080%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/BuilderInWorldAudioHandler.cs

#LineLine coverage
 1using DCL.Controllers;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL;
 5using UnityEngine;
 6
 7public class BuilderInWorldAudioHandler : MonoBehaviour
 8{
 9    const float MUSIC_DELAY_TIME_ON_START = 4f;
 10    const float MUSIC_FADE_OUT_TIME_ON_EXIT = 5f;
 11    const float MUSIC_FADE_OUT_TIME_ON_TUTORIAL = 3f;
 12
 13    private IBIWCreatorController creatorController;
 14
 15    private IBIWEntityHandler entityHandler;
 16
 17    private IBIWModeController modeController;
 18
 19    [Header("Audio Events")]
 20    [SerializeField]
 21    AudioEvent eventAssetSpawn;
 22
 23    [SerializeField]
 24    AudioEvent eventAssetPlace;
 25
 26    [SerializeField]
 27    AudioEvent eventAssetSelect;
 28
 29    [SerializeField]
 30    AudioEvent eventAssetDeselect;
 31
 32    [SerializeField]
 33    AudioEvent eventBuilderOutOfBounds;
 34
 35    [SerializeField]
 36    AudioEvent eventBuilderOutOfBoundsPlaced;
 37
 38    [SerializeField]
 39    AudioEvent eventAssetDelete;
 40
 41    [SerializeField]
 42    AudioEvent eventBuilderExit;
 43
 44    [SerializeField]
 45    AudioEvent eventBuilderMusic;
 46
 13447    private List<string> entitiesOutOfBounds = new List<string>();
 48    private int entityCount;
 49    bool playPlacementSoundOnDeselect;
 50    private BIWModeController.EditModeState state = BIWModeController.EditModeState.Inactive;
 51
 52    private Coroutine fadeInCoroutine;
 53    private Coroutine fadeOutCoroutine;
 54    private Coroutine startBuilderMusicCoroutine;
 55
 1456    private void Start() { playPlacementSoundOnDeselect = false; }
 57
 58    public void Initialize(BIWContext context)
 59    {
 2760        creatorController = context.creatorController;
 2761        entityHandler = context.entityHandler;
 2762        modeController = context.modeController;
 63
 2764        AddListeners();
 2765    }
 66
 67    private void AddListeners()
 68    {
 2769        creatorController.OnCatalogItemPlaced += OnAssetSpawn;
 2770        entityHandler.OnDeleteSelectedEntities += OnAssetDelete;
 2771        modeController.OnChangedEditModeState += OnChangedEditModeState;
 2772        DCL.Environment.i.world.sceneBoundsChecker.OnEntityBoundsCheckerStatusChanged += OnEntityBoundsCheckerStatusChan
 73
 2774        if (DCL.Tutorial.TutorialController.i != null)
 75        {
 076            DCL.Tutorial.TutorialController.i.OnTutorialEnabled += OnTutorialEnabled;
 077            DCL.Tutorial.TutorialController.i.OnTutorialDisabled += OnTutorialDisabled;
 78        }
 79
 2780        entityHandler.OnEntityDeselected += OnAssetDeselect;
 2781        entityHandler.OnEntitySelected += OnAssetSelect;
 2782    }
 83
 84    public void EnterEditMode(ParcelScene scene)
 85    {
 986        UpdateEntityCount();
 87
 988        if (eventBuilderMusic.source.gameObject.activeSelf)
 889            startBuilderMusicCoroutine = StartCoroutine(StartBuilderMusic());
 90
 991        if (HUDController.i.builderInWorldMainHud != null)
 892            HUDController.i.builderInWorldMainHud.OnCatalogItemSelected += OnCatalogItemSelected;
 93
 994        gameObject.SetActive(true);
 995    }
 96
 97    public void ExitEditMode()
 98    {
 599        eventBuilderExit.Play();
 5100        if (eventBuilderMusic.source.gameObject.activeSelf)
 3101            fadeOutCoroutine =  StartCoroutine(eventBuilderMusic.FadeOut(MUSIC_FADE_OUT_TIME_ON_EXIT));
 5102        if (HUDController.i.builderInWorldMainHud != null)
 4103            HUDController.i.builderInWorldMainHud.OnCatalogItemSelected -= OnCatalogItemSelected;
 104
 5105        gameObject.SetActive(false);
 5106    }
 107
 0108    private void OnAssetSpawn() { eventAssetSpawn.Play(); }
 109
 110    private void OnAssetDelete(List<BIWEntity> entities)
 111    {
 0112        foreach (BIWEntity deletedEntity in entities)
 113        {
 0114            if (entitiesOutOfBounds.Contains(deletedEntity.rootEntity.entityId))
 115            {
 0116                entitiesOutOfBounds.Remove(deletedEntity.rootEntity.entityId);
 117            }
 118        }
 119
 0120        eventAssetDelete.Play();
 0121    }
 122
 0123    private void OnAssetSelect() { eventAssetSelect.Play(); }
 124
 125    private void OnAssetDeselect(BIWEntity entity)
 126    {
 0127        if (playPlacementSoundOnDeselect)
 128        {
 0129            eventAssetPlace.Play();
 0130            playPlacementSoundOnDeselect = false;
 0131        }
 132        else
 0133            eventAssetDeselect.Play();
 134
 0135        UpdateEntityCount();
 136
 0137        if (entitiesOutOfBounds.Contains(entity.rootEntity.entityId))
 138        {
 0139            eventBuilderOutOfBoundsPlaced.Play();
 140        }
 0141    }
 142
 0143    private void OnCatalogItemSelected(CatalogItem catalogItem) { playPlacementSoundOnDeselect = true; }
 144
 145    private void OnTutorialEnabled()
 146    {
 0147        if (gameObject.activeInHierarchy)
 0148            fadeOutCoroutine =  StartCoroutine(eventBuilderMusic.FadeOut(MUSIC_FADE_OUT_TIME_ON_TUTORIAL));
 0149    }
 150
 151    private void OnTutorialDisabled()
 152    {
 0153        if (gameObject.activeInHierarchy)
 0154            startBuilderMusicCoroutine = StartCoroutine(StartBuilderMusic());
 0155    }
 156
 157    private IEnumerator StartBuilderMusic()
 158    {
 8159        yield return new WaitForSeconds(MUSIC_DELAY_TIME_ON_START);
 160
 0161        if (gameObject != null && gameObject.activeInHierarchy)
 0162            eventBuilderMusic.Play();
 0163    }
 164
 165    private void OnChangedEditModeState(BIWModeController.EditModeState previous, BIWModeController.EditModeState curren
 166    {
 12167        state = current;
 12168        if (previous != BIWModeController.EditModeState.Inactive)
 169        {
 170            switch (current)
 171            {
 172                case BIWModeController.EditModeState.FirstPerson:
 0173                    AudioScriptableObjects.cameraFadeIn.Play();
 0174                    break;
 175                case BIWModeController.EditModeState.GodMode:
 0176                    AudioScriptableObjects.cameraFadeOut.Play();
 177                    break;
 178                default:
 179                    break;
 180            }
 181        }
 10182    }
 183
 184    private void OnEntityBoundsCheckerStatusChanged(DCL.Models.IDCLEntity entity, bool isInsideBoundaries)
 185    {
 0186        if (state == BIWModeController.EditModeState.Inactive)
 0187            return;
 188
 0189        if (!isInsideBoundaries)
 190        {
 0191            if (!entitiesOutOfBounds.Contains(entity.entityId))
 192            {
 0193                entitiesOutOfBounds.Add(entity.entityId);
 0194                eventBuilderOutOfBounds.Play();
 195            }
 0196        }
 197        else
 198        {
 0199            if (entitiesOutOfBounds.Contains(entity.entityId))
 200            {
 0201                entitiesOutOfBounds.Remove(entity.entityId);
 202            }
 203        }
 0204    }
 205
 18206    private void UpdateEntityCount() { entityCount = entityHandler.GetCurrentSceneEntityCount(); }
 207
 0208    private bool EntityHasBeenAddedSinceLastUpdate() { return (entityHandler.GetCurrentSceneEntityCount() > entityCount)
 209
 54210    private void OnDestroy() { Dispose(); }
 211
 212    public void Dispose()
 213    {
 54214        if (startBuilderMusicCoroutine != null)
 8215            StopCoroutine(startBuilderMusicCoroutine);
 216
 54217        if (fadeInCoroutine != null)
 0218            StopCoroutine(fadeInCoroutine);
 219
 54220        if (fadeOutCoroutine != null)
 3221            StopCoroutine(fadeOutCoroutine);
 222
 54223        startBuilderMusicCoroutine = null;
 54224        fadeInCoroutine = null;
 54225        fadeOutCoroutine = null;
 226
 54227        RemoveListeners();
 54228    }
 229
 230    private void RemoveListeners()
 231    {
 54232        creatorController.OnCatalogItemPlaced -= OnAssetSpawn;
 54233        entityHandler.OnDeleteSelectedEntities -= OnAssetDelete;
 54234        modeController.OnChangedEditModeState -= OnChangedEditModeState;
 54235        DCL.Environment.i.world.sceneBoundsChecker.OnEntityBoundsCheckerStatusChanged -= OnEntityBoundsCheckerStatusChan
 236
 54237        if (DCL.Tutorial.TutorialController.i != null)
 238        {
 0239            DCL.Tutorial.TutorialController.i.OnTutorialEnabled -= OnTutorialEnabled;
 0240            DCL.Tutorial.TutorialController.i.OnTutorialDisabled -= OnTutorialDisabled;
 241        }
 242
 54243        entityHandler.OnEntityDeselected -= OnAssetDeselect;
 54244        entityHandler.OnEntitySelected -= OnAssetSelect;
 54245    }
 246}