| | 1 | | using System; |
| | 2 | | using DCL.Interface; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// This button lets the player jump to the current location of a friend. |
| | 9 | | /// To initialize this control, use UpdateInfo(). |
| | 10 | | /// </summary> |
| | 11 | | public class JumpInButton : MonoBehaviour |
| | 12 | | { |
| | 13 | | public Button button; |
| | 14 | | public TextMeshProUGUI playerLocationText; |
| | 15 | |
|
| | 16 | | private IFriendsController currentFriendsController; |
| | 17 | | private string currentUserId; |
| | 18 | | private FriendsController.UserStatus currentUserStatus; |
| | 19 | |
|
| | 20 | | internal Vector2 currentCoords; |
| | 21 | | internal string currentRealmServerName; |
| | 22 | | internal string currentRealmLayerName; |
| | 23 | | internal PresenceStatus currentPresenceStatus; |
| | 24 | |
|
| | 25 | | public event Action OnClick; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Prepares the JumpIn button for listening to a specific user. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="friendsController">Friends Controller to be listened</param> |
| | 31 | | /// <param name="userId">User ID to listen to</param> |
| | 32 | | public void Initialize(IFriendsController friendsController, string userId) |
| | 33 | | { |
| 26 | 34 | | if (friendsController == null) |
| 15 | 35 | | return; |
| | 36 | |
|
| 11 | 37 | | currentFriendsController = friendsController; |
| 11 | 38 | | currentUserId = userId; |
| | 39 | |
|
| 11 | 40 | | button.onClick.RemoveAllListeners(); |
| 12 | 41 | | button.onClick.AddListener(() => JumpIn()); |
| | 42 | |
|
| 11 | 43 | | currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus; |
| 11 | 44 | | currentFriendsController.OnUpdateUserStatus += FriendsController_OnUpdateUserStatus; |
| | 45 | |
|
| 11 | 46 | | SearchUserStatus(currentUserId); |
| 11 | 47 | | } |
| | 48 | |
|
| | 49 | | private void OnDestroy() |
| | 50 | | { |
| 56 | 51 | | if (currentFriendsController == null) |
| 45 | 52 | | return; |
| | 53 | |
|
| 11 | 54 | | button.onClick.RemoveAllListeners(); |
| 11 | 55 | | currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus; |
| 11 | 56 | | } |
| | 57 | |
|
| | 58 | | private void FriendsController_OnUpdateUserStatus(string userId, FriendsController.UserStatus userStatus) |
| | 59 | | { |
| 3 | 60 | | if (userId != currentUserId) |
| 0 | 61 | | return; |
| | 62 | |
|
| 3 | 63 | | UpdateInfo(userStatus.position, userStatus.realm?.serverName, userStatus.realm?.layer, userStatus.presence); |
| 3 | 64 | | } |
| | 65 | |
|
| | 66 | | private void SearchUserStatus(string userId) |
| | 67 | | { |
| 11 | 68 | | if (currentFriendsController.GetFriends().TryGetValue(userId, out currentUserStatus)) |
| | 69 | | { |
| 4 | 70 | | UpdateInfo( |
| | 71 | | currentUserStatus.position, |
| | 72 | | currentUserStatus.realm != null ? currentUserStatus.realm.serverName : string.Empty, |
| | 73 | | currentUserStatus.realm != null ? currentUserStatus.realm.layer : string.Empty, |
| | 74 | | currentUserStatus.presence); |
| | 75 | | } |
| 11 | 76 | | } |
| | 77 | |
|
| | 78 | | private void UpdateInfo(Vector2 coords, string realmServerName, string realmLayerName, PresenceStatus status) |
| | 79 | | { |
| 7 | 80 | | currentCoords = coords; |
| 7 | 81 | | currentRealmServerName = realmServerName; |
| 7 | 82 | | currentRealmLayerName = realmLayerName; |
| 7 | 83 | | currentPresenceStatus = status; |
| | 84 | |
|
| 7 | 85 | | RefreshInfo(); |
| 7 | 86 | | } |
| | 87 | |
|
| | 88 | | private void RefreshInfo() |
| | 89 | | { |
| 7 | 90 | | if (currentPresenceStatus == PresenceStatus.ONLINE && |
| | 91 | | !string.IsNullOrEmpty(currentRealmServerName)) |
| | 92 | | { |
| 6 | 93 | | string realmText = string.Format("{0}{1}", currentRealmServerName, !string.IsNullOrEmpty(currentRealmLayerNa |
| 6 | 94 | | playerLocationText.text = $"{realmText} {(int)currentCoords.x}, {(int)currentCoords.y}"; |
| 6 | 95 | | this.gameObject.SetActive(true); |
| 6 | 96 | | } |
| | 97 | | else |
| | 98 | | { |
| 1 | 99 | | this.gameObject.SetActive(false); |
| 1 | 100 | | playerLocationText.text = string.Empty; |
| | 101 | | } |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | private void JumpIn() |
| | 105 | | { |
| 1 | 106 | | OnClick?.Invoke(); |
| 1 | 107 | | WebInterface.JumpIn((int)currentCoords.x, (int)currentCoords.y, currentRealmServerName, currentRealmLayerName); |
| 1 | 108 | | } |
| | 109 | | } |