PKMatchingView.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7. /* 界面-PK匹配中 */
  8. public class PKMatchingView : MonoBehaviour, MenuBackInterface
  9. {
  10. [SerializeField] Sprite[] matchHeadBGList;
  11. float waitingTime = 0;
  12. //viewUUID
  13. [NonSerialized] public string viewUUID = System.Guid.NewGuid().ToString();
  14. //邀请者需要输入的信息-好友PK
  15. [NonSerialized] public int targetInvitePlayerID;
  16. [NonSerialized] public bool isFriendPKInviter;
  17. //被邀请者需要输入的信息-好友PK
  18. [NonSerialized] public string targetJoinRoomKey;
  19. [NonSerialized] public bool isFriendPKInvitee;
  20. //邀请者需要输入的信息-再来一次
  21. [NonSerialized] public bool isTryAgainInviter;
  22. //被邀请者需要输入的信息-再来一次
  23. [NonSerialized] public string targetJoinTryAgainRoomKey;
  24. [NonSerialized] public bool isTryAgainInvitee;
  25. public static PKMatchingView Create() {
  26. if (ins && ins.gameObject) Destroy(ins.gameObject);
  27. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/PKMatchingView"));
  28. o.SetActive(true);
  29. return o.GetComponent<PKMatchingView>();
  30. }
  31. public void InitForInviterToTryAgain() {
  32. //get other info
  33. int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2;
  34. MatchPlayerInfo info = GlobalData.matchPlayerInfos[otherIndex];
  35. //set config
  36. this.isTryAgainInviter = true;
  37. }
  38. public static PKMatchingView ins;
  39. public void Awake() {
  40. ins = this;
  41. }
  42. public void OnDestroy() {
  43. if (ins == this) ins = null;
  44. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  45. PKComp.Instance.cancelRandomMatch();
  46. }
  47. void Start()
  48. {
  49. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  50. Sprite avatar = RoleMgr.GetAvatar(LoginMgr.myUserInfo.avatarID);
  51. string nickname = LoginMgr.myUserInfo.nickname;
  52. RenderPlayerInfo(1, avatar, nickname, true);
  53. RenderPlayerInfo(2, null, "", false);
  54. if (isFriendPKInviter || isFriendPKInvitee) {
  55. RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-waiting1"));
  56. } else if (isTryAgainInviter || isTryAgainInvitee) {
  57. RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-waiting2"));
  58. } else {
  59. RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-waiting3"));
  60. }
  61. if (isFriendPKInviter) {
  62. PKComp.Instance.inviteFriendGamePK(targetInvitePlayerID, viewUUID);
  63. } else if (isFriendPKInvitee) {
  64. PKComp.Instance.acceptFriendGamePK(targetJoinRoomKey);
  65. } else if (isTryAgainInviter) {
  66. PKComp.Instance.inviteOtherTryAgainGamePK(viewUUID);
  67. } else if (isTryAgainInvitee) {
  68. PKComp.Instance.acceptOtherTryAgainGamePK(targetJoinTryAgainRoomKey);
  69. } else {
  70. PKComp.Instance.randomMatch();
  71. }
  72. }
  73. public bool OnMenuBack() {
  74. var isActive = this.transform.Find("Back").gameObject.activeSelf;
  75. if (isActive) Destroy(gameObject);
  76. return isActive;
  77. }
  78. public void EnterGameSceneOnMatchSuccess() {
  79. int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2;
  80. MatchPlayerInfo info = GlobalData.matchPlayerInfos[otherIndex];
  81. RenderPlayerInfo(2, RoleMgr.GetAvatar(info.avatarID), info.nickname, true);
  82. RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-success"));
  83. HideBtnBack();
  84. PauseWaitingTime();
  85. JCUnityLib.CoroutineStarter.Start(LoadSceneOnEnterGame());
  86. }
  87. IEnumerator LoadSceneOnEnterGame() {
  88. yield return new WaitForSecondsRealtime(1);
  89. GameMgr.gameType = GlobalData.matchGameType;
  90. if (GameMgr.gameType == 9) {
  91. SceneManager.LoadScene("Game", LoadSceneMode.Single);
  92. } else {
  93. SceneManager.LoadScene("GameChallenge", LoadSceneMode.Single);
  94. }
  95. }
  96. void Update() {
  97. if (waitingTime >= 0) {
  98. waitingTime += Time.deltaTime;
  99. this.transform.Find("BoxRight/TimeBG").GetComponentInChildren<Text>().text = TimeUtil.GetTimeStr(waitingTime, false);
  100. }
  101. }
  102. void PauseWaitingTime() {
  103. waitingTime = -1;
  104. }
  105. void RenderPlayerInfo(int playerID, Sprite avatar, string nickname, bool active) {
  106. this.transform.Find($"BoxRight/Player{playerID}/NameBox")
  107. .GetComponentInChildren<Text>().text = active
  108. ? nickname
  109. : TextAutoLanguage2.GetTextByKey("pk-match_wait-to-join");
  110. Transform avatarT = this.transform.Find($"BoxRight/Player{playerID}/MatchHeadBG/Avatar");
  111. avatarT.gameObject.SetActive(active);
  112. avatarT.Find("Sprite").GetComponent<Image>().sprite = avatar;
  113. }
  114. void ChangeMatchHeadBG(int typeIndex) {
  115. Image img = this.transform.Find("BoxRight/Player2/MatchHeadBG").GetComponent<Image>();
  116. img.sprite = matchHeadBGList[typeIndex];
  117. img.GetComponent<Button>().enabled = typeIndex == 1;
  118. }
  119. void RenderTip(string content) {
  120. this.transform.Find("BoxRight/Tip").GetComponentInChildren<Text>().text = content;
  121. }
  122. void HideBtnBack() {
  123. this.transform.Find("Back").gameObject.SetActive(false);
  124. }
  125. [NonSerialized] public bool banBackBtnLogic;
  126. public void Back() {
  127. AudioMgr.ins.PlayBtn();
  128. if (banBackBtnLogic) return;
  129. Destroy(this.gameObject);
  130. }
  131. [NonSerialized] public System.Action eventOnRejectPKInvite;
  132. }