| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | |
|
| | 4 | | internal class OwnPlayerGetAnchorPointsHandler : IAnchorPointsGetterHandler |
| | 5 | | { |
| | 6 | | public event Action<string> OnAvatarRemoved; |
| | 7 | | public event Action<string, IAvatarAnchorPoints> OnAvatarFound; |
| | 8 | |
|
| 13 | 9 | | private IBaseVariable<Player> ownPlayer => DataStore.i.player.ownPlayer; |
| | 10 | | private string avatarId; |
| | 11 | |
|
| | 12 | | public void GetAnchorPoints(string id) |
| | 13 | | { |
| 2 | 14 | | avatarId = id; |
| 2 | 15 | | string ownPlayerId = ownPlayer.Get()?.id; |
| | 16 | |
|
| | 17 | | // NOTE: ownPlayerId null means player avatar is not loaded yet |
| | 18 | | // so we subscribe to the event for when player avatar is set. |
| 2 | 19 | | if (string.IsNullOrEmpty(ownPlayerId)) |
| | 20 | | { |
| 1 | 21 | | ownPlayer.OnChange -= OnOwnPlayerChanged; |
| 1 | 22 | | ownPlayer.OnChange += OnOwnPlayerChanged; |
| 1 | 23 | | return; |
| | 24 | | } |
| | 25 | |
|
| 1 | 26 | | if (id == ownPlayerId) |
| | 27 | | { |
| 1 | 28 | | OnAvatarFound?.Invoke(id, ownPlayer.Get().anchorPoints); |
| | 29 | | } |
| 1 | 30 | | } |
| | 31 | |
|
| | 32 | | public void CleanUp() |
| | 33 | | { |
| 7 | 34 | | ownPlayer.OnChange -= OnOwnPlayerChanged; |
| 7 | 35 | | } |
| | 36 | |
|
| | 37 | | public void Dispose() |
| | 38 | | { |
| 7 | 39 | | CleanUp(); |
| 7 | 40 | | } |
| | 41 | |
|
| | 42 | | private void OnOwnPlayerChanged(Player player, Player prev) |
| | 43 | | { |
| 1 | 44 | | if (player.id != avatarId) |
| | 45 | | { |
| 0 | 46 | | return; |
| | 47 | | } |
| | 48 | |
|
| 1 | 49 | | ownPlayer.OnChange -= OnOwnPlayerChanged; |
| 1 | 50 | | OnAvatarFound?.Invoke(player.id, player.anchorPoints); |
| 1 | 51 | | } |
| | 52 | | } |