invitePlayer.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. //ToDo exclude owner
  46. {
  47. Debug.Log("OnMouseDown on invitePlayer " + GetComponent<playerNetwork>().playerName);
  48. ShowInviteUI();
  49. }
  50. public void ShowInviteUI()
  51. {
  52. inviteUI.SetActive(true);
  53. uiCanvasGroup.alpha = 0;
  54. uiRectTransform.localScale = Vector3.zero;
  55. Sequence showSequence = DOTween.Sequence();
  56. showSequence.Append(uiRectTransform.DOScale(1, appearDuration).SetEase(appearEase))
  57. .Join(uiCanvasGroup.DOFade(1, appearDuration))
  58. .SetUpdate(true);
  59. Camera.main.DOShakePosition(0.2f, 0.1f, 1, 90f, false);
  60. }
  61. public void CloseInviteUI()
  62. {
  63. Sequence hideSequence = DOTween.Sequence();
  64. hideSequence.Append(uiRectTransform.DOScale(0, disappearDuration).SetEase(disappearEase))
  65. .Join(uiCanvasGroup.DOFade(0, disappearDuration))
  66. .OnComplete(() => inviteUI.SetActive(false));
  67. }
  68. public void InvitePlayer()
  69. {
  70. string thisPlayerName = GetComponent<playerNetwork>().playerName;
  71. playerNetwork.localPlayer.CmdInvitePlayer(thisPlayerName);
  72. CloseInviteUI();
  73. }
  74. public string InviteOwner = "";
  75. public void ShowInvite(string ownerName)
  76. {
  77. InviteOwner = ownerName;
  78. inviteOwnerNameTxt.text = "You are being invited to a Party by " + ownerName;
  79. Debug.Log("ShowPanel called");
  80. beingInviteUI.SetActive(true);
  81. panelRect.DOAnchorPos(onScreenPosition, 0.5f)
  82. .SetEase(Ease.OutBack);
  83. }
  84. public void AcceptInvite()
  85. {
  86. playerNetwork.localPlayer.CmdAcceptInvite(InviteOwner);
  87. HidePanel();
  88. }
  89. public void DeclineInvite()
  90. {
  91. HidePanel();
  92. }
  93. public void HidePanel()
  94. {
  95. panelRect.DOAnchorPos(offScreenPosition, 0.5f)
  96. .SetEase(Ease.OutBack)
  97. .OnComplete(() => beingInviteUI.SetActive(false));
  98. }
  99. public void InParty(string ownerName)
  100. {
  101. if (ownerName.Length == 0)
  102. {
  103. inPartyUI.SetActive(false);
  104. }
  105. else
  106. {
  107. inPartyUI.SetActive(true);
  108. inPartyOwnerNameTxt.text = $"{ownerName}'s Party";
  109. playerNetwork[] players = FindObjectsOfType<playerNetwork>();
  110. List<string> playerNames = new List<string>();
  111. foreach (playerNetwork player in players)
  112. {
  113. if (player.myPartyOwner == ownerName)
  114. {
  115. playerNames.Add(player.playerName);
  116. }
  117. }
  118. inPartyPlayersTxt.text = string.Join(", ", playerNames);
  119. }
  120. }
  121. public void LeaveParty(){
  122. //playerNetwork.localPlayer.CmdLeaveParty();
  123. inPartyUI.SetActive(false);
  124. }
  125. }