< Summary

Class:DCL.MapAtlas
Assembly:MapRenderer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/MapAtlas.cs
Covered lines:44
Uncovered lines:5
Coverable lines:49
Total lines:113
Line coverage:89.7% (44 of 49)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MapAtlas()0%110100%
Cleanup()0%440100%
GetChunk(...)0%6200%
CenterToTile(...)0%2.012085.71%
UpdateCulling()0%220100%
InitializeChunks()0%55096.88%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/MapAtlas.cs

#LineLine coverage
 1using DCL.Helpers;
 2using System.Collections.Generic;
 3using UnityEngine;
 4
 5namespace DCL
 6{
 7    public class MapAtlas : MonoBehaviour
 8    {
 9        public RectTransform viewport;
 10        public GameObject container;
 11        public RectTransform chunksParent;
 12        public GameObject overlayLayerGameobject;
 13
 1414        Dictionary<Vector2Int, MapChunk> chunks = new Dictionary<Vector2Int, MapChunk>();
 15
 16        public GameObject mapChunkPrefab;
 17        private bool chunksInitialized = false;
 18
 19        public void Cleanup()
 20        {
 120421            foreach (var kvp in chunks)
 22            {
 58823                if (kvp.Value != null && kvp.Value.gameObject != null)
 24                {
 58825                    Destroy(kvp.Value.gameObject);
 26                }
 27            }
 28
 1429            chunks.Clear();
 1430            chunksInitialized = false;
 1431        }
 32
 33        public MapChunk GetChunk(int x, int y)
 34        {
 035            if (chunks.TryGetValue(new Vector2Int(x, y), out MapChunk value))
 36            {
 037                return value;
 38            }
 39
 040            return null;
 41        }
 42
 43        public void CenterToTile(Vector2 tilePosition)
 44        {
 545            if (viewport == null)
 046                return;
 47
 548            Vector3 center = viewport.transform.TransformPoint(viewport.rect.center);
 549            Vector3 delta = center - container.transform.TransformPoint(MapUtils.GetTileToLocalPosition(tilePosition.x, 
 50
 551            container.transform.position += delta;
 552            UpdateCulling();
 553        }
 54
 55        [ContextMenu("Force Update Culling")]
 56        public void UpdateCulling()
 57        {
 558            using (var it = chunks.GetEnumerator())
 59            {
 25060                while (it.MoveNext())
 61                {
 24562                    it.Current.Value.UpdateCulling();
 63                }
 564            }
 565        }
 66
 67        public void InitializeChunks()
 68        {
 1269            if (chunksInitialized)
 070                return;
 71
 1272            chunksInitialized = true;
 1273            int tileCoverageX = MapUtils.CHUNK_SIZE.x / MapUtils.PARCEL_SIZE;
 1274            int tileCoverageY = MapUtils.CHUNK_SIZE.y / MapUtils.PARCEL_SIZE;
 75
 2476            int xTile = 0, yTile = 0;
 77
 19278            for (int x = MapUtils.WORLD_PARCELS_OFFSET_MIN.x; x <= MapUtils.WORLD_PARCELS_OFFSET_MAX.x; x += tileCoverag
 79            {
 134480                for (int y = MapUtils.WORLD_PARCELS_OFFSET_MIN.y; y <= MapUtils.WORLD_PARCELS_OFFSET_MAX.y; y += tileCov
 81                {
 58882                    var chunk = Object.Instantiate(mapChunkPrefab).GetComponent<MapChunk>();
 83
 84#if UNITY_EDITOR
 58885                    chunk.gameObject.name = $"Chunk {xTile}, {yTile}";
 86#endif
 58887                    if (!(chunk.transform is RectTransform chunkTransform))
 88                        continue;
 89
 58890                    chunkTransform.SetParent(chunksParent.transform);
 58891                    chunkTransform.localScale = Vector3.one;
 58892                    chunkTransform.localPosition = new Vector3(xTile * MapUtils.CHUNK_SIZE.x, yTile * MapUtils.CHUNK_SIZ
 93
 94                    //NOTE(Brian): Configure chunk with proper params
 58895                    chunk.center.x = x;
 58896                    chunk.center.y = y;
 58897                    chunk.size.x = MapUtils.CHUNK_SIZE.x;
 58898                    chunk.size.y = MapUtils.CHUNK_SIZE.y;
 58899                    chunk.tileSize = MapUtils.PARCEL_SIZE;
 588100                    chunkTransform.sizeDelta = new Vector2(MapUtils.CHUNK_SIZE.x, MapUtils.CHUNK_SIZE.y);
 588101                    chunk.owner = this;
 102
 588103                    chunks[new Vector2Int(xTile, yTile)] = chunk;
 588104                    yTile++;
 105                }
 106
 84107                xTile++;
 84108                yTile = 0;
 109            }
 12110            overlayLayerGameobject.transform.SetAsLastSibling();
 12111        }
 112    }
 113}