< 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:72
Uncovered lines:5
Coverable lines:77
Total lines:192
Line coverage:93.5% (72 of 77)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WorldBlockersController()0%110100%
WorldBlockersController()0%110100%
Initialize(...)0%110100%
OnRendererStateChange(...)0%3.333066.67%
CreateWithDefaultDependencies(...)0%2100%
InitializeWithDefaultDependencies(...)0%110100%
SetupWorldBlockers()0%330100%
SetEnabled(...)0%220100%
OnWorldReposition(...)0%110100%
Dispose()0%220100%
SetupWorldBlockers(...)0%1313097.62%

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using DCL.Helpers;
 3using DCL.Rendering;
 4using UnityEngine;
 5
 6namespace DCL.Controllers
 7{
 8    /// <summary>
 9    /// This class is the domain-specific glue for BlockerInstanceHandler.
 10    /// <br/><br/>
 11    /// Responsibilities:<br/>
 12    /// - Spawning blockers depending on scene state<br/>
 13    /// - Moving blockers when the world is repositioned<br/>
 14    /// - Handling lifecycle of BlockerInstanceHandler<br/>
 15    /// </summary>
 16    public class WorldBlockersController : IWorldBlockersController
 17    {
 65118        public bool enabled = true;
 19
 20        Transform blockersParent;
 21
 22        ISceneHandler sceneHandler;
 23        IBlockerInstanceHandler blockerInstanceHandler;
 24
 65125        HashSet<Vector2Int> blockersToRemove = new HashSet<Vector2Int>();
 65126        HashSet<Vector2Int> blockersToAdd = new HashSet<Vector2Int>();
 27
 128        static Vector2Int[] aroundOffsets =
 29        {
 30            new Vector2Int(1, 0),
 31            new Vector2Int(-1, 0),
 32            new Vector2Int(0, 1),
 33            new Vector2Int(0, -1),
 34            new Vector2Int(1, 1),
 35            new Vector2Int(-1, -1),
 36            new Vector2Int(1, -1),
 37            new Vector2Int(-1, 1)
 38        };
 39
 40        public void Initialize(ISceneHandler sceneHandler, IBlockerInstanceHandler blockerInstanceHandler)
 41        {
 65242            enabled = true;
 65243            this.blockerInstanceHandler = blockerInstanceHandler;
 65244            this.sceneHandler = sceneHandler;
 45
 65246            blockerInstanceHandler.SetParent(blockersParent);
 47
 65248            CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition;
 65249            CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition;
 50
 65251            CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange;
 65252            CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange;
 65253        }
 54
 55        void OnRendererStateChange(bool newValue, bool oldValue)
 56        {
 420657            if (newValue && DataStore.i.debugConfig.isDebugMode.Get())
 058                SetEnabled(false);
 420659        }
 60
 65161        public WorldBlockersController()
 62        {
 65163            blockersParent = new GameObject("WorldBlockers").transform;
 65164            blockersParent.position = Vector3.zero;
 65165        }
 66
 67        public static WorldBlockersController CreateWithDefaultDependencies(ISceneHandler sceneHandler, ICullingControll
 68        {
 069            var worldBlockersController = new WorldBlockersController();
 070            worldBlockersController.InitializeWithDefaultDependencies(sceneHandler, cullingController);
 071            return worldBlockersController;
 72        }
 73
 74        public void InitializeWithDefaultDependencies(ISceneHandler sceneHandler, ICullingController cullingController)
 75        {
 64876            var blockerAnimationHandler = new BlockerAnimationHandler();
 64877            var blockerInstanceHandler = new BlockerInstanceHandler();
 78
 64879            blockerInstanceHandler.Initialize(
 80                blockerAnimationHandler,
 81                cullingController
 82            );
 83
 64884            Initialize(
 85                sceneHandler,
 86                blockerInstanceHandler);
 64887        }
 88
 89        public void SetupWorldBlockers()
 90        {
 78291            if (!enabled || sceneHandler == null)
 38992                return;
 93
 39394            SetupWorldBlockers(sceneHandler.GetAllLoadedScenesCoords());
 39395        }
 96
 97        public void SetEnabled(bool targetValue)
 98        {
 899            enabled = targetValue;
 100
 8101            if (!enabled)
 6102                blockerInstanceHandler.DestroyAllBlockers();
 8103        }
 104
 105        void OnWorldReposition(Vector3 current, Vector3 previous)
 106        {
 2107            var newPosition = PositionUtils.WorldToUnityPosition(Vector3.zero); // Blockers parent original position
 2108            blockersParent.position = newPosition;
 2109        }
 110
 111        public void Dispose()
 112        {
 658113            CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition;
 658114            blockerInstanceHandler.DestroyAllBlockers();
 115
 658116            if (blockersParent != null)
 658117                Object.Destroy(blockersParent.gameObject);
 118
 658119            enabled = false;
 658120        }
 121
 122        internal void SetupWorldBlockers(HashSet<Vector2Int> allLoadedParcelCoords)
 123        {
 393124            if (allLoadedParcelCoords.Count == 0)
 386125                return;
 126
 7127            blockersToRemove.Clear();
 7128            blockersToAdd.Clear();
 129
 7130            var blockers = blockerInstanceHandler.GetBlockers();
 131
 132            // Detect blockers to be removed
 38133            foreach (var item in blockers)
 134            {
 12135                if (allLoadedParcelCoords.Contains(item.Key))
 136                {
 2137                    blockersToRemove.Add(item.Key);
 2138                }
 139                else
 140                {
 10141                    bool foundAroundLoadedScenes = false;
 70142                    for (int i = 0; i < aroundOffsets.Length; i++)
 143                    {
 35144                        Vector2Int offset = aroundOffsets[i];
 35145                        Vector2Int checkedPosition = new Vector2Int(item.Key.x + offset.x, item.Key.y + offset.y);
 146
 35147                        if (allLoadedParcelCoords.Contains(checkedPosition))
 148                        {
 10149                            foundAroundLoadedScenes = true;
 10150                            break;
 151                        }
 152                    }
 153
 10154                    if (!foundAroundLoadedScenes)
 0155                        blockersToRemove.Add(item.Key);
 156                }
 157            }
 158
 159            // Detect missing blockers to be added
 7160            using (var it = allLoadedParcelCoords.GetEnumerator())
 161            {
 27162                while (it.MoveNext())
 163                {
 20164                    Vector2Int pos = it.Current;
 165
 360166                    for (int i = 0; i < aroundOffsets.Length; i++)
 167                    {
 160168                        Vector2Int offset = aroundOffsets[i];
 160169                        Vector2Int checkedPosition = new Vector2Int(pos.x + offset.x, pos.y + offset.y);
 170
 160171                        if (!allLoadedParcelCoords.Contains(checkedPosition) && !blockers.ContainsKey(checkedPosition))
 172                        {
 96173                            blockersToAdd.Add(checkedPosition);
 174                        }
 175                    }
 176                }
 7177            }
 178
 179            // Remove extra blockers
 18180            foreach (var coords in blockersToRemove)
 181            {
 2182                blockerInstanceHandler.HideBlocker(coords, false);
 183            }
 184
 185            // Add missing blockers
 154186            foreach (var coords in blockersToAdd)
 187            {
 70188                blockerInstanceHandler.ShowBlocker(coords);
 189            }
 7190        }
 191    }
 192}