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