invitePlayer.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. using Mirror;
  6. using TMPro;
  7. public class invitePlayer : NetworkBehaviour
  8. {
  9. [Header("UI References")]
  10. [SerializeField] private GameObject inviteUI, beingInviteUI;
  11. [SerializeField] private GameObject inPartyUI;
  12. [SerializeField] private TMP_Text inPartyOwnerNameTxt;
  13. [SerializeField] private TMP_Text inPartyPlayersTxt;
  14. [SerializeField] private TMP_Text inviteOwnerNameTxt;
  15. [SerializeField] private CanvasGroup uiCanvasGroup, bUiCanvasGroup;
  16. [SerializeField] private RectTransform uiRectTransform, panelRect;
  17. [Header("Animation Settings")]
  18. [SerializeField] private float appearDuration = 0.3f;
  19. [SerializeField] private float disappearDuration = 0.2f;
  20. [SerializeField] private Ease appearEase = Ease.OutBack;
  21. [SerializeField] private Ease disappearEase = Ease.InBack;
  22. [SerializeField] private Vector2 offScreenPosition = new Vector2(1200, -140);
  23. [SerializeField] private Vector2 onScreenPosition = new Vector2(1300, -140);
  24. private void Start()
  25. {
  26. uiRectTransform.localScale = Vector3.zero;
  27. uiCanvasGroup.alpha = 0;
  28. inviteUI.SetActive(false);
  29. beingInviteUI.SetActive(false);
  30. onScreenPosition = panelRect.anchoredPosition;
  31. offScreenPosition = new Vector2(
  32. +Screen.width, // Use screen width for consistent off-screen positioning
  33. onScreenPosition.y
  34. );
  35. panelRect.anchoredPosition = offScreenPosition;
  36. }
  37. void Update()
  38. {
  39. if (Input.GetKeyDown(KeyCode.L))
  40. {
  41. ShowInvite("keyBoard");
  42. }
  43. }
  44. private void OnMouseDown() //clicking on collider
  45. {
  46. ShowInviteUI();
  47. }
  48. public void ShowInviteUI()
  49. {
  50. inviteUI.SetActive(true);
  51. uiCanvasGroup.alpha = 0;
  52. uiRectTransform.localScale = Vector3.zero;
  53. Sequence showSequence = DOTween.Sequence();
  54. showSequence.Append(uiRectTransform.DOScale(1, appearDuration).SetEase(appearEase))
  55. .Join(uiCanvasGroup.DOFade(1, appearDuration))
  56. .SetUpdate(true);
  57. Camera.main.DOShakePosition(0.2f, 0.1f, 1, 90f, false);
  58. }
  59. public void CloseInviteUI()
  60. {
  61. Sequence hideSequence = DOTween.Sequence();
  62. hideSequence.Append(uiRectTransform.DOScale(0, disappearDuration).SetEase(disappearEase))
  63. .Join(uiCanvasGroup.DOFade(0, disappearDuration))
  64. .OnComplete(() => inviteUI.SetActive(false));
  65. }
  66. public void InvitePlayer()
  67. {
  68. string thisPlayerName = GetComponent<playerNetwork>().playerName;
  69. playerNetwork.localPlayer.CmdInvitePlayer(thisPlayerName);
  70. HidePanel();
  71. }
  72. public string InviteOwner = "";
  73. public void ShowInvite(string ownerName)
  74. {
  75. InviteOwner = ownerName;
  76. inviteOwnerNameTxt.text = "You are being invited to a Party by " + ownerName;
  77. Debug.Log("ShowPanel called");
  78. beingInviteUI.SetActive(true);
  79. panelRect.DOAnchorPos(onScreenPosition, 0.5f)
  80. .SetEase(Ease.OutBack);
  81. }
  82. public void AcceptInvite()
  83. {
  84. playerNetwork.localPlayer.CmdAcceptInvite(InviteOwner);
  85. HidePanel();
  86. }
  87. public void DeclineInvite()
  88. {
  89. HidePanel();
  90. }
  91. public void HidePanel()
  92. {
  93. panelRect.DOAnchorPos(offScreenPosition, 0.5f)
  94. .SetEase(Ease.OutBack)
  95. .OnComplete(() => beingInviteUI.SetActive(false));
  96. }
  97. public void InParty(string ownerName)
  98. {
  99. if (ownerName.Length == 0)
  100. {
  101. inPartyUI.SetActive(false);
  102. }
  103. else
  104. {
  105. inPartyUI.SetActive(true);
  106. inPartyOwnerNameTxt.text = $"{ownerName}'s Party";
  107. playerNetwork[] players = FindObjectsOfType<playerNetwork>();
  108. List<string> playerNames = new List<string>();
  109. foreach (playerNetwork player in players)
  110. {
  111. if (player.myPartyOwner == ownerName)
  112. {
  113. playerNames.Add(player.playerName);
  114. }
  115. }
  116. inPartyPlayersTxt.text = string.Join(", ", playerNames);
  117. }
  118. }
  119. public void LeaveParty(){
  120. //playerNetwork.localPlayer.CmdLeaveParty();
  121. inPartyUI.SetActive(false);
  122. }
  123. }