< 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:53
Uncovered lines:3
Coverable lines:56
Total lines:144
Line coverage:94.6% (53 of 56)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BlockerInstanceHandler(...)0%110100%
ShowBlocker(...)0%33095.24%
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/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
 50228        Dictionary<Vector2Int, IPoolableObject> blockers = new Dictionary<Vector2Int, IPoolableObject>();
 29
 30        private IBlockerAnimationHandler animationHandler;
 31        private Transform parent;
 32
 50233        public BlockerInstanceHandler(IBlockerAnimationHandler animationHandler)
 34        {
 50235            this.animationHandler = animationHandler;
 50236            blockerPrefab = Resources.Load<GameObject>("LoadingBlocker_Green");
 50237            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
 50241        }
 42
 43        public void ShowBlocker(Vector2Int pos, bool instant = false, bool colliderEnabled = true)
 44        {
 3845            float centerOffset = ParcelSettings.PARCEL_SIZE / 2;
 3846            PoolableObject blockerPoolable = PoolManager.i.Get(PARCEL_BLOCKER_POOL_NAME);
 3847            GameObject blockerGo = blockerPoolable.gameObject;
 3848            BoxCollider blockerCollider = blockerGo.GetComponent<BoxCollider>();
 49
 3850            Vector3 blockerPos = PositionUtils.WorldToUnityPosition(Utils.GridToWorldPosition(pos.x, pos.y));
 51
 3852            auxPosVec.x = blockerPos.x + centerOffset;
 3853            auxPosVec.z = blockerPos.z + centerOffset;
 3854            auxPosVec.y = 8;
 55
 3856            Transform blockerTransform = blockerGo.transform;
 3857            blockerTransform.SetParent(parent, false);
 3858            blockerTransform.position = auxPosVec;
 3859            blockerTransform.localScale = Vector3.one * 16;
 60
 3861            blockerCollider.size = Vector3.one + (Vector3.up * auxScaleVec.y);
 3862            blockerCollider.center = Vector3.up * ((auxScaleVec.y / 2) - 0.5f);
 3863            blockerCollider.enabled = colliderEnabled;
 64
 65#if UNITY_EDITOR
 3866            blockerGo.name = "BLOCKER " + pos;
 67#endif
 68
 3869            blockers.Add(pos, blockerPoolable);
 70
 3871            if (!instant)
 3172                animationHandler.FadeIn(blockerGo);
 73
 3874            DCL.Environment.i.platform.cullingController?.MarkDirty();
 075        }
 76
 77        private void EnsureBlockerPool()
 78        {
 79            // We need to manually create the Pool for empty game objects if it doesn't exist
 50280            if (PoolManager.i.ContainsPool(PARCEL_BLOCKER_POOL_NAME))
 81            {
 10582                PoolManager.i.RemovePool(PARCEL_BLOCKER_POOL_NAME);
 83            }
 84
 50285            GameObject go = Object.Instantiate(blockerPrefab);
 50286            Pool pool = PoolManager.i.AddPool(PARCEL_BLOCKER_POOL_NAME, go);
 50287            pool.persistent = true;
 50288            pool.ForcePrewarm();
 50289        }
 90
 91        public void SetParent(Transform parent)
 92        {
 50393            this.parent = parent;
 50394        }
 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        {
 35112            if (!blockers.ContainsKey(coords))
 0113                return;
 114
 35115            blockers[coords].Release();
 35116            blockers.Remove(coords);
 35117        }
 118
 119        public Dictionary<Vector2Int, IPoolableObject> GetBlockers()
 120        {
 22121            return new Dictionary<Vector2Int, IPoolableObject>(blockers);
 122        }
 123
 124        public void SetCollision(bool newState)
 125        {
 1150126            foreach (var keyValuePair in blockers)
 127            {
 0128                keyValuePair.Value.gameObject.GetComponent<Collider>().enabled = newState;
 129            }
 575130        }
 131
 132        public void DestroyAllBlockers()
 133        {
 497134            var keys = blockers.Keys.ToArray();
 135
 1056136            for (var i = 0; i < keys.Length; i++)
 137            {
 31138                ReleaseBlocker(keys[i]);
 139            }
 140
 497141            blockers.Clear();
 497142        }
 143    }
 144}