< 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:76
Uncovered lines:5
Coverable lines:81
Total lines:198
Line coverage:93.8% (76 of 81)
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.143075%
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    {
 63320        public bool enabled = true;
 21
 22        Transform blockersParent;
 23
 24        ISceneHandler sceneHandler;
 25        IBlockerInstanceHandler blockerInstanceHandler;
 26
 63327        HashSet<Vector2Int> blockersToRemove = new HashSet<Vector2Int>();
 63328        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        {
 410244            blockerInstanceHandler.SetCollision(newValue);
 45
 410246            if (newValue && DataStore.i.debugConfig.isDebugMode.Get())
 047                SetEnabled(false);
 410248        }
 49
 63350        public WorldBlockersController(IBlockerInstanceHandler blockerInstanceHandler = null, ISceneHandler sceneHandler
 51        {
 63352            this.sceneHandler = sceneHandler;
 63353            this.blockerInstanceHandler = blockerInstanceHandler;
 63354        }
 55
 56        public void SetupWorldBlockers()
 57        {
 58#if UNITY_STANDALONE || UNITY_EDITOR
 75759            if (DataStore.i.common.isApplicationQuitting.Get())
 060                return;
 61#endif
 62
 75763            if (!enabled || sceneHandler == null)
 864                return;
 65
 74966            SetupWorldBlockers(sceneHandler.GetAllLoadedScenesCoords());
 74967        }
 68
 69        public void SetEnabled(bool targetValue)
 70        {
 71#if UNITY_STANDALONE || UNITY_EDITOR
 872            if (DataStore.i.common.isApplicationQuitting.Get())
 073                return;
 74#endif
 75
 876            enabled = targetValue;
 77
 878            if (!enabled)
 679                blockerInstanceHandler.DestroyAllBlockers();
 880        }
 81
 82        void OnWorldReposition(Vector3 current, Vector3 previous)
 83        {
 284            var newPosition = PositionUtils.WorldToUnityPosition(Vector3.zero); // Blockers parent original position
 285            blockersParent.position = newPosition;
 286        }
 87
 88        public void Dispose()
 89        {
 90#if UNITY_STANDALONE || UNITY_EDITOR
 63491            if (DataStore.i.common.isApplicationQuitting.Get())
 092                return;
 93#endif
 94
 63495            CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition;
 63496            blockerInstanceHandler.DestroyAllBlockers();
 97
 63498            if (blockersParent != null)
 62999                Object.Destroy(blockersParent.gameObject);
 100
 634101            enabled = false;
 634102        }
 103
 104        public void Initialize()
 105        {
 629106            enabled = true;
 629107            blockersParent = new GameObject("WorldBlockers").transform;
 629108            blockersParent.position = Vector3.zero;
 109
 629110            if ( blockerInstanceHandler == null )
 111            {
 629112                var blockerAnimationHandler = new BlockerAnimationHandler();
 629113                blockerInstanceHandler = new BlockerInstanceHandler(blockerAnimationHandler);
 114            }
 115
 629116            if ( this.sceneHandler == null )
 629117                this.sceneHandler = DCL.Environment.i.world.state;
 118
 629119            blockerInstanceHandler.SetParent(blockersParent);
 120
 629121            CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition;
 629122            CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition;
 123
 629124            CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange;
 629125            CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange;
 629126        }
 127
 128        internal void SetupWorldBlockers(HashSet<Vector2Int> allLoadedParcelCoords)
 129        {
 749130            if (allLoadedParcelCoords.Count == 0)
 742131                return;
 132
 7133            blockersToRemove.Clear();
 7134            blockersToAdd.Clear();
 135
 7136            var blockers = blockerInstanceHandler.GetBlockers();
 137
 138            // Detect blockers to be removed
 38139            foreach (var item in blockers)
 140            {
 12141                if (allLoadedParcelCoords.Contains(item.Key))
 142                {
 2143                    blockersToRemove.Add(item.Key);
 2144                }
 145                else
 146                {
 10147                    bool foundAroundLoadedScenes = false;
 70148                    for (int i = 0; i < aroundOffsets.Length; i++)
 149                    {
 35150                        Vector2Int offset = aroundOffsets[i];
 35151                        Vector2Int checkedPosition = new Vector2Int(item.Key.x + offset.x, item.Key.y + offset.y);
 152
 35153                        if (allLoadedParcelCoords.Contains(checkedPosition))
 154                        {
 10155                            foundAroundLoadedScenes = true;
 10156                            break;
 157                        }
 158                    }
 159
 10160                    if (!foundAroundLoadedScenes)
 0161                        blockersToRemove.Add(item.Key);
 162                }
 163            }
 164
 165            // Detect missing blockers to be added
 7166            using (var it = allLoadedParcelCoords.GetEnumerator())
 167            {
 27168                while (it.MoveNext())
 169                {
 20170                    Vector2Int pos = it.Current;
 171
 360172                    for (int i = 0; i < aroundOffsets.Length; i++)
 173                    {
 160174                        Vector2Int offset = aroundOffsets[i];
 160175                        Vector2Int checkedPosition = new Vector2Int(pos.x + offset.x, pos.y + offset.y);
 176
 160177                        if (!allLoadedParcelCoords.Contains(checkedPosition) && !blockers.ContainsKey(checkedPosition))
 178                        {
 96179                            blockersToAdd.Add(checkedPosition);
 180                        }
 181                    }
 182                }
 7183            }
 184
 185            // Remove extra blockers
 18186            foreach (var coords in blockersToRemove)
 187            {
 2188                blockerInstanceHandler.HideBlocker(coords, false);
 189            }
 190
 191            // Add missing blockers
 154192            foreach (var coords in blockersToAdd)
 193            {
 70194                blockerInstanceHandler.ShowBlocker(coords, false, CommonScriptableObjects.rendererState.Get());
 195            }
 7196        }
 197    }
 198}