< Summary

Class:DCL.Controllers.WorldBlockersController
Assembly:WorldBlockersController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BlockerController/WorldBlockersController.cs
Covered lines:75
Uncovered lines:5
Coverable lines:80
Total lines:196
Line coverage:93.7% (75 of 80)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WorldBlockersController(...)0%110100%
WorldBlockersController()0%110100%
OnRendererStateChange(...)0%3.333066.67%
SetupWorldBlockers()0%4.074083.33%
SetEnabled(...)0%3.043083.33%
OnWorldReposition(...)0%110100%
Dispose()0%3.023087.5%
Initialize()0%330100%
SetupWorldBlockers(...)0%1313097.62%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BlockerController/WorldBlockersController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using DCL.Helpers;
 4using DCL.Rendering;
 5using UnityEngine;
 6using Object = UnityEngine.Object;
 7
 8namespace DCL.Controllers
 9{
 10    /// <summary>
 11    /// This class is the domain-specific glue for BlockerInstanceHandler.
 12    /// <br/><br/>
 13    /// Responsibilities:<br/>
 14    /// - Spawning blockers depending on scene state<br/>
 15    /// - Moving blockers when the world is repositioned<br/>
 16    /// - Handling lifecycle of BlockerInstanceHandler<br/>
 17    /// </summary>
 18    public class WorldBlockersController : IWorldBlockersController
 19    {
 63420        public bool enabled = true;
 21
 22        Transform blockersParent;
 23
 24        ISceneHandler sceneHandler;
 25        IBlockerInstanceHandler blockerInstanceHandler;
 26
 63427        HashSet<Vector2Int> blockersToRemove = new HashSet<Vector2Int>();
 63428        HashSet<Vector2Int> blockersToAdd = new HashSet<Vector2Int>();
 29
 130        static Vector2Int[] aroundOffsets =
 31        {
 32            new Vector2Int(1, 0),
 33            new Vector2Int(-1, 0),
 34            new Vector2Int(0, 1),
 35            new Vector2Int(0, -1),
 36            new Vector2Int(1, 1),
 37            new Vector2Int(-1, -1),
 38            new Vector2Int(1, -1),
 39            new Vector2Int(-1, 1)
 40        };
 41
 42        void OnRendererStateChange(bool newValue, bool oldValue)
 43        {
 409644            if (newValue && DataStore.i.debugConfig.isDebugMode.Get())
 045                SetEnabled(false);
 409646        }
 47
 63448        public WorldBlockersController(IBlockerInstanceHandler blockerInstanceHandler = null, ISceneHandler sceneHandler
 49        {
 63450            this.sceneHandler = sceneHandler;
 63451            this.blockerInstanceHandler = blockerInstanceHandler;
 63452        }
 53
 54        public void SetupWorldBlockers()
 55        {
 56#if UNITY_STANDALONE || UNITY_EDITOR
 75557            if (DataStore.i.common.isApplicationQuitting.Get())
 058                return;
 59#endif
 60
 75561            if (!enabled || sceneHandler == null)
 862                return;
 63
 74764            SetupWorldBlockers(sceneHandler.GetAllLoadedScenesCoords());
 74765        }
 66
 67        public void SetEnabled(bool targetValue)
 68        {
 69#if UNITY_STANDALONE || UNITY_EDITOR
 870            if (DataStore.i.common.isApplicationQuitting.Get())
 071                return;
 72#endif
 73
 874            enabled = targetValue;
 75
 876            if (!enabled)
 677                blockerInstanceHandler.DestroyAllBlockers();
 878        }
 79
 80        void OnWorldReposition(Vector3 current, Vector3 previous)
 81        {
 282            var newPosition = PositionUtils.WorldToUnityPosition(Vector3.zero); // Blockers parent original position
 283            blockersParent.position = newPosition;
 284        }
 85
 86        public void Dispose()
 87        {
 88#if UNITY_STANDALONE || UNITY_EDITOR
 63589            if (DataStore.i.common.isApplicationQuitting.Get())
 090                return;
 91#endif
 92
 63593            CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition;
 63594            blockerInstanceHandler.DestroyAllBlockers();
 95
 63596            if (blockersParent != null)
 63097                Object.Destroy(blockersParent.gameObject);
 98
 63599            enabled = false;
 635100        }
 101
 102        public void Initialize()
 103        {
 630104            enabled = true;
 630105            blockersParent = new GameObject("WorldBlockers").transform;
 630106            blockersParent.position = Vector3.zero;
 107
 630108            if ( blockerInstanceHandler == null )
 109            {
 630110                var blockerAnimationHandler = new BlockerAnimationHandler();
 630111                blockerInstanceHandler = new BlockerInstanceHandler(blockerAnimationHandler);
 112            }
 113
 630114            if ( this.sceneHandler == null )
 630115                this.sceneHandler = DCL.Environment.i.world.state;
 116
 630117            blockerInstanceHandler.SetParent(blockersParent);
 118
 630119            CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition;
 630120            CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition;
 121
 630122            CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange;
 630123            CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange;
 630124        }
 125
 126        internal void SetupWorldBlockers(HashSet<Vector2Int> allLoadedParcelCoords)
 127        {
 747128            if (allLoadedParcelCoords.Count == 0)
 740129                return;
 130
 7131            blockersToRemove.Clear();
 7132            blockersToAdd.Clear();
 133
 7134            var blockers = blockerInstanceHandler.GetBlockers();
 135
 136            // Detect blockers to be removed
 38137            foreach (var item in blockers)
 138            {
 12139                if (allLoadedParcelCoords.Contains(item.Key))
 140                {
 2141                    blockersToRemove.Add(item.Key);
 2142                }
 143                else
 144                {
 10145                    bool foundAroundLoadedScenes = false;
 70146                    for (int i = 0; i < aroundOffsets.Length; i++)
 147                    {
 35148                        Vector2Int offset = aroundOffsets[i];
 35149                        Vector2Int checkedPosition = new Vector2Int(item.Key.x + offset.x, item.Key.y + offset.y);
 150
 35151                        if (allLoadedParcelCoords.Contains(checkedPosition))
 152                        {
 10153                            foundAroundLoadedScenes = true;
 10154                            break;
 155                        }
 156                    }
 157
 10158                    if (!foundAroundLoadedScenes)
 0159                        blockersToRemove.Add(item.Key);
 160                }
 161            }
 162
 163            // Detect missing blockers to be added
 7164            using (var it = allLoadedParcelCoords.GetEnumerator())
 165            {
 27166                while (it.MoveNext())
 167                {
 20168                    Vector2Int pos = it.Current;
 169
 360170                    for (int i = 0; i < aroundOffsets.Length; i++)
 171                    {
 160172                        Vector2Int offset = aroundOffsets[i];
 160173                        Vector2Int checkedPosition = new Vector2Int(pos.x + offset.x, pos.y + offset.y);
 174
 160175                        if (!allLoadedParcelCoords.Contains(checkedPosition) && !blockers.ContainsKey(checkedPosition))
 176                        {
 96177                            blockersToAdd.Add(checkedPosition);
 178                        }
 179                    }
 180                }
 7181            }
 182
 183            // Remove extra blockers
 18184            foreach (var coords in blockersToRemove)
 185            {
 2186                blockerInstanceHandler.HideBlocker(coords, false);
 187            }
 188
 189            // Add missing blockers
 154190            foreach (var coords in blockersToAdd)
 191            {
 70192                blockerInstanceHandler.ShowBlocker(coords);
 193            }
 7194        }
 195    }
 196}