PKMatchingView.cs 5.2 KB

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