< Summary

Class:DCL.Controllers.BlockerInstanceHandler
Assembly:WorldBlockersController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BlockerController/BlockerHandler.cs
Covered lines:50
Uncovered lines:1
Coverable lines:51
Total lines:129
Line coverage:98% (50 of 51)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BlockerInstanceHandler(...)0%110100%
ShowBlocker(...)0%330100%
EnsureBlockerPool()0%220100%
SetParent(...)0%110100%
HideBlocker(...)0%220100%
ReleaseBlocker(...)0%2.032080%
GetBlockers()0%110100%
DestroyAllBlockers()0%220100%

File(s)

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

#LineLine coverage
 1using System;
 2using DCL.Configuration;
 3using DCL.Helpers;
 4using System.Collections.Generic;
 5using System.Linq;
 6using DCL.Rendering;
 7using UnityEngine;
 8using Object = UnityEngine.Object;
 9
 10namespace DCL.Controllers
 11{
 12    /// <summary>
 13    /// This class is the loading blockers composite and instancing handler.
 14    /// <br/>
 15    /// Responsibilities include:<br/>
 16    /// - Handling hide/show of blockers through its IBlockerAnimationHandler<br/>
 17    /// - Keeping track of all blockers<br/>
 18    /// </summary>
 19    public class BlockerInstanceHandler : IBlockerInstanceHandler
 20    {
 21        static GameObject blockerPrefab;
 22
 23        const string PARCEL_BLOCKER_POOL_NAME = "ParcelBlocker";
 24
 25        Vector3 auxPosVec = new Vector3();
 26        Vector3 auxScaleVec = new Vector3();
 27
 63928        Dictionary<Vector2Int, IPoolableObject> blockers = new Dictionary<Vector2Int, IPoolableObject>();
 29
 30        private IBlockerAnimationHandler animationHandler;
 31        private Transform parent;
 32
 63933        public BlockerInstanceHandler(IBlockerAnimationHandler animationHandler)
 34        {
 63935            this.animationHandler = animationHandler;
 63936            blockerPrefab = Resources.Load<GameObject>("LoadingBlocker_Green");
 63937            EnsureBlockerPool();
 38            //TODO We lost the purple blockers when implementing the procedural skybox
 39            // We could set a color in their shader dynamically,
 40            // similar to how we set toon shader material in the Editor to disable the skybox
 63941        }
 42
 43        public void ShowBlocker(Vector2Int pos, bool instant = false)
 44        {
 5445            float centerOffset = ParcelSettings.PARCEL_SIZE / 2;
 5446            PoolableObject blockerPoolable = PoolManager.i.Get(PARCEL_BLOCKER_POOL_NAME);
 5447            GameObject blockerGo = blockerPoolable.gameObject;
 5448            BoxCollider blockerCollider = blockerGo.GetComponent<BoxCollider>();
 49
 5450            Vector3 blockerPos = PositionUtils.WorldToUnityPosition(Utils.GridToWorldPosition(pos.x, pos.y));
 51
 5452            auxPosVec.x = blockerPos.x + centerOffset;
 5453            auxPosVec.z = blockerPos.z + centerOffset;
 5454            auxPosVec.y = 8;
 55
 5456            Transform blockerTransform = blockerGo.transform;
 5457            blockerTransform.SetParent(parent, false);
 5458            blockerTransform.position = auxPosVec;
 5459            blockerTransform.localScale = Vector3.one * 16;
 60
 5461            blockerCollider.size = Vector3.one + (Vector3.up * auxScaleVec.y);
 5462            blockerCollider.center = Vector3.up * ((auxScaleVec.y / 2) - 0.5f);
 63
 64#if UNITY_EDITOR
 5465            blockerGo.name = "BLOCKER " + pos;
 66#endif
 67
 5468            blockers.Add(pos, blockerPoolable);
 69
 5470            if (!instant)
 4771                animationHandler.FadeIn(blockerGo);
 72
 5473            DCL.Environment.i.platform.cullingController?.MarkDirty();
 1674        }
 75
 76        private void EnsureBlockerPool()
 77        {
 78            // We need to manually create the Pool for empty game objects if it doesn't exist
 63979            if (PoolManager.i.ContainsPool(PARCEL_BLOCKER_POOL_NAME))
 80            {
 5781                PoolManager.i.RemovePool(PARCEL_BLOCKER_POOL_NAME);
 82            }
 83
 63984            GameObject go = Object.Instantiate(blockerPrefab);
 63985            Pool pool = PoolManager.i.AddPool(PARCEL_BLOCKER_POOL_NAME, go);
 63986            pool.persistent = true;
 63987            pool.ForcePrewarm();
 63988        }
 89
 128090        public void SetParent(Transform parent) { this.parent = parent; }
 91
 92        public void HideBlocker(Vector2Int coords, bool instant = false)
 93        {
 494            if (instant)
 95            {
 196                ReleaseBlocker(coords);
 197                return;
 98            }
 99
 3100            animationHandler.FadeOut(
 101                blockers[coords].gameObject,
 3102                () => ReleaseBlocker(coords)
 103            );
 3104        }
 105
 106        private void ReleaseBlocker(Vector2Int coords)
 107        {
 51108            if (!blockers.ContainsKey(coords))
 0109                return;
 110
 51111            blockers[coords].Release();
 51112            blockers.Remove(coords);
 51113        }
 114
 15115        public Dictionary<Vector2Int, IPoolableObject> GetBlockers() { return new Dictionary<Vector2Int, IPoolableObject
 116
 117        public void DestroyAllBlockers()
 118        {
 639119            var keys = blockers.Keys.ToArray();
 120
 1372121            for (var i = 0; i < keys.Length; i++)
 122            {
 47123                ReleaseBlocker(keys[i]);
 124            }
 125
 639126            blockers.Clear();
 639127        }
 128    }
 129}