| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCLServices.MapRendererV2.CoordsUtils; |
| | 3 | | using DCLServices.MapRendererV2.Culling; |
| | 4 | | using DCLServices.MapRendererV2.MapCameraController; |
| | 5 | | using MainScripts.DCL.Helpers.Utils; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Threading; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.Pool; |
| | 11 | |
|
| | 12 | | namespace DCLServices.MapRendererV2.MapLayers.UsersMarkers.HotArea |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Updates players' positions within the comms radius |
| | 16 | | /// </summary> |
| | 17 | | internal class UsersMarkersHotAreaController : MapLayerControllerBase, IMapLayerController |
| | 18 | | { |
| | 19 | | internal const int PREWARM_PER_FRAME = 20; |
| | 20 | |
|
| | 21 | | private readonly IUnityObjectPool<HotUserMarkerObject> objectsPool; |
| | 22 | | private readonly IObjectPool<IHotUserMarker> wrapsPool; |
| | 23 | |
|
| | 24 | | private readonly BaseDictionary<string, Player> otherPlayers; |
| | 25 | |
|
| | 26 | | private readonly int prewarmCount; |
| | 27 | |
|
| 96 | 28 | | private readonly Dictionary<string, IHotUserMarker> markers = new (); |
| | 29 | |
|
| 0 | 30 | | internal IReadOnlyDictionary<string, IHotUserMarker> Markers => markers; |
| | 31 | |
|
| | 32 | | public UsersMarkersHotAreaController(BaseDictionary<string, Player> otherPlayers, |
| | 33 | | IUnityObjectPool<HotUserMarkerObject> objectsPool, IObjectPool<IHotUserMarker> wrapsPool, |
| | 34 | | int prewarmCount, Transform parent, ICoordsUtils coordsUtils, IMapCullingController cullingController) |
| 96 | 35 | | : base(parent, coordsUtils, cullingController) |
| | 36 | | { |
| 96 | 37 | | this.otherPlayers = otherPlayers; |
| 96 | 38 | | this.objectsPool = objectsPool; |
| 96 | 39 | | this.prewarmCount = prewarmCount; |
| | 40 | |
|
| 96 | 41 | | this.wrapsPool = wrapsPool; |
| 96 | 42 | | } |
| | 43 | |
|
| | 44 | | public UniTask Initialize(CancellationToken cancellationToken) |
| | 45 | | { |
| 96 | 46 | | wrapsPool.Prewarm(prewarmCount); |
| 96 | 47 | | return objectsPool.PrewarmAsync(prewarmCount, PREWARM_PER_FRAME, LinkWithDisposeToken(cancellationToken).Tok |
| | 48 | | } |
| | 49 | |
|
| | 50 | | protected override void DisposeImpl() |
| | 51 | | { |
| 6 | 52 | | objectsPool.Clear(); |
| 6 | 53 | | wrapsPool.Clear(); |
| | 54 | |
|
| 6 | 55 | | otherPlayers.OnAdded -= OnOtherPlayerAdded; |
| 6 | 56 | | otherPlayers.OnRemoved -= OnOtherPlayerRemoved; |
| 6 | 57 | | } |
| | 58 | |
|
| | 59 | | private void OnOtherPlayerAdded(string id, Player player) |
| | 60 | | { |
| 0 | 61 | | var wrap = wrapsPool.Get(); |
| 0 | 62 | | wrap.TrackPlayer(player); |
| 0 | 63 | | markers.Add(id, wrap); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | private void OnOtherPlayerRemoved(string id, Player player) |
| | 67 | | { |
| 0 | 68 | | if (markers.TryGetValue(id, out var marker)) |
| | 69 | | { |
| 0 | 70 | | wrapsPool.Release(marker); |
| 0 | 71 | | markers.Remove(id); |
| | 72 | | } |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public UniTask Enable(CancellationToken cancellationToken) |
| | 76 | | { |
| 0 | 77 | | foreach (var pair in otherPlayers.Get()) |
| 0 | 78 | | OnOtherPlayerAdded(pair.Key, pair.Value); |
| | 79 | |
|
| 0 | 80 | | otherPlayers.OnAdded += OnOtherPlayerAdded; |
| 0 | 81 | | otherPlayers.OnRemoved += OnOtherPlayerRemoved; |
| 0 | 82 | | return UniTask.CompletedTask; |
| | 83 | | } |
| | 84 | |
|
| | 85 | | public UniTask Disable(CancellationToken cancellationToken) |
| | 86 | | { |
| 88 | 87 | | otherPlayers.OnAdded -= OnOtherPlayerAdded; |
| 88 | 88 | | otherPlayers.OnRemoved -= OnOtherPlayerRemoved; |
| | 89 | |
|
| 176 | 90 | | foreach (IHotUserMarker marker in markers.Values) |
| 0 | 91 | | wrapsPool.Release(marker); |
| | 92 | |
|
| 88 | 93 | | markers.Clear(); |
| 88 | 94 | | return UniTask.CompletedTask; |
| | 95 | | } |
| | 96 | | } |
| | 97 | | } |