| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCLServices.MapRendererV2.CommonBehavior; |
| | 5 | | using DCLServices.MapRendererV2.CoordsUtils; |
| | 6 | | using DCLServices.MapRendererV2.Culling; |
| | 7 | | using MainScripts.DCL.Helpers.Utils; |
| | 8 | | using System; |
| | 9 | | using System.Threading; |
| | 10 | | using UnityEngine; |
| | 11 | |
|
| | 12 | | namespace DCLServices.MapRendererV2.MapLayers.UsersMarkers.Friends |
| | 13 | | { |
| | 14 | | internal class FriendUserMarker : IFriendUserMarker |
| | 15 | | { |
| | 16 | | private const int MAX_NAME_LENGTH = 15; |
| | 17 | |
|
| | 18 | | private readonly ICoordsUtils coordsUtils; |
| | 19 | | private readonly Vector3Variable worldOffset; |
| | 20 | |
|
| | 21 | | private readonly IMapCullingController cullingController; |
| | 22 | |
|
| | 23 | | private CancellationTokenSource cts; |
| | 24 | |
|
| 2880 | 25 | | public string CurrentPlayerId { get; private set; } |
| 0 | 26 | | public Vector3 CurrentPosition => poolableBehavior.currentPosition; |
| | 27 | |
|
| 0 | 28 | | public Vector2 Pivot { get; } |
| | 29 | |
|
| | 30 | | private MapMarkerPoolableBehavior<FriendUserMarkerObject> poolableBehavior; |
| 0 | 31 | | internal string profilePicUrl { get; private set; } |
| 0 | 32 | | internal string profileName { get; private set; } |
| | 33 | | private AssetPromise_Texture profilePicturePromise; |
| | 34 | |
|
| 2880 | 35 | | internal FriendUserMarker(IUnityObjectPool<FriendUserMarkerObject> pool, IMapCullingController mapCullingControl |
| | 36 | | { |
| 2880 | 37 | | this.coordsUtils = coordsUtils; |
| 2880 | 38 | | this.worldOffset = worldOffset; |
| 2880 | 39 | | this.cullingController = mapCullingController; |
| 2880 | 40 | | this.Pivot = pool.Prefab.pivot; |
| | 41 | |
|
| 2880 | 42 | | poolableBehavior = new MapMarkerPoolableBehavior<FriendUserMarkerObject>(pool); |
| 2880 | 43 | | } |
| | 44 | |
|
| | 45 | | public void TrackPlayer(Player player) |
| | 46 | | { |
| 0 | 47 | | cts = new CancellationTokenSource(); |
| | 48 | |
|
| 0 | 49 | | CurrentPlayerId = player.id; |
| 0 | 50 | | TrackPosition(player, cts.Token).Forget(); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public void SetProfilePicture(string url) |
| | 54 | | { |
| 0 | 55 | | profilePicUrl = url; |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public void SetPlayerName(string name) |
| | 59 | | { |
| 0 | 60 | | this.profileName = name.Length > MAX_NAME_LENGTH ? name.Substring(0, MAX_NAME_LENGTH) : name; |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | public void SetZoom(float baseScale, float baseZoom, float zoom) |
| | 64 | | { |
| 0 | 65 | | float newScale = Math.Max(zoom / baseZoom * baseScale, baseScale); |
| | 66 | |
|
| 0 | 67 | | if (poolableBehavior.instance != null) |
| 0 | 68 | | poolableBehavior.instance.transform.localScale = new Vector3(newScale, newScale, 1f); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public void ResetScale(float scale) |
| | 72 | | { |
| 0 | 73 | | if (poolableBehavior.instance != null) |
| 0 | 74 | | poolableBehavior.instance.transform.localScale = new Vector3(scale, scale, 1f); |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | private async UniTaskVoid TrackPosition(Player player, CancellationToken ct) |
| | 78 | | { |
| 0 | 79 | | var startedTracking = false; |
| | 80 | |
|
| | 81 | | // it takes the first value |
| 0 | 82 | | await foreach (var position in player.WorldPositionProp) |
| | 83 | | { |
| 0 | 84 | | if (ct.IsCancellationRequested) |
| | 85 | | return; |
| | 86 | |
|
| 0 | 87 | | var gridPosition = Utils.WorldToGridPositionUnclamped(position + worldOffset.Get()); |
| 0 | 88 | | poolableBehavior.SetCurrentPosition(coordsUtils.PivotPosition(this, coordsUtils.CoordsToPositionUnclampe |
| | 89 | |
|
| 0 | 90 | | if (startedTracking) |
| 0 | 91 | | cullingController.SetTrackedObjectPositionDirty(this); |
| | 92 | | else |
| | 93 | | { |
| 0 | 94 | | cullingController.StartTracking(this, this); |
| 0 | 95 | | startedTracking = true; |
| | 96 | | } |
| | 97 | | } |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | private void ResetPlayer() |
| | 101 | | { |
| 2880 | 102 | | CurrentPlayerId = null; |
| 2880 | 103 | | cts?.Cancel(); |
| 2880 | 104 | | cts?.Dispose(); |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | public void Dispose() |
| | 108 | | { |
| 2880 | 109 | | OnMapObjectCulled(this); |
| 2880 | 110 | | cullingController.StopTracking(this); |
| 2880 | 111 | | ResetPlayer(); |
| 2880 | 112 | | } |
| | 113 | |
|
| | 114 | | public void OnMapObjectBecameVisible(IFriendUserMarker obj) |
| | 115 | | { |
| 0 | 116 | | FriendUserMarkerObject friendUserMarkerObject = poolableBehavior.OnBecameVisible(); |
| 0 | 117 | | friendUserMarkerObject.profileName.text = profileName; |
| 0 | 118 | | profilePicturePromise = new AssetPromise_Texture(profilePicUrl); |
| 0 | 119 | | profilePicturePromise.OnSuccessEvent += (assetTexture) => OnTextureDownloaded(assetTexture, friendUserMarker |
| 0 | 120 | | AssetPromiseKeeper_Texture.i.Keep(profilePicturePromise); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | private void OnTextureDownloaded(Asset_Texture obj, SpriteRenderer spriteRenderer) => |
| 0 | 124 | | spriteRenderer.sprite = Sprite.Create(obj.texture, new Rect(0.0f, 0.0f, obj.texture.width, obj.texture.heigh |
| | 125 | |
|
| | 126 | | public void OnMapObjectCulled(IFriendUserMarker obj) |
| | 127 | | { |
| 2880 | 128 | | poolableBehavior.OnBecameInvisible(); |
| 2880 | 129 | | AssetPromiseKeeper_Texture.i.Forget(profilePicturePromise); |
| 2880 | 130 | | } |
| | 131 | | } |
| | 132 | | } |