| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCLServices.MapRendererV2.CoordsUtils; |
| | 3 | | using DCLServices.MapRendererV2.Culling; |
| | 4 | | using DCLServices.MapRendererV2.MapCameraController; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace 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 | |
|
| 1388 | 23 | | private int parcelSize => coordsUtils.ParcelSize; |
| | 24 | |
|
| | 25 | | public ParcelChunkAtlasController(Transform parent, int chunkSize, |
| | 26 | | ICoordsUtils coordsUtils, IMapCullingController cullingController, ChunkBuilder chunkBuilder) |
| 96 | 27 | | : base(parent, coordsUtils, cullingController) |
| | 28 | | { |
| 96 | 29 | | this.chunkSize = chunkSize; |
| 96 | 30 | | this.chunkBuilder = chunkBuilder; |
| | 31 | |
|
| 96 | 32 | | var worldSize = ((Vector2)coordsUtils.WorldMaxCoords - coordsUtils.WorldMinCoords) * parcelSize; |
| 96 | 33 | | var chunkAmounts = new Vector2Int(Mathf.CeilToInt(worldSize.x / this.chunkSize), Mathf.CeilToInt(worldSize.y |
| 96 | 34 | | chunks = new List<IChunkController>(chunkAmounts.x * chunkAmounts.y); |
| 96 | 35 | | parcelsInsideChunk = Mathf.Max(1, chunkSize / parcelSize); |
| 96 | 36 | | } |
| | 37 | |
|
| | 38 | | public async UniTask Initialize(CancellationToken ct) |
| | 39 | | { |
| 96 | 40 | | var linkedCt = CancellationTokenSource.CreateLinkedTokenSource(ctsDisposing.Token, ct).Token; |
| | 41 | |
|
| 96 | 42 | | ClearCurrentChunks(); |
| 96 | 43 | | float halfParcelSize = parcelSize * 0.5f; |
| | 44 | |
|
| 96 | 45 | | List<UniTask<IChunkController>> chunksCreating = new List<UniTask<IChunkController>>(CHUNKS_CREATED_PER_BATC |
| | 46 | |
|
| 220 | 47 | | for (int i = coordsUtils.WorldMinCoords.x; i <= coordsUtils.WorldMaxCoords.x; i += parcelsInsideChunk) |
| | 48 | | { |
| 1320 | 49 | | for (int j = coordsUtils.WorldMinCoords.y; j <= coordsUtils.WorldMaxCoords.y; j += parcelsInsideChunk) |
| | 50 | | { |
| 646 | 51 | | if (chunksCreating.Count >= CHUNKS_CREATED_PER_BATCH) |
| | 52 | | { |
| 330 | 53 | | chunks.AddRange(await UniTask.WhenAll(chunksCreating)); |
| 14 | 54 | | chunksCreating.Clear(); |
| | 55 | | } |
| | 56 | |
|
| 550 | 57 | | Vector2Int coordsCenter = new Vector2Int(i, j); |
| | 58 | |
|
| | 59 | | // Subtract half parcel size to displace the pivot, this allow easier PositionToCoords calculations. |
| 550 | 60 | | Vector3 localPosition = new Vector3((parcelSize * i) - halfParcelSize, (parcelSize * j) - halfParcel |
| | 61 | |
|
| 550 | 62 | | var instance = chunkBuilder.Invoke(chunkLocalPosition: localPosition, coordsCenter, instantiationPar |
| 550 | 63 | | chunksCreating.Add(instance); |
| | 64 | | } |
| | 65 | | } |
| | 66 | |
|
| 0 | 67 | | if (chunksCreating.Count >= 0) |
| | 68 | | { |
| 0 | 69 | | chunks.AddRange(await UniTask.WhenAll(chunksCreating)); |
| 0 | 70 | | chunksCreating.Clear(); |
| | 71 | | } |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | private void ClearCurrentChunks() |
| | 75 | | { |
| 204 | 76 | | foreach (IChunkController chunk in chunks) |
| 0 | 77 | | chunk.Dispose(); |
| | 78 | |
|
| 102 | 79 | | chunks.Clear(); |
| 102 | 80 | | } |
| | 81 | |
|
| | 82 | | protected override void DisposeImpl() |
| | 83 | | { |
| 6 | 84 | | ClearCurrentChunks(); |
| 6 | 85 | | } |
| | 86 | |
|
| | 87 | | UniTask IMapLayerController.Enable(CancellationToken cancellationToken) |
| | 88 | | { |
| 0 | 89 | | instantiationParent.gameObject.SetActive(true); |
| 0 | 90 | | return UniTask.CompletedTask; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | UniTask IMapLayerController.Disable(CancellationToken cancellationToken) |
| | 94 | | { |
| 88 | 95 | | instantiationParent.gameObject.SetActive(false); |
| 88 | 96 | | return UniTask.CompletedTask; |
| | 97 | | } |
| | 98 | | } |
| | 99 | | } |