< Summary

Class:DCL.MapAtlas
Assembly:MapRenderer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/MapAtlas.cs
Covered lines:0
Uncovered lines:48
Coverable lines:48
Total lines:109
Line coverage:0% (0 of 48)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:6
Method coverage:0% (0 of 6)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MapAtlas()0%2100%
Cleanup()0%20400%
GetChunk(...)0%6200%
CenterToTile(...)0%6200%
UpdateCulling()0%6200%
InitializeChunks()0%30500%

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using DCL.Helpers;
 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
 014        Dictionary<Vector2Int, MapChunk> chunks = new Dictionary<Vector2Int, MapChunk>();
 15
 16        public GameObject mapChunkPrefab;
 17        private bool chunksInitialized = false;
 18
 19        public void Cleanup()
 20        {
 021            foreach (var kvp in chunks)
 22            {
 023                if (kvp.Value != null && kvp.Value.gameObject != null)
 24                {
 025                    Destroy(kvp.Value.gameObject);
 26                }
 27            }
 28
 029            chunks.Clear();
 030            chunksInitialized = false;
 031        }
 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        {
 045            if (viewport == null)
 046                return;
 47
 048            Vector3 center = viewport.transform.TransformPoint(viewport.rect.center);
 049            Vector3 delta = center - container.transform.TransformPoint(MapUtils.GetTileToLocalPosition(tilePosition.x, 
 50
 051            container.transform.position += delta;
 052            UpdateCulling();
 053        }
 54
 55        [ContextMenu("Force Update Culling")]
 56        public void UpdateCulling()
 57        {
 058            using var it = chunks.GetEnumerator();
 059            while (it.MoveNext())
 060                it.Current.Value.UpdateCulling();
 061        }
 62
 63        public void InitializeChunks()
 64        {
 065            if (chunksInitialized)
 066                return;
 67
 068            chunksInitialized = true;
 069            int tileCoverageX = MapUtils.CHUNK_SIZE.x / MapUtils.PARCEL_SIZE;
 070            int tileCoverageY = MapUtils.CHUNK_SIZE.y / MapUtils.PARCEL_SIZE;
 71
 072            int xTile = 0, yTile = 0;
 73
 074            for (int x = MapUtils.WORLD_PARCELS_OFFSET_MIN.x; x <= MapUtils.WORLD_PARCELS_OFFSET_MAX.x; x += tileCoverag
 75            {
 076                for (int y = MapUtils.WORLD_PARCELS_OFFSET_MIN.y; y <= MapUtils.WORLD_PARCELS_OFFSET_MAX.y; y += tileCov
 77                {
 078                    var chunk = Object.Instantiate(mapChunkPrefab).GetComponent<MapChunk>();
 79
 80#if UNITY_EDITOR
 081                    chunk.gameObject.name = $"Chunk {xTile}, {yTile}";
 82#endif
 083                    if (!(chunk.transform is RectTransform chunkTransform))
 84                        continue;
 85
 086                    chunkTransform.SetParent(chunksParent.transform);
 087                    chunkTransform.localScale = Vector3.one;
 088                    chunkTransform.localPosition = new Vector3(xTile * MapUtils.CHUNK_SIZE.x, yTile * MapUtils.CHUNK_SIZ
 89
 90                    //NOTE(Brian): Configure chunk with proper params
 091                    chunk.center.x = x;
 092                    chunk.center.y = y;
 093                    chunk.size.x = MapUtils.CHUNK_SIZE.x;
 094                    chunk.size.y = MapUtils.CHUNK_SIZE.y;
 095                    chunk.tileSize = MapUtils.PARCEL_SIZE;
 096                    chunkTransform.sizeDelta = new Vector2(MapUtils.CHUNK_SIZE.x, MapUtils.CHUNK_SIZE.y);
 097                    chunk.owner = this;
 98
 099                    chunks[new Vector2Int(xTile, yTile)] = chunk;
 0100                    yTile++;
 101                }
 102
 0103                xTile++;
 0104                yTile = 0;
 105            }
 0106            overlayLayerGameobject.transform.SetAsLastSibling();
 0107        }
 108    }
 109}