< Summary

Class:DCLServices.MapRendererV2.MapLayers.Atlas.ParcelChunkAtlasController
Assembly:MapRendererV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/MapRendererV2/MapLayers/Atlas/ParcelAtlas/ParcelChunkAtlasController.cs
Covered lines:29
Uncovered lines:7
Coverable lines:36
Total lines:99
Line coverage:80.5% (29 of 36)
Covered branches:0
Total branches:0
Covered methods:6
Total methods:7
Method coverage:85.7% (6 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ParcelChunkAtlasController(...)0%110100%
Initialize()0%10.129076%
ClearCurrentChunks()0%2.152066.67%
DisposeImpl()0%110100%
Enable(...)0%2100%
Disable(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/MapRendererV2/MapLayers/Atlas/ParcelAtlas/ParcelChunkAtlasController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCLServices.MapRendererV2.CoordsUtils;
 3using DCLServices.MapRendererV2.Culling;
 4using DCLServices.MapRendererV2.MapCameraController;
 5using System;
 6using System.Collections.Generic;
 7using System.Threading;
 8using UnityEngine;
 9
 10namespace DCLServices.MapRendererV2.MapLayers.Atlas
 11{
 12    internal class ParcelChunkAtlasController : MapLayerControllerBase, IAtlasController
 13    {
 14        public delegate UniTask<IChunkController> ChunkBuilder(Vector3 chunkLocalPosition, Vector2Int coordsCenter, Tran
 15
 16        public const int CHUNKS_CREATED_PER_BATCH = 5;
 17
 18        private readonly int chunkSize;
 19        private readonly int parcelsInsideChunk;
 20        private readonly ChunkBuilder chunkBuilder;
 21        private readonly List<IChunkController> chunks;
 22
 138823        private int parcelSize => coordsUtils.ParcelSize;
 24
 25        public ParcelChunkAtlasController(Transform parent, int chunkSize,
 26            ICoordsUtils coordsUtils, IMapCullingController cullingController, ChunkBuilder chunkBuilder)
 9627            : base(parent, coordsUtils, cullingController)
 28        {
 9629            this.chunkSize = chunkSize;
 9630            this.chunkBuilder = chunkBuilder;
 31
 9632            var worldSize = ((Vector2)coordsUtils.WorldMaxCoords - coordsUtils.WorldMinCoords) * parcelSize;
 9633            var chunkAmounts = new Vector2Int(Mathf.CeilToInt(worldSize.x / this.chunkSize), Mathf.CeilToInt(worldSize.y
 9634            chunks = new List<IChunkController>(chunkAmounts.x * chunkAmounts.y);
 9635            parcelsInsideChunk = Mathf.Max(1, chunkSize / parcelSize);
 9636        }
 37
 38        public async UniTask Initialize(CancellationToken ct)
 39        {
 9640            var linkedCt = CancellationTokenSource.CreateLinkedTokenSource(ctsDisposing.Token, ct).Token;
 41
 9642            ClearCurrentChunks();
 9643            float halfParcelSize = parcelSize * 0.5f;
 44
 9645            List<UniTask<IChunkController>> chunksCreating = new List<UniTask<IChunkController>>(CHUNKS_CREATED_PER_BATC
 46
 22047            for (int i = coordsUtils.WorldMinCoords.x; i <= coordsUtils.WorldMaxCoords.x; i += parcelsInsideChunk)
 48            {
 132049                for (int j = coordsUtils.WorldMinCoords.y; j <= coordsUtils.WorldMaxCoords.y; j += parcelsInsideChunk)
 50                {
 64651                    if (chunksCreating.Count >= CHUNKS_CREATED_PER_BATCH)
 52                    {
 33053                        chunks.AddRange(await UniTask.WhenAll(chunksCreating));
 1454                        chunksCreating.Clear();
 55                    }
 56
 55057                    Vector2Int coordsCenter = new Vector2Int(i, j);
 58
 59                    // Subtract half parcel size to displace the pivot, this allow easier PositionToCoords calculations.
 55060                    Vector3 localPosition = new Vector3((parcelSize * i) - halfParcelSize, (parcelSize * j) - halfParcel
 61
 55062                    var instance = chunkBuilder.Invoke(chunkLocalPosition: localPosition, coordsCenter, instantiationPar
 55063                    chunksCreating.Add(instance);
 64                }
 65            }
 66
 067            if (chunksCreating.Count >= 0)
 68            {
 069                chunks.AddRange(await UniTask.WhenAll(chunksCreating));
 070                chunksCreating.Clear();
 71            }
 072        }
 73
 74        private void ClearCurrentChunks()
 75        {
 20476            foreach (IChunkController chunk in chunks)
 077                chunk.Dispose();
 78
 10279            chunks.Clear();
 10280        }
 81
 82        protected override void DisposeImpl()
 83        {
 684            ClearCurrentChunks();
 685        }
 86
 87        UniTask IMapLayerController.Enable(CancellationToken cancellationToken)
 88        {
 089            instantiationParent.gameObject.SetActive(true);
 090            return UniTask.CompletedTask;
 91        }
 92
 93        UniTask IMapLayerController.Disable(CancellationToken cancellationToken)
 94        {
 8895            instantiationParent.gameObject.SetActive(false);
 8896            return UniTask.CompletedTask;
 97        }
 98    }
 99}