| | 1 | | using DCL.Helpers; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | public class MapAtlas : MonoBehaviour |
| | 8 | | { |
| | 9 | | public RectTransform viewport; |
| | 10 | | public GameObject container; |
| | 11 | | public GameObject chunksParent; |
| | 12 | | public GameObject overlayLayerGameobject; |
| | 13 | |
|
| 12 | 14 | | Dictionary<Vector2Int, MapChunk> chunks = new Dictionary<Vector2Int, MapChunk>(); |
| | 15 | |
|
| | 16 | | public GameObject mapChunkPrefab; |
| | 17 | | private bool chunksInitialized = false; |
| | 18 | |
|
| | 19 | | public void Cleanup() |
| | 20 | | { |
| 1002 | 21 | | foreach (var kvp in chunks) |
| | 22 | | { |
| 490 | 23 | | if (kvp.Value != null && kvp.Value.gameObject != null) |
| | 24 | | { |
| 490 | 25 | | Destroy(kvp.Value.gameObject); |
| | 26 | | } |
| | 27 | | } |
| | 28 | |
|
| 11 | 29 | | chunks.Clear(); |
| 11 | 30 | | chunksInitialized = false; |
| 11 | 31 | | } |
| | 32 | |
|
| | 33 | | public MapChunk GetChunk(int x, int y) |
| | 34 | | { |
| 0 | 35 | | if (chunks.TryGetValue(new Vector2Int(x, y), out MapChunk value)) |
| | 36 | | { |
| 0 | 37 | | return value; |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | return null; |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public void CenterToTile(Vector2 tilePosition) |
| | 44 | | { |
| 0 | 45 | | if (viewport == null) |
| 0 | 46 | | return; |
| | 47 | |
|
| 0 | 48 | | Vector3 center = viewport.transform.TransformPoint(viewport.rect.center); |
| 0 | 49 | | Vector3 delta = center - container.transform.TransformPoint(MapUtils.GetTileToLocalPosition(tilePosition.x, |
| | 50 | |
|
| 0 | 51 | | container.transform.position += delta; |
| 0 | 52 | | UpdateCulling(); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | [ContextMenu("Force Update Culling")] |
| | 56 | | public void UpdateCulling() |
| | 57 | | { |
| 0 | 58 | | using (var it = chunks.GetEnumerator()) |
| | 59 | | { |
| 0 | 60 | | while (it.MoveNext()) |
| | 61 | | { |
| 0 | 62 | | it.Current.Value.UpdateCulling(); |
| | 63 | | } |
| 0 | 64 | | } |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | public void InitializeChunks() |
| | 68 | | { |
| 10 | 69 | | if (chunksInitialized) |
| 0 | 70 | | return; |
| | 71 | |
|
| 10 | 72 | | chunksInitialized = true; |
| 10 | 73 | | int tileCoverageX = MapUtils.CHUNK_SIZE.x / MapUtils.PARCEL_SIZE; |
| 10 | 74 | | int tileCoverageY = MapUtils.CHUNK_SIZE.y / MapUtils.PARCEL_SIZE; |
| | 75 | |
|
| 20 | 76 | | int xTile = 0, yTile = 0; |
| | 77 | |
|
| 160 | 78 | | for (int x = MapUtils.WORLD_PARCELS_OFFSET_MIN.x; x <= MapUtils.WORLD_PARCELS_OFFSET_MAX.x; x += tileCoverag |
| | 79 | | { |
| 1120 | 80 | | for (int y = MapUtils.WORLD_PARCELS_OFFSET_MIN.y; y <= MapUtils.WORLD_PARCELS_OFFSET_MAX.y; y += tileCov |
| | 81 | | { |
| 490 | 82 | | var chunk = Object.Instantiate(mapChunkPrefab).GetComponent<MapChunk>(); |
| | 83 | |
|
| | 84 | | #if UNITY_EDITOR |
| 490 | 85 | | chunk.gameObject.name = $"Chunk {xTile}, {yTile}"; |
| | 86 | | #endif |
| 490 | 87 | | if (!(chunk.transform is RectTransform chunkTransform)) |
| | 88 | | continue; |
| | 89 | |
|
| 490 | 90 | | chunkTransform.SetParent(chunksParent.transform); |
| 490 | 91 | | chunkTransform.localScale = Vector3.one; |
| 490 | 92 | | chunkTransform.localPosition = new Vector3(xTile * MapUtils.CHUNK_SIZE.x, yTile * MapUtils.CHUNK_SIZ |
| | 93 | |
|
| | 94 | | //NOTE(Brian): Configure chunk with proper params |
| 490 | 95 | | chunk.center.x = x; |
| 490 | 96 | | chunk.center.y = y; |
| 490 | 97 | | chunk.size.x = MapUtils.CHUNK_SIZE.x; |
| 490 | 98 | | chunk.size.y = MapUtils.CHUNK_SIZE.y; |
| 490 | 99 | | chunk.tileSize = MapUtils.PARCEL_SIZE; |
| 490 | 100 | | chunkTransform.sizeDelta = new Vector2(MapUtils.CHUNK_SIZE.x, MapUtils.CHUNK_SIZE.y); |
| 490 | 101 | | chunk.owner = this; |
| | 102 | |
|
| 490 | 103 | | chunks[new Vector2Int(xTile, yTile)] = chunk; |
| 490 | 104 | | yTile++; |
| | 105 | | } |
| | 106 | |
|
| 70 | 107 | | xTile++; |
| 70 | 108 | | yTile = 0; |
| | 109 | | } |
| 10 | 110 | | } |
| | 111 | | } |
| | 112 | | } |