< 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:56
Uncovered lines:5
Coverable lines:61
Total lines:148
Line coverage:91.8% (56 of 61)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BlockerInstanceHandler()0%110100%
Initialize(...)0%2100%
OnChangeProfile(...)0%2.032080%
ShowBlocker(...)0%440100%
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        private bool blockerPrefabDirty;
 23
 24        const string PARCEL_BLOCKER_POOL_NAME = "ParcelBlocker";
 25
 26        Vector3 auxPosVec = new Vector3();
 27        Vector3 auxScaleVec = new Vector3();
 28
 65729        Dictionary<Vector2Int, IPoolableObject> blockers = new Dictionary<Vector2Int, IPoolableObject>();
 30
 31        private IBlockerAnimationHandler animationHandler;
 32        private ICullingController cullingController;
 33        private Transform parent;
 34
 35        public void Initialize(IBlockerAnimationHandler animationHandler, ICullingController cullingController)
 36        {
 037            this.cullingController = cullingController;
 038            this.animationHandler = animationHandler;
 039        }
 40
 65741        public BlockerInstanceHandler()
 42        {
 65743            RenderProfileManifest.i.OnChangeProfile += OnChangeProfile;
 65744            OnChangeProfile(RenderProfileManifest.i.currentProfile);
 65745        }
 46
 47        private void OnChangeProfile(RenderProfileWorld profile)
 48        {
 1823149            if (profile == null)
 050                return;
 51
 1823152            blockerPrefabDirty = true;
 1823153            blockerPrefab = profile.loadingBlockerPrefab;
 1823154        }
 55
 56        public void ShowBlocker(Vector2Int pos, bool instant = false)
 57        {
 5458            if (blockerPrefabDirty)
 59            {
 1060                blockerPrefabDirty = false;
 1061                EnsureBlockerPool();
 62            }
 63
 5464            float centerOffset = ParcelSettings.PARCEL_SIZE / 2;
 5465            PoolableObject blockerPoolable = PoolManager.i.Get(PARCEL_BLOCKER_POOL_NAME);
 5466            GameObject blockerGo = blockerPoolable.gameObject;
 5467            BoxCollider blockerCollider = blockerGo.GetComponent<BoxCollider>();
 68
 5469            Vector3 blockerPos = PositionUtils.WorldToUnityPosition(Utils.GridToWorldPosition(pos.x, pos.y));
 70
 5471            auxPosVec.x = blockerPos.x + centerOffset;
 5472            auxPosVec.z = blockerPos.z + centerOffset;
 5473            auxPosVec.y = 8;
 74
 5475            Transform blockerTransform = blockerGo.transform;
 5476            blockerTransform.SetParent(parent, false);
 5477            blockerTransform.position = auxPosVec;
 5478            blockerTransform.localScale = Vector3.one * 16;
 79
 5480            blockerCollider.size = Vector3.one + (Vector3.up * auxScaleVec.y);
 5481            blockerCollider.center = Vector3.up * ((auxScaleVec.y / 2) - 0.5f);
 82
 83#if UNITY_EDITOR
 5484            blockerGo.name = "BLOCKER " + pos;
 85#endif
 86
 5487            blockers.Add(pos, blockerPoolable);
 88
 5489            if (!instant)
 4790                animationHandler.FadeIn(blockerGo);
 91
 5492            cullingController?.MarkDirty();
 1693        }
 94
 95        private void EnsureBlockerPool()
 96        {
 97            // We need to manually create the Pool for empty game objects if it doesn't exist
 1098            if (PoolManager.i.ContainsPool(PARCEL_BLOCKER_POOL_NAME))
 99            {
 7100                PoolManager.i.RemovePool(PARCEL_BLOCKER_POOL_NAME);
 101            }
 102
 10103            GameObject go = Object.Instantiate(blockerPrefab);
 10104            Pool pool = PoolManager.i.AddPool(PARCEL_BLOCKER_POOL_NAME, go);
 10105            pool.persistent = true;
 10106            pool.ForcePrewarm();
 10107        }
 108
 1322109        public void SetParent(Transform parent) { this.parent = parent; }
 110
 111        public void HideBlocker(Vector2Int coords, bool instant = false)
 112        {
 4113            if (instant)
 114            {
 1115                ReleaseBlocker(coords);
 1116                return;
 117            }
 118
 3119            animationHandler.FadeOut(
 120                blockers[coords].gameObject,
 3121                () => ReleaseBlocker(coords)
 122            );
 3123        }
 124
 125        private void ReleaseBlocker(Vector2Int coords)
 126        {
 51127            if (!blockers.ContainsKey(coords))
 0128                return;
 129
 51130            blockers[coords].Release();
 51131            blockers.Remove(coords);
 51132        }
 133
 15134        public Dictionary<Vector2Int, IPoolableObject> GetBlockers() { return new Dictionary<Vector2Int, IPoolableObject
 135
 136        public void DestroyAllBlockers()
 137        {
 662138            var keys = blockers.Keys.ToArray();
 139
 1418140            for (var i = 0; i < keys.Length; i++)
 141            {
 47142                ReleaseBlocker(keys[i]);
 143            }
 144
 662145            blockers.Clear();
 662146        }
 147    }
 148}