< Summary

Class:BuilderInWorldAudioHandler
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/BuilderInWorldAudioHandler.cs
Covered lines:45
Uncovered lines:45
Coverable lines:90
Total lines:230
Line coverage:50% (45 of 90)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuilderInWorldAudioHandler()0%110100%
Start()0%2100%
Init(...)0%110100%
OnDestroy()0%110100%
AddListeners()0%2.032080%
RemoveListeners()0%2.032080%
Dispose()0%3.073080%
EnterEditMode(...)0%220100%
ExitEditMode()0%220100%
OnAssetSpawn()0%110100%
OnAssetDelete(...)0%12300%
OnAssetSelect()0%2100%
OnAssetDeselect(...)0%12300%
OnCatalogItemSelected(...)0%2100%
OnTutorialEnabled()0%6200%
OnTutorialDisabled()0%6200%
StartBuilderMusic()0%10.754025%
OnChangedEditModeState(...)0%64050%
OnEntityBoundsCheckerStatusChanged(...)0%30500%
UpdateEntityCount()0%110100%
EntityHasBeenAddedSinceLastUpdate()0%2100%

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 UnityEngine;
 5
 6public class BuilderInWorldAudioHandler : MonoBehaviour
 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    private IBIWCreatorController creatorController;
 13
 14    private IBIWEntityHandler entityHandler;
 15
 16    private IBIWModeController modeController;
 17
 18    [Header("Audio Events")]
 19    [SerializeField]
 20    AudioEvent eventAssetSpawn;
 21
 22    [SerializeField]
 23    AudioEvent eventAssetPlace;
 24
 25    [SerializeField]
 26    AudioEvent eventAssetSelect;
 27
 28    [SerializeField]
 29    AudioEvent eventAssetDeselect;
 30
 31    [SerializeField]
 32    AudioEvent eventBuilderOutOfBounds;
 33
 34    [SerializeField]
 35    AudioEvent eventBuilderOutOfBoundsPlaced;
 36
 37    [SerializeField]
 38    AudioEvent eventAssetDelete;
 39
 40    [SerializeField]
 41    AudioEvent eventBuilderExit;
 42
 43    [SerializeField]
 44    AudioEvent eventBuilderMusic;
 45
 11746    private List<string> entitiesOutOfBounds = new List<string>();
 47    private int entityCount;
 48    bool playPlacementSoundOnDeselect;
 49    private BIWModeController.EditModeState state = BIWModeController.EditModeState.Inactive;
 50    private Coroutine fadeInCoroutine, fadeOutCoroutine;
 51
 052    private void Start() { playPlacementSoundOnDeselect = false; }
 53
 54    public void Init(BIWContext context)
 55    {
 2856        creatorController = context.creatorController;
 57
 2858        entityHandler = context.entityHandler;
 59
 2860        modeController = context.modeController;
 61
 2862        AddListeners();
 2863    }
 64
 5665    private void OnDestroy() { RemoveListeners(); }
 66
 67    private void AddListeners()
 68    {
 2869        creatorController.OnCatalogItemPlaced += OnAssetSpawn;
 2870        entityHandler.OnDeleteSelectedEntities += OnAssetDelete;
 2871        modeController.OnChangedEditModeState += OnChangedEditModeState;
 2872        DCL.Environment.i.world.sceneBoundsChecker.OnEntityBoundsCheckerStatusChanged += OnEntityBoundsCheckerStatusChan
 2873        if (DCL.Tutorial.TutorialController.i != null)
 74        {
 075            DCL.Tutorial.TutorialController.i.OnTutorialEnabled += OnTutorialEnabled;
 076            DCL.Tutorial.TutorialController.i.OnTutorialDisabled += OnTutorialDisabled;
 77        }
 78
 2879        entityHandler.OnEntityDeselected += OnAssetDeselect;
 2880        entityHandler.OnEntitySelected += OnAssetSelect;
 2881    }
 82
 83    private void RemoveListeners()
 84    {
 2885        creatorController.OnCatalogItemPlaced -= OnAssetSpawn;
 2886        entityHandler.OnDeleteSelectedEntities -= OnAssetDelete;
 2887        modeController.OnChangedEditModeState -= OnChangedEditModeState;
 2888        DCL.Environment.i.world.sceneBoundsChecker.OnEntityBoundsCheckerStatusChanged -= OnEntityBoundsCheckerStatusChan
 89
 2890        if (DCL.Tutorial.TutorialController.i != null)
 91        {
 092            DCL.Tutorial.TutorialController.i.OnTutorialEnabled -= OnTutorialEnabled;
 093            DCL.Tutorial.TutorialController.i.OnTutorialDisabled -= OnTutorialDisabled;
 94        }
 95
 2896        entityHandler.OnEntityDeselected -= OnAssetDeselect;
 2897        entityHandler.OnEntitySelected -= OnAssetSelect;
 2898    }
 99
 100    public void Dispose()
 101    {
 29102        if (fadeInCoroutine != null)
 0103            CoroutineStarter.Stop(fadeInCoroutine);
 104
 29105        if (fadeOutCoroutine != null)
 5106            CoroutineStarter.Stop(fadeOutCoroutine);
 29107    }
 108
 109    public void EnterEditMode(ParcelScene scene)
 110    {
 8111        UpdateEntityCount();
 8112        CoroutineStarter.Start(StartBuilderMusic());
 8113        if (HUDController.i.builderInWorldMainHud != null)
 7114            HUDController.i.builderInWorldMainHud.OnCatalogItemSelected += OnCatalogItemSelected;
 115
 8116        gameObject.SetActive(true);
 8117    }
 118
 119    public void ExitEditMode()
 120    {
 5121        eventBuilderExit.Play();
 5122        fadeOutCoroutine = CoroutineStarter.Start(eventBuilderMusic.FadeOut(MUSIC_FADE_OUT_TIME_ON_EXIT));
 5123        if (HUDController.i.builderInWorldMainHud != null)
 4124            HUDController.i.builderInWorldMainHud.OnCatalogItemSelected -= OnCatalogItemSelected;
 125
 5126        gameObject.SetActive(false);
 5127    }
 128
 2129    private void OnAssetSpawn() { eventAssetSpawn.Play(); }
 130
 131    private void OnAssetDelete(List<BIWEntity> entities)
 132    {
 0133        foreach (BIWEntity deletedEntity in entities)
 134        {
 0135            if (entitiesOutOfBounds.Contains(deletedEntity.rootEntity.entityId))
 136            {
 0137                entitiesOutOfBounds.Remove(deletedEntity.rootEntity.entityId);
 138            }
 139        }
 140
 0141        eventAssetDelete.Play();
 0142    }
 143
 0144    private void OnAssetSelect() { eventAssetSelect.Play(); }
 145
 146    private void OnAssetDeselect(BIWEntity entity)
 147    {
 0148        if (playPlacementSoundOnDeselect)
 149        {
 0150            eventAssetPlace.Play();
 0151            playPlacementSoundOnDeselect = false;
 0152        }
 153        else
 0154            eventAssetDeselect.Play();
 155
 0156        UpdateEntityCount();
 157
 0158        if (entitiesOutOfBounds.Contains(entity.rootEntity.entityId))
 159        {
 0160            eventBuilderOutOfBoundsPlaced.Play();
 161        }
 0162    }
 163
 0164    private void OnCatalogItemSelected(CatalogItem catalogItem) { playPlacementSoundOnDeselect = true; }
 165
 166    private void OnTutorialEnabled()
 167    {
 0168        if (gameObject.activeInHierarchy)
 0169            fadeOutCoroutine = CoroutineStarter.Start(eventBuilderMusic.FadeOut(MUSIC_FADE_OUT_TIME_ON_TUTORIAL));
 0170    }
 171
 172    private void OnTutorialDisabled()
 173    {
 0174        if (gameObject.activeInHierarchy)
 0175            CoroutineStarter.Start(StartBuilderMusic());
 0176    }
 177
 178    private IEnumerator StartBuilderMusic()
 179    {
 8180        yield return new WaitForSeconds(MUSIC_DELAY_TIME_ON_START);
 181
 0182        if (gameObject.activeInHierarchy)
 0183            eventBuilderMusic.Play();
 0184    }
 185
 186    private void OnChangedEditModeState(BIWModeController.EditModeState previous, BIWModeController.EditModeState curren
 187    {
 11188        state = current;
 11189        if (previous != BIWModeController.EditModeState.Inactive)
 190        {
 191            switch (current)
 192            {
 193                case BIWModeController.EditModeState.FirstPerson:
 0194                    AudioScriptableObjects.cameraFadeIn.Play();
 0195                    break;
 196                case BIWModeController.EditModeState.GodMode:
 0197                    AudioScriptableObjects.cameraFadeOut.Play();
 198                    break;
 199                default:
 200                    break;
 201            }
 202        }
 9203    }
 204
 205    private void OnEntityBoundsCheckerStatusChanged(DCL.Models.IDCLEntity entity, bool isInsideBoundaries)
 206    {
 0207        if (state == BIWModeController.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
 16227    private void UpdateEntityCount() { entityCount = entityHandler.GetCurrentSceneEntityCount(); }
 228
 0229    private bool EntityHasBeenAddedSinceLastUpdate() { return (entityHandler.GetCurrentSceneEntityCount() > entityCount)
 230}