| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCLServices.MapRendererV2.CoordsUtils; |
| | 3 | | using DCLServices.MapRendererV2.Culling; |
| | 4 | | using DCLServices.MapRendererV2.MapCameraController; |
| | 5 | | using DCLServices.MapRendererV2.MapLayers.Atlas; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Threading; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | namespace DCLServices.MapRendererV2.MapLayers.SatelliteAtlas |
| | 12 | | { |
| | 13 | | internal class SatelliteChunkAtlasController : MapLayerControllerBase, IAtlasController |
| | 14 | | { |
| | 15 | | public delegate UniTask<IChunkController> ChunkBuilder(Vector3 chunkLocalPosition, Vector2Int coordsCenter, Tran |
| | 16 | |
|
| | 17 | | private const int CHUNKS_CREATED_PER_BATCH = 5; |
| | 18 | |
|
| | 19 | | private readonly int gridSize; |
| | 20 | | private readonly int parcelsInsideChunk; |
| | 21 | |
|
| | 22 | | private readonly ChunkBuilder chunkBuilder; |
| | 23 | | private readonly List<IChunkController> chunks; |
| | 24 | |
|
| | 25 | | public SatelliteChunkAtlasController(Transform parent, int gridSize, int parcelsInsideChunk, ICoordsUtils coords |
| 96 | 26 | | : base(parent, coordsUtils, cullingController) |
| | 27 | | { |
| 96 | 28 | | this.gridSize = gridSize; |
| 96 | 29 | | this.parcelsInsideChunk = parcelsInsideChunk; |
| 96 | 30 | | this.chunkBuilder = chunkBuilder; |
| | 31 | |
|
| 96 | 32 | | var chunkAmounts = new Vector2Int(gridSize, gridSize); |
| 96 | 33 | | chunks = new List<IChunkController>(chunkAmounts.x * chunkAmounts.y); |
| 96 | 34 | | } |
| | 35 | |
|
| | 36 | | public async UniTask Initialize(CancellationToken ct) |
| | 37 | | { |
| 96 | 38 | | int chunkSpriteSize = parcelsInsideChunk * coordsUtils.ParcelSize; |
| 96 | 39 | | Vector3 offset = SatelliteMapOffset(); |
| | 40 | |
|
| 96 | 41 | | CancellationToken linkedCt = CancellationTokenSource.CreateLinkedTokenSource(ctsDisposing.Token, ct).Token; |
| | 42 | |
|
| 96 | 43 | | var chunksCreating = new List<UniTask<IChunkController>>(CHUNKS_CREATED_PER_BATCH); |
| | 44 | |
|
| 216 | 45 | | for (var i = 0; i < gridSize; i++) |
| | 46 | | { |
| 108 | 47 | | float x = offset.x + (chunkSpriteSize * i); |
| | 48 | |
|
| 1316 | 49 | | for (var j = 0; j < gridSize; j++) |
| | 50 | | { |
| 646 | 51 | | float y = offset.y - (chunkSpriteSize * j); |
| | 52 | |
|
| 646 | 53 | | if (chunksCreating.Count >= CHUNKS_CREATED_PER_BATCH) |
| | 54 | | { |
| 330 | 55 | | chunks.AddRange(await UniTask.WhenAll(chunksCreating)); |
| 14 | 56 | | chunksCreating.Clear(); |
| | 57 | | } |
| | 58 | |
|
| 550 | 59 | | var localPosition = new Vector3(x, y, 0); |
| | 60 | |
|
| 550 | 61 | | UniTask<IChunkController> instance = chunkBuilder.Invoke(chunkLocalPosition: localPosition, new Vect |
| 550 | 62 | | chunksCreating.Add(instance); |
| | 63 | | } |
| | 64 | | } |
| | 65 | |
|
| 0 | 66 | | if (chunksCreating.Count >= 0) |
| | 67 | | { |
| 0 | 68 | | chunks.AddRange(await UniTask.WhenAll(chunksCreating)); |
| 0 | 69 | | chunksCreating.Clear(); |
| | 70 | | } |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | private Vector3 SatelliteMapOffset() |
| | 74 | | { |
| | 75 | | // World minimum plus half size of the chunk to get position (in parcels units) for the center of first chun |
| 96 | 76 | | Vector2Int topLeftCornerChunkCenter = coordsUtils.WorldMinCoords + new Vector2Int(parcelsInsideChunk / 2, pa |
| | 77 | | // offset by (3,2) parcels because Satellite image has border parcels outside of the world |
| 96 | 78 | | topLeftCornerChunkCenter = new Vector2Int(topLeftCornerChunkCenter.x - 3, Math.Abs(topLeftCornerChunkCenter. |
| | 79 | |
|
| 96 | 80 | | return coordsUtils.CoordsToPosition(topLeftCornerChunkCenter); |
| | 81 | | } |
| | 82 | |
|
| | 83 | | UniTask IMapLayerController.Enable(CancellationToken cancellationToken) |
| | 84 | | { |
| 0 | 85 | | instantiationParent.gameObject.SetActive(true); |
| 0 | 86 | | return UniTask.CompletedTask; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | UniTask IMapLayerController.Disable(CancellationToken cancellationToken) |
| | 90 | | { |
| 88 | 91 | | instantiationParent.gameObject.SetActive(false); |
| 88 | 92 | | return UniTask.CompletedTask; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | protected override void DisposeImpl() |
| | 96 | | { |
| 12 | 97 | | foreach (IChunkController chunk in chunks) |
| 0 | 98 | | chunk.Dispose(); |
| | 99 | |
|
| 6 | 100 | | chunks.Clear(); |
| 6 | 101 | | } |
| | 102 | | } |
| | 103 | | } |