PKMatchingView.cs 4.8 KB

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