PKMatchingView.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. int avatarID = LoginMgr.myUserInfo.avatarID;
  51. string avatarUrl = LoginMgr.myUserInfo.avatarUrl;
  52. string nickname = LoginMgr.myUserInfo.nickname;
  53. RenderPlayerInfo(1, avatarID, avatarUrl, nickname, true);
  54. RenderPlayerInfo(2, RoleMgr.NullAvatarID, "", "", false);
  55. if (isFriendPKInviter || isFriendPKInvitee) {
  56. RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-waiting1"));
  57. } else if (isTryAgainInviter || isTryAgainInvitee) {
  58. RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-waiting2"));
  59. } else {
  60. RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-waiting3"));
  61. }
  62. if (isFriendPKInviter) {
  63. PKComp.Instance.inviteFriendGamePK(targetInvitePlayerID, viewUUID);
  64. } else if (isFriendPKInvitee) {
  65. PKComp.Instance.acceptFriendGamePK(targetJoinRoomKey);
  66. } else if (isTryAgainInviter) {
  67. PKComp.Instance.inviteOtherTryAgainGamePK(viewUUID);
  68. } else if (isTryAgainInvitee) {
  69. PKComp.Instance.acceptOtherTryAgainGamePK(targetJoinTryAgainRoomKey);
  70. } else {
  71. PKComp.Instance.randomMatch();
  72. }
  73. }
  74. public bool OnMenuBack() {
  75. var isActive = this.transform.Find("Back").gameObject.activeSelf;
  76. if (isActive) Destroy(gameObject);
  77. return isActive;
  78. }
  79. public void EnterGameSceneOnMatchSuccess() {
  80. int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2;
  81. MatchPlayerInfo info = GlobalData.matchPlayerInfos[otherIndex];
  82. RenderPlayerInfo(2, info.avatarID, info.avatarUrl, info.nickname, true);
  83. RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-success"));
  84. HideBtnBack();
  85. PauseWaitingTime();
  86. JCUnityLib.CoroutineStarter.Start(LoadSceneOnEnterGame());
  87. }
  88. IEnumerator LoadSceneOnEnterGame() {
  89. yield return new WaitForSecondsRealtime(1);
  90. GameMgr.gameType = GlobalData.matchGameType;
  91. if (GameMgr.gameType == 9) {
  92. SceneManager.LoadScene("Game", LoadSceneMode.Single);
  93. } else {
  94. SceneManager.LoadScene("GameChallenge", LoadSceneMode.Single);
  95. }
  96. }
  97. void Update() {
  98. if (waitingTime >= 0) {
  99. waitingTime += Time.deltaTime;
  100. this.transform.Find("BoxRight/TimeBG").GetComponentInChildren<Text>().text = TimeUtil.GetTimeStr(waitingTime, false);
  101. }
  102. }
  103. void PauseWaitingTime() {
  104. waitingTime = -1;
  105. }
  106. void RenderPlayerInfo(int playerID, int avatarID, string avatarUrl, string nickname, bool active) {
  107. Text _textTemp = transform.Find($"BoxRight/Player{playerID}/NameBox").GetComponentInChildren<Text>();
  108. string inputText = active? nickname : TextAutoLanguage2.GetTextByKey("pk-match_wait-to-join");
  109. TextEllipsis.SetTextWithEllipsis(_textTemp, inputText);
  110. Transform avatarT = this.transform.Find($"BoxRight/Player{playerID}/MatchHeadBG/Avatar");
  111. avatarT.gameObject.SetActive(active);
  112. RoleMgr.SetAvatarToImage(
  113. avatarT.Find("Sprite").GetComponent<Image>(),
  114. avatarID, avatarUrl
  115. );
  116. }
  117. void ChangeMatchHeadBG(int typeIndex) {
  118. Image img = this.transform.Find("BoxRight/Player2/MatchHeadBG").GetComponent<Image>();
  119. img.sprite = matchHeadBGList[typeIndex];
  120. img.GetComponent<Button>().enabled = typeIndex == 1;
  121. }
  122. void RenderTip(string content) {
  123. this.transform.Find("BoxRight/Tip").GetComponentInChildren<Text>().text = content;
  124. }
  125. void HideBtnBack() {
  126. this.transform.Find("Back").gameObject.SetActive(false);
  127. }
  128. [NonSerialized] public bool banBackBtnLogic;
  129. public void Back() {
  130. AudioMgr.ins.PlayBtn();
  131. if (banBackBtnLogic) return;
  132. Destroy(this.gameObject);
  133. }
  134. [NonSerialized] public System.Action eventOnRejectPKInvite;
  135. }