| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCLServices.MapRendererV2.CommonBehavior; |
| | 4 | | using DCLServices.MapRendererV2.CoordsUtils; |
| | 5 | | using DCLServices.MapRendererV2.Culling; |
| | 6 | | using MainScripts.DCL.Helpers.Utils; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCLServices.MapRendererV2.MapLayers.UsersMarkers.HotArea |
| | 11 | | { |
| | 12 | | internal class HotUserMarker : IHotUserMarker |
| | 13 | | { |
| | 14 | | private readonly ICoordsUtils coordsUtils; |
| | 15 | | private readonly Vector3Variable worldOffset; |
| | 16 | |
|
| | 17 | | private readonly IMapCullingController cullingController; |
| | 18 | |
|
| | 19 | | private CancellationTokenSource cts; |
| | 20 | |
|
| 2880 | 21 | | public string CurrentPlayerId { get; private set; } |
| 0 | 22 | | public Vector3 CurrentPosition => poolableBehavior.currentPosition; |
| | 23 | |
|
| 0 | 24 | | public Vector2 Pivot { get; } |
| | 25 | |
|
| | 26 | | private MapMarkerPoolableBehavior<HotUserMarkerObject> poolableBehavior; |
| | 27 | |
|
| 2880 | 28 | | internal HotUserMarker(IUnityObjectPool<HotUserMarkerObject> pool, IMapCullingController mapCullingController, I |
| | 29 | | { |
| 2880 | 30 | | this.coordsUtils = coordsUtils; |
| 2880 | 31 | | this.worldOffset = worldOffset; |
| 2880 | 32 | | this.cullingController = mapCullingController; |
| 2880 | 33 | | this.Pivot = pool.Prefab.pivot; |
| | 34 | |
|
| 2880 | 35 | | poolableBehavior = new MapMarkerPoolableBehavior<HotUserMarkerObject>(pool); |
| 2880 | 36 | | } |
| | 37 | |
|
| | 38 | | public void TrackPlayer(Player player) |
| | 39 | | { |
| 0 | 40 | | cts = new CancellationTokenSource(); |
| | 41 | |
|
| 0 | 42 | | CurrentPlayerId = player.id; |
| 0 | 43 | | TrackPosition(player, cts.Token).Forget(); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | private async UniTaskVoid TrackPosition(Player player, CancellationToken ct) |
| | 47 | | { |
| 0 | 48 | | var startedTracking = false; |
| | 49 | |
|
| | 50 | | // it takes the first value |
| 0 | 51 | | await foreach (var position in player.WorldPositionProp) |
| | 52 | | { |
| 0 | 53 | | if (ct.IsCancellationRequested) |
| | 54 | | return; |
| | 55 | |
|
| 0 | 56 | | var gridPosition = Utils.WorldToGridPositionUnclamped(position + worldOffset.Get()); |
| 0 | 57 | | poolableBehavior.SetCurrentPosition(coordsUtils.PivotPosition(this, coordsUtils.CoordsToPositionUnclampe |
| | 58 | |
|
| 0 | 59 | | if (startedTracking) |
| 0 | 60 | | cullingController.SetTrackedObjectPositionDirty(this); |
| | 61 | | else |
| | 62 | | { |
| 0 | 63 | | cullingController.StartTracking(this, this); |
| 0 | 64 | | startedTracking = true; |
| | 65 | | } |
| | 66 | | } |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | private void ResetPlayer() |
| | 70 | | { |
| 2880 | 71 | | CurrentPlayerId = null; |
| 2880 | 72 | | cts?.Cancel(); |
| 2880 | 73 | | cts?.Dispose(); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | public void Dispose() |
| | 77 | | { |
| 2880 | 78 | | OnMapObjectCulled(this); |
| 2880 | 79 | | cullingController.StopTracking(this); |
| 2880 | 80 | | ResetPlayer(); |
| 2880 | 81 | | } |
| | 82 | |
|
| | 83 | | public void OnMapObjectBecameVisible(IHotUserMarker obj) |
| | 84 | | { |
| 0 | 85 | | poolableBehavior.OnBecameVisible(); |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | public void OnMapObjectCulled(IHotUserMarker obj) |
| | 89 | | { |
| 2880 | 90 | | poolableBehavior.OnBecameInvisible(); |
| | 91 | | // Keep tracking position |
| 2880 | 92 | | } |
| | 93 | | } |
| | 94 | | } |