| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace AvatarNamesHUD |
| | 8 | | { |
| | 9 | | public interface IAvatarNamesHUDView |
| | 10 | | { |
| | 11 | | void Initialize(); |
| | 12 | | void SetVisibility(bool visibility); |
| | 13 | | void TrackPlayer(Player player); |
| | 14 | | void UntrackPlayer(string userId); |
| | 15 | | void Dispose(); |
| | 16 | | } |
| | 17 | |
|
| | 18 | | public class AvatarNamesHUDView : MonoBehaviour, IAvatarNamesHUDView |
| | 19 | | { |
| | 20 | | internal const int MAX_AVATAR_NAMES = 100; //Regardless of what the dataStore says, we will show up to 100 names |
| | 21 | | internal const int INITIAL_RESERVE_SIZE = 50; //Regardless of what the dataStore says, we will show up to 100 na |
| | 22 | |
|
| | 23 | | [SerializeField] internal RectTransform canvasRect; |
| | 24 | | [SerializeField] internal RectTransform backgroundsContainer; |
| | 25 | | [SerializeField] internal RectTransform namesContainer; |
| | 26 | | [SerializeField] internal RectTransform voiceChatContainer; |
| | 27 | | [SerializeField] internal GameObject backgroundPrefab; |
| | 28 | | [SerializeField] internal GameObject namePrefab; |
| | 29 | | [SerializeField] internal GameObject voiceChatPrefab; |
| | 30 | |
|
| 9 | 31 | | internal readonly Dictionary<string, AvatarNamesTracker> trackers = new Dictionary<string, AvatarNamesTracker>() |
| 9 | 32 | | internal readonly Queue<AvatarNamesTracker> reserveTrackers = new Queue<AvatarNamesTracker>(); |
| 0 | 33 | | internal BaseHashSet<string> visibleNames => DataStore.i.avatarsLOD.visibleNames; |
| | 34 | |
|
| | 35 | | internal bool isDestroyed = false; |
| | 36 | |
|
| | 37 | | public static IAvatarNamesHUDView CreateView() |
| | 38 | | { |
| 1 | 39 | | AvatarNamesHUDView view = Instantiate(Resources.Load<GameObject>("AvatarNamesHUD")).GetComponent<AvatarNames |
| 1 | 40 | | view.gameObject.name = "_AvatarNamesHUD"; |
| 1 | 41 | | return view; |
| | 42 | | } |
| | 43 | |
|
| | 44 | | private void OnEnable() |
| | 45 | | { |
| 6 | 46 | | canvasRect.GetComponent<Canvas>().sortingOrder = -2; |
| 6 | 47 | | StartCoroutine(UpdateTrackersRoutine()); |
| 6 | 48 | | } |
| | 49 | |
|
| | 50 | | public void Initialize() |
| | 51 | | { |
| | 52 | | //Return current trackers to reserve |
| 6 | 53 | | trackers.Keys.ToList().ForEach(UntrackPlayer); |
| 6 | 54 | | trackers.Clear(); |
| | 55 | |
|
| | 56 | | //Fill the reserve |
| 306 | 57 | | while (reserveTrackers.Count < INITIAL_RESERVE_SIZE) |
| | 58 | | { |
| 300 | 59 | | AvatarNamesTracker tracker = CreateTracker(); |
| 300 | 60 | | reserveTrackers.Enqueue(tracker); |
| | 61 | | } |
| 6 | 62 | | } |
| | 63 | |
|
| | 64 | | private AvatarNamesTracker CreateTracker() |
| | 65 | | { |
| 300 | 66 | | RectTransform background = Instantiate(backgroundPrefab, backgroundsContainer).GetComponent<RectTransform>() |
| 300 | 67 | | RectTransform nameTMP = Instantiate(namePrefab, namesContainer).GetComponent<RectTransform>(); |
| 300 | 68 | | RectTransform voiceChat = Instantiate(voiceChatPrefab, voiceChatContainer).GetComponent<RectTransform>(); |
| 300 | 69 | | AvatarNamesTracker tracker = new AvatarNamesTracker(canvasRect, background, nameTMP, voiceChat); |
| 300 | 70 | | tracker.SetVisibility(false); |
| 300 | 71 | | return tracker; |
| | 72 | | } |
| | 73 | |
|
| 2 | 74 | | public void SetVisibility(bool visibility) { gameObject.SetActive(visibility); } |
| | 75 | |
|
| | 76 | | public void TrackPlayer(Player player) |
| | 77 | | { |
| 5 | 78 | | if (reserveTrackers.Count == 0) |
| 0 | 79 | | reserveTrackers.Enqueue(CreateTracker()); |
| | 80 | |
|
| 5 | 81 | | AvatarNamesTracker tracker = reserveTrackers.Dequeue(); |
| 5 | 82 | | tracker.SetPlayer(player); |
| 5 | 83 | | tracker.SetVisibility(true); |
| 5 | 84 | | trackers.Add(player.id, tracker); |
| 5 | 85 | | } |
| | 86 | |
|
| | 87 | | public void UntrackPlayer(string userId) |
| | 88 | | { |
| 1 | 89 | | if (!trackers.TryGetValue(userId, out AvatarNamesTracker tracker)) |
| 0 | 90 | | return; |
| | 91 | |
|
| 1 | 92 | | trackers.Remove(userId); |
| 1 | 93 | | tracker.SetPlayer(null); |
| 1 | 94 | | tracker.SetVisibility(false); |
| 1 | 95 | | reserveTrackers.Enqueue(tracker); |
| 1 | 96 | | } |
| | 97 | |
|
| | 98 | | private IEnumerator UpdateTrackersRoutine() |
| | 99 | | { |
| 22 | 100 | | while (true) |
| | 101 | | { |
| 28 | 102 | | int count = 0; |
| 126 | 103 | | foreach (KeyValuePair<string, AvatarNamesTracker> kvp in trackers) |
| | 104 | | { |
| 35 | 105 | | if (count >= MAX_AVATAR_NAMES || !visibleNames.Contains(kvp.Key)) |
| | 106 | | { |
| 35 | 107 | | kvp.Value.SetVisibility(false); |
| 35 | 108 | | continue; |
| | 109 | | } |
| 0 | 110 | | kvp.Value.SetVisibility(true); |
| 0 | 111 | | kvp.Value.UpdatePosition(); |
| 0 | 112 | | count++; |
| | 113 | | } |
| 28 | 114 | | yield return null; |
| | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| 12 | 118 | | private void OnDestroy() { isDestroyed = true; } |
| | 119 | |
|
| | 120 | | public void Dispose() |
| | 121 | | { |
| 1 | 122 | | if (isDestroyed) |
| 0 | 123 | | return; |
| 1 | 124 | | isDestroyed = true; |
| 1 | 125 | | Destroy(gameObject); |
| 1 | 126 | | } |
| | 127 | | } |
| | 128 | | } |