< Summary

Class:DCL.Controllers.BlockerInstanceHandler
Assembly:WorldBlockersController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/BlockerController/BlockerHandler.cs
Covered lines:55
Uncovered lines:1
Coverable lines:56
Total lines:144
Line coverage:98.2% (55 of 56)
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%
SetCollision(...)0%220100%
DestroyAllBlockers()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/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
 67428        Dictionary<Vector2Int, IPoolableObject> blockers = new Dictionary<Vector2Int, IPoolableObject>();
 29
 30        private IBlockerAnimationHandler animationHandler;
 31        private Transform parent;
 32
 67433        public BlockerInstanceHandler(IBlockerAnimationHandler animationHandler)
 34        {
 67435            this.animationHandler = animationHandler;
 67436            blockerPrefab = Resources.Load<GameObject>("LoadingBlocker_Green");
 67437            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
 67441        }
 42
 43        public void ShowBlocker(Vector2Int pos, bool instant = false, bool colliderEnabled = true)
 44        {
 7045            float centerOffset = ParcelSettings.PARCEL_SIZE / 2;
 7046            PoolableObject blockerPoolable = PoolManager.i.Get(PARCEL_BLOCKER_POOL_NAME);
 7047            GameObject blockerGo = blockerPoolable.gameObject;
 7048            BoxCollider blockerCollider = blockerGo.GetComponent<BoxCollider>();
 49
 7050            Vector3 blockerPos = PositionUtils.WorldToUnityPosition(Utils.GridToWorldPosition(pos.x, pos.y));
 51
 7052            auxPosVec.x = blockerPos.x + centerOffset;
 7053            auxPosVec.z = blockerPos.z + centerOffset;
 7054            auxPosVec.y = 8;
 55
 7056            Transform blockerTransform = blockerGo.transform;
 7057            blockerTransform.SetParent(parent, false);
 7058            blockerTransform.position = auxPosVec;
 7059            blockerTransform.localScale = Vector3.one * 16;
 60
 7061            blockerCollider.size = Vector3.one + (Vector3.up * auxScaleVec.y);
 7062            blockerCollider.center = Vector3.up * ((auxScaleVec.y / 2) - 0.5f);
 7063            blockerCollider.enabled = colliderEnabled;
 64
 65#if UNITY_EDITOR
 7066            blockerGo.name = "BLOCKER " + pos;
 67#endif
 68
 7069            blockers.Add(pos, blockerPoolable);
 70
 7071            if (!instant)
 6372                animationHandler.FadeIn(blockerGo);
 73
 7074            DCL.Environment.i.platform.cullingController?.MarkDirty();
 3275        }
 76
 77        private void EnsureBlockerPool()
 78        {
 79            // We need to manually create the Pool for empty game objects if it doesn't exist
 67480            if (PoolManager.i.ContainsPool(PARCEL_BLOCKER_POOL_NAME))
 81            {
 9282                PoolManager.i.RemovePool(PARCEL_BLOCKER_POOL_NAME);
 83            }
 84
 67485            GameObject go = Object.Instantiate(blockerPrefab);
 67486            Pool pool = PoolManager.i.AddPool(PARCEL_BLOCKER_POOL_NAME, go);
 67487            pool.persistent = true;
 67488            pool.ForcePrewarm();
 67489        }
 90
 91        public void SetParent(Transform parent)
 92        {
 67593            this.parent = parent;
 67594        }
 95
 96        public void HideBlocker(Vector2Int coords, bool instant = false)
 97        {
 498            if (instant)
 99            {
 1100                ReleaseBlocker(coords);
 1101                return;
 102            }
 103
 3104            animationHandler.FadeOut(
 105                blockers[coords].gameObject,
 3106                () => ReleaseBlocker(coords)
 107            );
 3108        }
 109
 110        private void ReleaseBlocker(Vector2Int coords)
 111        {
 67112            if (!blockers.ContainsKey(coords))
 0113                return;
 114
 67115            blockers[coords].Release();
 67116            blockers.Remove(coords);
 67117        }
 118
 119        public Dictionary<Vector2Int, IPoolableObject> GetBlockers()
 120        {
 18121            return new Dictionary<Vector2Int, IPoolableObject>(blockers);
 122        }
 123
 124        public void SetCollision(bool newState)
 125        {
 1570126            foreach (var keyValuePair in blockers)
 127            {
 32128                keyValuePair.Value.gameObject.GetComponent<Collider>().enabled = newState;
 129            }
 753130        }
 131
 132        public void DestroyAllBlockers()
 133        {
 674134            var keys = blockers.Keys.ToArray();
 135
 1474136            for (var i = 0; i < keys.Length; i++)
 137            {
 63138                ReleaseBlocker(keys[i]);
 139            }
 140
 674141            blockers.Clear();
 674142        }
 143    }
 144}