PKMatchingView.cs 4.7 KB

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