< 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
 67529        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
 67541        public BlockerInstanceHandler()
 42        {
 67543            RenderProfileManifest.i.OnChangeProfile += OnChangeProfile;
 67544            OnChangeProfile(RenderProfileManifest.i.currentProfile);
 67545        }
 46
 47        private void OnChangeProfile(RenderProfileWorld profile)
 48        {
 859149            if (profile == null)
 050                return;
 51
 859152            blockerPrefabDirty = true;
 859153            blockerPrefab = profile.loadingBlockerPrefab;
 859154        }
 55
 56        public void ShowBlocker(Vector2Int pos, bool instant = false)
 57        {
 6258            if (blockerPrefabDirty)
 59            {
 1160                blockerPrefabDirty = false;
 1161                EnsureBlockerPool();
 62            }
 63
 6264            float centerOffset = ParcelSettings.PARCEL_SIZE / 2;
 6265            PoolableObject blockerPoolable = PoolManager.i.Get(PARCEL_BLOCKER_POOL_NAME);
 6266            GameObject blockerGo = blockerPoolable.gameObject;
 6267            BoxCollider blockerCollider = blockerGo.GetComponent<BoxCollider>();
 68
 6269            Vector3 blockerPos = PositionUtils.WorldToUnityPosition(Utils.GridToWorldPosition(pos.x, pos.y));
 70
 6271            auxPosVec.x = blockerPos.x + centerOffset;
 6272            auxPosVec.z = blockerPos.z + centerOffset;
 6273            auxPosVec.y = 8;
 74
 6275            Transform blockerTransform = blockerGo.transform;
 6276            blockerTransform.SetParent(parent, false);
 6277            blockerTransform.position = auxPosVec;
 6278            blockerTransform.localScale = Vector3.one * 16;
 79
 6280            blockerCollider.size = Vector3.one + (Vector3.up * auxScaleVec.y);
 6281            blockerCollider.center = Vector3.up * ((auxScaleVec.y / 2) - 0.5f);
 82
 83#if UNITY_EDITOR
 6284            blockerGo.name = "BLOCKER " + pos;
 85#endif
 86
 6287            blockers.Add(pos, blockerPoolable);
 88
 6289            if (!instant)
 5590                animationHandler.FadeIn(blockerGo);
 91
 6292            cullingController?.MarkDirty();
 2493        }
 94
 95        private void EnsureBlockerPool()
 96        {
 97            // We need to manually create the Pool for empty game objects if it doesn't exist
 1198            if (PoolManager.i.ContainsPool(PARCEL_BLOCKER_POOL_NAME))
 99            {
 7100                PoolManager.i.RemovePool(PARCEL_BLOCKER_POOL_NAME);
 101            }
 102
 11103            GameObject go = Object.Instantiate(blockerPrefab);
 11104            Pool pool = PoolManager.i.AddPool(PARCEL_BLOCKER_POOL_NAME, go);
 11105            pool.persistent = true;
 11106            pool.ForcePrewarm();
 11107        }
 108
 1358109        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
 16134        public Dictionary<Vector2Int, IPoolableObject> GetBlockers() { return new Dictionary<Vector2Int, IPoolableObject
 135
 136        public void DestroyAllBlockers()
 137        {
 713138            var keys = blockers.Keys.ToArray();
 139
 1520140            for (var i = 0; i < keys.Length; i++)
 141            {
 47142                ReleaseBlocker(keys[i]);
 143            }
 144
 713145            blockers.Clear();
 713146        }
 147    }
 148}