< 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:53
Uncovered lines:2
Coverable lines:55
Total lines:138
Line coverage:96.3% (53 of 55)
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%2.262060%
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
 63828        Dictionary<Vector2Int, IPoolableObject> blockers = new Dictionary<Vector2Int, IPoolableObject>();
 29
 30        private IBlockerAnimationHandler animationHandler;
 31        private Transform parent;
 32
 63833        public BlockerInstanceHandler(IBlockerAnimationHandler animationHandler)
 34        {
 63835            this.animationHandler = animationHandler;
 63836            blockerPrefab = Resources.Load<GameObject>("LoadingBlocker_Green");
 63837            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
 63841        }
 42
 43        public void ShowBlocker(Vector2Int pos, bool instant = false, bool colliderEnabled = true)
 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);
 5463            blockerCollider.enabled = colliderEnabled;
 64
 65#if UNITY_EDITOR
 5466            blockerGo.name = "BLOCKER " + pos;
 67#endif
 68
 5469            blockers.Add(pos, blockerPoolable);
 70
 5471            if (!instant)
 4772                animationHandler.FadeIn(blockerGo);
 73
 5474            DCL.Environment.i.platform.cullingController?.MarkDirty();
 1675        }
 76
 77        private void EnsureBlockerPool()
 78        {
 79            // We need to manually create the Pool for empty game objects if it doesn't exist
 63880            if (PoolManager.i.ContainsPool(PARCEL_BLOCKER_POOL_NAME))
 81            {
 5782                PoolManager.i.RemovePool(PARCEL_BLOCKER_POOL_NAME);
 83            }
 84
 63885            GameObject go = Object.Instantiate(blockerPrefab);
 63886            Pool pool = PoolManager.i.AddPool(PARCEL_BLOCKER_POOL_NAME, go);
 63887            pool.persistent = true;
 63888            pool.ForcePrewarm();
 63889        }
 90
 127891        public void SetParent(Transform parent) { this.parent = parent; }
 92
 93        public void HideBlocker(Vector2Int coords, bool instant = false)
 94        {
 495            if (instant)
 96            {
 197                ReleaseBlocker(coords);
 198                return;
 99            }
 100
 3101            animationHandler.FadeOut(
 102                blockers[coords].gameObject,
 3103                () => ReleaseBlocker(coords)
 104            );
 3105        }
 106
 107        private void ReleaseBlocker(Vector2Int coords)
 108        {
 51109            if (!blockers.ContainsKey(coords))
 0110                return;
 111
 51112            blockers[coords].Release();
 51113            blockers.Remove(coords);
 51114        }
 115
 15116        public Dictionary<Vector2Int, IPoolableObject> GetBlockers() { return new Dictionary<Vector2Int, IPoolableObject
 117
 118        public void SetCollision(bool newState)
 119        {
 8204120            foreach (var keyValuePair in blockers)
 121            {
 0122                keyValuePair.Value.gameObject.GetComponent<Collider>().enabled = newState;
 123            }
 4102124        }
 125
 126        public void DestroyAllBlockers()
 127        {
 638128            var keys = blockers.Keys.ToArray();
 129
 1370130            for (var i = 0; i < keys.Length; i++)
 131            {
 47132                ReleaseBlocker(keys[i]);
 133            }
 134
 638135            blockers.Clear();
 638136        }
 137    }
 138}