| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | |
|
| | 4 | | internal class OtherPlayerGetAnchorPointsHandler : IAnchorPointsGetterHandler |
| | 5 | | { |
| | 6 | | public event Action<string> OnAvatarRemoved; |
| | 7 | | public event Action<string, IAvatarAnchorPoints> OnAvatarFound; |
| | 8 | |
|
| 52 | 9 | | private IBaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| | 10 | | private string avatarId; |
| | 11 | |
|
| 15 | 12 | | public OtherPlayerGetAnchorPointsHandler() |
| | 13 | | { |
| 15 | 14 | | otherPlayers.OnRemoved += OnOtherPlayersRemoved; |
| 15 | 15 | | } |
| | 16 | |
|
| | 17 | | public void GetAnchorPoints(string id) |
| | 18 | | { |
| 8 | 19 | | avatarId = id; |
| | 20 | |
|
| 8 | 21 | | if (otherPlayers.TryGetValue(avatarId, out Player player)) |
| | 22 | | { |
| 6 | 23 | | OnAvatarFound?.Invoke(avatarId, player.anchorPoints); |
| 6 | 24 | | return; |
| | 25 | | } |
| | 26 | |
|
| 2 | 27 | | otherPlayers.OnAdded -= OnOtherPlayersAdded; |
| 2 | 28 | | otherPlayers.OnAdded += OnOtherPlayersAdded; |
| 2 | 29 | | } |
| | 30 | |
|
| | 31 | | public void CleanUp() |
| | 32 | | { |
| 17 | 33 | | avatarId = null; |
| 17 | 34 | | otherPlayers.OnAdded -= OnOtherPlayersAdded; |
| 17 | 35 | | } |
| | 36 | |
|
| | 37 | | public void Dispose() |
| | 38 | | { |
| 7 | 39 | | CleanUp(); |
| 7 | 40 | | otherPlayers.OnRemoved -= OnOtherPlayersRemoved; |
| 7 | 41 | | } |
| | 42 | |
|
| | 43 | | private void OnOtherPlayersAdded(string id, Player player) |
| | 44 | | { |
| 1 | 45 | | if (id != avatarId) |
| | 46 | | { |
| 0 | 47 | | return; |
| | 48 | | } |
| 1 | 49 | | OnAvatarFound?.Invoke(avatarId, player.anchorPoints); |
| 1 | 50 | | otherPlayers.OnAdded -= OnOtherPlayersAdded; |
| 1 | 51 | | } |
| | 52 | |
|
| | 53 | | private void OnOtherPlayersRemoved(string id, Player player) |
| | 54 | | { |
| 2 | 55 | | if (id != avatarId) |
| | 56 | | { |
| 0 | 57 | | return; |
| | 58 | | } |
| 2 | 59 | | OnAvatarRemoved?.Invoke(avatarId); |
| 2 | 60 | | } |
| | 61 | | } |