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