| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace DCL.Components |
| | 4 | | { |
| | 5 | | public class GetAnchorPointsHandler : IDisposable |
| | 6 | | { |
| | 7 | | public event Action OnAvatarRemoved; |
| | 8 | |
|
| | 9 | | private Action<IAvatarAnchorPoints> onAvatarFound; |
| | 10 | | private bool cleaned = false; |
| | 11 | |
|
| 45 | 12 | | private UserProfile ownPlayerProfile => UserProfile.GetOwnUserProfile(); |
| | 13 | |
|
| 0 | 14 | | private IAnchorPointsGetterHandler ownPlayerAnchorPointsGetterHandler => new OwnPlayerGetAnchorPointsHandler(); |
| 28 | 15 | | private IAnchorPointsGetterHandler otherPlayerAnchorPointsGetterHandler => new OtherPlayerGetAnchorPointsHandler |
| | 16 | | private IAnchorPointsGetterHandler currentAnchorPointsGetterHandler; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Search for an `avatarId` that could be the own player or any other player. |
| | 20 | | /// If that `avatarId` is loaded it will call `onSuccess`, otherwise it will wait |
| | 21 | | /// for that `avatarId` to be loaded |
| | 22 | | /// </summary> |
| | 23 | | public void SearchAnchorPoints(string avatarId, Action<IAvatarAnchorPoints> onSuccess) |
| | 24 | | { |
| 15 | 25 | | CleanUp(); |
| 15 | 26 | | cleaned = false; |
| | 27 | |
|
| 15 | 28 | | if (string.IsNullOrEmpty(avatarId)) |
| 0 | 29 | | return; |
| | 30 | |
|
| 15 | 31 | | string ownUserId = ownPlayerProfile.userId; |
| | 32 | |
|
| 15 | 33 | | onAvatarFound = onSuccess; |
| | 34 | |
|
| | 35 | | void GetOwnProfileUpdated(UserProfile profile) |
| | 36 | | { |
| 15 | 37 | | ownPlayerProfile.OnUpdate -= GetOwnProfileUpdated; |
| 15 | 38 | | ownUserId = profile.userId; |
| | 39 | |
|
| 15 | 40 | | if (cleaned) |
| | 41 | | { |
| 0 | 42 | | return; |
| | 43 | | } |
| | 44 | |
|
| 15 | 45 | | currentAnchorPointsGetterHandler = GetHandler(avatarId, ownUserId); |
| 15 | 46 | | currentAnchorPointsGetterHandler.OnAvatarFound += OnAvatarFoundEvent; |
| 15 | 47 | | currentAnchorPointsGetterHandler.OnAvatarRemoved += OnAvatarRemovedEvent; |
| 15 | 48 | | currentAnchorPointsGetterHandler.GetAnchorPoints(avatarId); |
| 15 | 49 | | } |
| | 50 | |
|
| 15 | 51 | | if (string.IsNullOrEmpty(ownUserId)) |
| | 52 | | { |
| 0 | 53 | | ownPlayerProfile.OnUpdate += GetOwnProfileUpdated; |
| 0 | 54 | | } |
| | 55 | | else |
| | 56 | | { |
| 15 | 57 | | GetOwnProfileUpdated(ownPlayerProfile); |
| | 58 | | } |
| 15 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Cancel the active search/waiting of the `avatarId` |
| | 63 | | /// </summary> |
| | 64 | | public void CancelCurrentSearch() |
| | 65 | | { |
| 28 | 66 | | CleanUp(); |
| 28 | 67 | | } |
| | 68 | |
|
| | 69 | | public void Dispose() |
| | 70 | | { |
| 15 | 71 | | CleanUp(); |
| 15 | 72 | | ownPlayerAnchorPointsGetterHandler.Dispose(); |
| 15 | 73 | | otherPlayerAnchorPointsGetterHandler.Dispose(); |
| 15 | 74 | | } |
| | 75 | |
|
| | 76 | | private void CleanUp() |
| | 77 | | { |
| 58 | 78 | | onAvatarFound = null; |
| 58 | 79 | | cleaned = true; |
| | 80 | |
|
| 58 | 81 | | if (currentAnchorPointsGetterHandler != null) |
| | 82 | | { |
| 21 | 83 | | currentAnchorPointsGetterHandler.CleanUp(); |
| 21 | 84 | | currentAnchorPointsGetterHandler.OnAvatarFound -= OnAvatarFoundEvent; |
| 21 | 85 | | currentAnchorPointsGetterHandler.OnAvatarRemoved -= OnAvatarRemovedEvent; |
| | 86 | | } |
| 58 | 87 | | } |
| | 88 | |
|
| | 89 | | private IAnchorPointsGetterHandler GetHandler(string id, string ownPlayerId) |
| | 90 | | { |
| 15 | 91 | | return id == ownPlayerId ? ownPlayerAnchorPointsGetterHandler : otherPlayerAnchorPointsGetterHandler; |
| | 92 | | } |
| | 93 | |
|
| | 94 | | private void OnAvatarFoundEvent(string id, IAvatarAnchorPoints anchorPoints) |
| | 95 | | { |
| 13 | 96 | | onAvatarFound?.Invoke(anchorPoints); |
| 12 | 97 | | } |
| | 98 | |
|
| | 99 | | private void OnAvatarRemovedEvent(string id) |
| | 100 | | { |
| 3 | 101 | | OnAvatarRemoved?.Invoke(); |
| 2 | 102 | | } |
| | 103 | | } |
| | 104 | | } |