PKMatchingView.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. SocketPlayer socketPlayer;
  12. //邀请者需要输入的信息
  13. public PKFriendInfoPack pkFriendInfoPack = null;
  14. [NonSerialized] public bool isFriendPKInviter;
  15. //被邀请者需要输入的信息
  16. [NonSerialized] public int targetJoinRoomID;
  17. [NonSerialized] public bool isFriendPKInvitee;
  18. public static PKMatchingView Create() {
  19. GameObject o = GameObject.Instantiate(GameObject.Find("WindowViews").transform.Find("PKMatchingView").gameObject);
  20. o.SetActive(true);
  21. return o.GetComponent<PKMatchingView>();
  22. }
  23. public static PKMatchingView ins;
  24. public void Awake() {
  25. ins = this;
  26. }
  27. public void OnDestroy() {
  28. if (ins == this) ins = null;
  29. }
  30. void Start()
  31. {
  32. Sprite avatar = RoleMgr.GetAvatar(LoginMgr.myUserInfo.avatarID);
  33. string nickname = LoginMgr.myUserInfo.nickname;
  34. RenderPlayerInfo(1, avatar, nickname, true);
  35. RenderPlayerInfo(2, null, "", false);
  36. if (isFriendPKInviter || isFriendPKInvitee) {
  37. RenderTip("正在等待好友加入游戏···");
  38. } else {
  39. RenderTip("正在搜索实力相当的对手···");
  40. }
  41. socketPlayer = new GameObject("SocketPlayer").AddComponent<SocketPlayer>();
  42. socketPlayer.onLoad_ = () => {
  43. socketPlayer.UploadPlayerInfo();
  44. if (isFriendPKInviter) {
  45. socketPlayer.CreateFriendPKRoom();
  46. } else if (isFriendPKInvitee) {
  47. socketPlayer.JoinFriendPKRoom(targetJoinRoomID);
  48. } else {
  49. socketPlayer.RandomMatchRoom();
  50. }
  51. };
  52. socketPlayer.onCreateFriendPKRoom = (roomID) => {
  53. UserPlayer.ins.call("friendComp.invitePK",
  54. pkFriendInfoPack.id, pkFriendInfoPack.avatarID, pkFriendInfoPack.nickname,
  55. roomID, GlobalData.matchRoomType
  56. );
  57. };
  58. socketPlayer.onMatchSuccess = () => {
  59. int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2;
  60. MatchPlayerInfo info = GlobalData.matchPlayerInfos[otherIndex];
  61. (Sprite avatar, string nickname) = RoleMgr.GetRoleInfo(info.avatarID);
  62. nickname = info.nickname;
  63. RenderPlayerInfo(2, avatar, nickname, true);
  64. RenderTip("匹配成功,即将开始游戏!");
  65. HideBtnBack();
  66. PauseWaitingTime();
  67. socketPlayer.AgreeStartGame();
  68. };
  69. socketPlayer.onAgreeStartGame = () => {
  70. DontDestroyOnLoad(socketPlayer);
  71. toLoadGame = true;
  72. };
  73. socketPlayer.onDestroy_ += () => {
  74. if (this && this.gameObject) {
  75. Destroy(this.gameObject);
  76. Destroy(socketPlayer.gameObject);
  77. PopupMgr.ins.ShowBGTip("网络发生意外!");
  78. }
  79. };
  80. }
  81. //通过此方法,不需要经过匹配页面进行匹配,可以直接在游戏场景匹配,方便测试
  82. public static void MoniMatchForTestInGameScene(Action onAgreeStartGame) {
  83. SocketPlayer socketPlayer = new GameObject("SocketPlayer").AddComponent<SocketPlayer>();
  84. socketPlayer.onLoad_ = () => {
  85. socketPlayer.UploadPlayerInfo();
  86. socketPlayer.RandomMatchRoom();
  87. };
  88. socketPlayer.onMatchSuccess = () => {
  89. socketPlayer.AgreeStartGame();
  90. };
  91. socketPlayer.onAgreeStartGame = () => {
  92. DontDestroyOnLoad(socketPlayer);
  93. if (onAgreeStartGame != null) onAgreeStartGame();
  94. };
  95. }
  96. bool toLoadGame = false;
  97. float loadGameSceneCountdown = 1;
  98. void Update() {
  99. if (waitingTime >= 0) {
  100. waitingTime += Time.deltaTime;
  101. this.transform.Find("BoxRight/TimeBG").GetComponentInChildren<Text>().text = TimeUtil.GetTimeStr(waitingTime, false);
  102. }
  103. if (toLoadGame) {
  104. loadGameSceneCountdown -= Time.deltaTime;
  105. if (loadGameSceneCountdown <= 0) {
  106. toLoadGame = false;
  107. //loadscene
  108. if (GlobalData.matchRoomType == 0) {
  109. GameMgr.gameType = 9;
  110. SceneManager.LoadScene("Game", LoadSceneMode.Single);
  111. }
  112. //challenge-game
  113. int[] roomTypes = {1, 2, 3};
  114. int[] gameTypes = {10, 11, 12};
  115. int curIndex = Array.IndexOf(roomTypes, GlobalData.matchRoomType);
  116. if (curIndex >= 0) {
  117. GameMgr.gameType = gameTypes[curIndex];
  118. SceneManager.LoadScene("GameChallenge", LoadSceneMode.Single);
  119. }
  120. }
  121. }
  122. }
  123. void PauseWaitingTime() {
  124. waitingTime = -1;
  125. }
  126. void RenderPlayerInfo(int playerID, Sprite avatar, string nickname, bool active) {
  127. this.transform.Find($"BoxRight/Player{playerID}/NameBox")
  128. .GetComponentInChildren<Text>().text = active ? nickname : "等待加入";
  129. Transform avatarT = this.transform.Find($"BoxRight/Player{playerID}/MatchHeadBG/Avatar");
  130. avatarT.gameObject.SetActive(active);
  131. avatarT.Find("Sprite").GetComponent<Image>().sprite = avatar;
  132. }
  133. void ChangeMatchHeadBG(int typeIndex) {
  134. Image img = this.transform.Find("BoxRight/Player2/MatchHeadBG").GetComponent<Image>();
  135. img.sprite = matchHeadBGList[typeIndex];
  136. img.GetComponent<Button>().enabled = typeIndex == 1;
  137. }
  138. void RenderTip(string content) {
  139. this.transform.Find("BoxRight/Tip").GetComponentInChildren<Text>().text = content;
  140. }
  141. void HideBtnBack() {
  142. this.transform.Find("Back").gameObject.SetActive(false);
  143. }
  144. public void Back() {
  145. AudioMgr.ins.PlayBtn();
  146. Destroy(this.gameObject);
  147. Destroy(socketPlayer.gameObject);
  148. }
  149. }
  150. public class PKFriendInfoPack {
  151. public int id;
  152. public int avatarID;
  153. public string nickname = "";
  154. public PKFriendInfoPack() {}
  155. public PKFriendInfoPack(int id, int avatarID, string nickname) {
  156. this.id = id;
  157. this.avatarID = avatarID;
  158. this.nickname = nickname;
  159. }
  160. }