< Summary

Class:DCL.Controllers.WorldBlockersController
Assembly:WorldBlockersController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/BlockerController/WorldBlockersController.cs
Covered lines:76
Uncovered lines:5
Coverable lines:81
Total lines:199
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/WorldRuntime/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    {
 66520        public bool enabled = true;
 21
 22        Transform blockersParent;
 23
 24        ISceneHandler sceneHandler;
 25        IBlockerInstanceHandler blockerInstanceHandler;
 26
 66527        HashSet<Vector2Int> blockersToRemove = new HashSet<Vector2Int>();
 66528        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        {
 75144            blockerInstanceHandler.SetCollision(newValue);
 45
 75146            if (newValue && DataStore.i.debugConfig.isDebugMode.Get())
 047                SetEnabled(false);
 75148        }
 49
 66550        public WorldBlockersController(IBlockerInstanceHandler blockerInstanceHandler = null,
 51            ISceneHandler sceneHandler = null)
 52        {
 66553            this.sceneHandler = sceneHandler;
 66554            this.blockerInstanceHandler = blockerInstanceHandler;
 66555        }
 56
 57        public void SetupWorldBlockers()
 58        {
 59#if UNITY_STANDALONE || UNITY_EDITOR
 81660            if (DataStore.i.common.isApplicationQuitting.Get())
 061                return;
 62#endif
 63
 81664            if (!enabled || sceneHandler == null)
 865                return;
 66
 80867            SetupWorldBlockers(sceneHandler.GetAllLoadedScenesCoords());
 80868        }
 69
 70        public void SetEnabled(bool targetValue)
 71        {
 72#if UNITY_STANDALONE || UNITY_EDITOR
 873            if (DataStore.i.common.isApplicationQuitting.Get())
 074                return;
 75#endif
 76
 877            enabled = targetValue;
 78
 879            if (!enabled)
 680                blockerInstanceHandler.DestroyAllBlockers();
 881        }
 82
 83        void OnWorldReposition(Vector3 current, Vector3 previous)
 84        {
 285            var newPosition = PositionUtils.WorldToUnityPosition(Vector3.zero); // Blockers parent original position
 286            blockersParent.position = newPosition;
 287        }
 88
 89        public void Dispose()
 90        {
 91#if UNITY_STANDALONE || UNITY_EDITOR
 66692            if (DataStore.i.common.isApplicationQuitting.Get())
 093                return;
 94#endif
 95
 66696            CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition;
 66697            blockerInstanceHandler.DestroyAllBlockers();
 98
 66699            if (blockersParent != null)
 661100                Object.Destroy(blockersParent.gameObject);
 101
 666102            enabled = false;
 666103        }
 104
 105        public void Initialize()
 106        {
 661107            enabled = true;
 661108            blockersParent = new GameObject("WorldBlockers").transform;
 661109            blockersParent.position = Vector3.zero;
 110
 661111            if (blockerInstanceHandler == null)
 112            {
 661113                var blockerAnimationHandler = new BlockerAnimationHandler();
 661114                blockerInstanceHandler = new BlockerInstanceHandler(blockerAnimationHandler);
 115            }
 116
 661117            if (this.sceneHandler == null)
 661118                this.sceneHandler = DCL.Environment.i.world.state;
 119
 661120            blockerInstanceHandler.SetParent(blockersParent);
 121
 661122            CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition;
 661123            CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition;
 124
 661125            CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange;
 661126            CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange;
 661127        }
 128
 129        internal void SetupWorldBlockers(HashSet<Vector2Int> allLoadedParcelCoords)
 130        {
 808131            if (allLoadedParcelCoords.Count == 0)
 798132                return;
 133
 10134            blockersToRemove.Clear();
 10135            blockersToAdd.Clear();
 136
 10137            var blockers = blockerInstanceHandler.GetBlockers();
 138
 139            // Detect blockers to be removed
 60140            foreach (var item in blockers)
 141            {
 20142                if (allLoadedParcelCoords.Contains(item.Key))
 143                {
 2144                    blockersToRemove.Add(item.Key);
 2145                }
 146                else
 147                {
 18148                    bool foundAroundLoadedScenes = false;
 142149                    for (int i = 0; i < aroundOffsets.Length; i++)
 150                    {
 71151                        Vector2Int offset = aroundOffsets[i];
 71152                        Vector2Int checkedPosition = new Vector2Int(item.Key.x + offset.x, item.Key.y + offset.y);
 153
 71154                        if (allLoadedParcelCoords.Contains(checkedPosition))
 155                        {
 18156                            foundAroundLoadedScenes = true;
 18157                            break;
 158                        }
 159                    }
 160
 18161                    if (!foundAroundLoadedScenes)
 0162                        blockersToRemove.Add(item.Key);
 163                }
 164            }
 165
 166            // Detect missing blockers to be added
 10167            using (var it = allLoadedParcelCoords.GetEnumerator())
 168            {
 33169                while (it.MoveNext())
 170                {
 23171                    Vector2Int pos = it.Current;
 172
 414173                    for (int i = 0; i < aroundOffsets.Length; i++)
 174                    {
 184175                        Vector2Int offset = aroundOffsets[i];
 184176                        Vector2Int checkedPosition = new Vector2Int(pos.x + offset.x, pos.y + offset.y);
 177
 184178                        if (!allLoadedParcelCoords.Contains(checkedPosition) && !blockers.ContainsKey(checkedPosition))
 179                        {
 112180                            blockersToAdd.Add(checkedPosition);
 181                        }
 182                    }
 183                }
 10184            }
 185
 186            // Remove extra blockers
 24187            foreach (var coords in blockersToRemove)
 188            {
 2189                blockerInstanceHandler.HideBlocker(coords, false);
 190            }
 191
 192            // Add missing blockers
 192193            foreach (var coords in blockersToAdd)
 194            {
 86195                blockerInstanceHandler.ShowBlocker(coords, false, CommonScriptableObjects.rendererState.Get());
 196            }
 10197        }
 198    }
 199}