| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL; |
| | 3 | |
|
| | 4 | | namespace AvatarNamesHUD |
| | 5 | | { |
| | 6 | | public class AvatarNamesHUDController : IHUD |
| | 7 | | { |
| 0 | 8 | | private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| | 9 | |
|
| | 10 | | internal IAvatarNamesHUDView view; |
| 6 | 11 | | internal readonly HashSet<string> trackingPlayers = new HashSet<string>(); |
| | 12 | |
|
| 1 | 13 | | internal virtual IAvatarNamesHUDView CreateView() { return AvatarNamesHUDView.CreateView(); } |
| | 14 | |
|
| | 15 | | public void Initialize() |
| | 16 | | { |
| 6 | 17 | | view = CreateView(); |
| 6 | 18 | | view?.Initialize(); |
| | 19 | |
|
| 6 | 20 | | otherPlayers.OnAdded += OnOtherPlayersStatusAdded; |
| 6 | 21 | | otherPlayers.OnRemoved += OnOtherPlayersStatusRemoved; |
| 6 | 22 | | using var enumerator = otherPlayers.Get().GetEnumerator(); |
| 10 | 23 | | while (enumerator.MoveNext()) |
| | 24 | | { |
| 4 | 25 | | OnOtherPlayersStatusAdded(enumerator.Current.Key, enumerator.Current.Value); |
| | 26 | | } |
| 12 | 27 | | } |
| | 28 | |
|
| | 29 | | internal void OnOtherPlayersStatusAdded(string userId, Player player) |
| | 30 | | { |
| 10 | 31 | | if (trackingPlayers.Contains(userId)) |
| 2 | 32 | | return; |
| | 33 | |
|
| 8 | 34 | | trackingPlayers.Add(userId); |
| 8 | 35 | | view?.TrackPlayer(player); |
| 8 | 36 | | } |
| | 37 | |
|
| | 38 | | internal void OnOtherPlayersStatusRemoved(string userId, Player player) |
| | 39 | | { |
| 3 | 40 | | if (!trackingPlayers.Remove(userId)) |
| 1 | 41 | | return; |
| | 42 | |
|
| 2 | 43 | | view?.UntrackPlayer(userId); |
| 2 | 44 | | } |
| | 45 | |
|
| 2 | 46 | | public void SetVisibility(bool visible) { view?.SetVisibility(visible); } |
| | 47 | |
|
| | 48 | | public void Dispose() |
| | 49 | | { |
| 1 | 50 | | otherPlayers.OnAdded -= OnOtherPlayersStatusAdded; |
| 1 | 51 | | otherPlayers.OnRemoved -= OnOtherPlayersStatusRemoved; |
| 1 | 52 | | view?.Dispose(); |
| 1 | 53 | | } |
| | 54 | | } |
| | 55 | | } |