PKMatchingView.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. //邀请者需要输入的信息
  12. [NonSerialized] public int targetInvitePlayerID;
  13. [NonSerialized] public bool isFriendPKInviter;
  14. //被邀请者需要输入的信息
  15. [NonSerialized] public string targetJoinRoomKey;
  16. [NonSerialized] public bool isFriendPKInvitee;
  17. public static PKMatchingView Create() {
  18. GameObject o = GameObject.Instantiate(GameObject.Find("WindowViews").transform.Find("PKMatchingView").gameObject);
  19. o.SetActive(true);
  20. return o.GetComponent<PKMatchingView>();
  21. }
  22. public static PKMatchingView ins;
  23. public void Awake() {
  24. ins = this;
  25. }
  26. public void OnDestroy() {
  27. if (ins == this) ins = null;
  28. PKComp.ins.cancelRandomMatch();
  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. if (isFriendPKInviter) {
  42. PKComp.ins.inviteFriendGamePK(targetInvitePlayerID);
  43. } else if (isFriendPKInvitee) {
  44. PKComp.ins.acceptFriendGamePK(targetJoinRoomKey);
  45. } else {
  46. PKComp.ins.randomMatch();
  47. }
  48. }
  49. public void EnterGameSceneOnMatchSuccess() {
  50. int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2;
  51. MatchPlayerInfo info = GlobalData.matchPlayerInfos[otherIndex];
  52. RenderPlayerInfo(2, RoleMgr.GetAvatar(info.avatarID), info.nickname, true);
  53. RenderTip("匹配成功,即将开始游戏!");
  54. HideBtnBack();
  55. PauseWaitingTime();
  56. JC.Unity.CoroutineStarter.Start(LoadSceneOnEnterGame());
  57. }
  58. IEnumerator LoadSceneOnEnterGame() {
  59. yield return new WaitForSecondsRealtime(1);
  60. GameMgr.gameType = GlobalData.matchGameType;
  61. if (GameMgr.gameType == 9) {
  62. SceneManager.LoadScene("Game", LoadSceneMode.Single);
  63. } else {
  64. SceneManager.LoadScene("GameChallenge", LoadSceneMode.Single);
  65. }
  66. }
  67. //通过此方法,不需要经过匹配页面进行匹配,可以直接在游戏场景匹配,方便测试
  68. public static void MoniMatchForTestInGameScene(Action onAgreeStartGame) {
  69. }
  70. void Update() {
  71. if (waitingTime >= 0) {
  72. waitingTime += Time.deltaTime;
  73. this.transform.Find("BoxRight/TimeBG").GetComponentInChildren<Text>().text = TimeUtil.GetTimeStr(waitingTime, false);
  74. }
  75. }
  76. void PauseWaitingTime() {
  77. waitingTime = -1;
  78. }
  79. void RenderPlayerInfo(int playerID, Sprite avatar, string nickname, bool active) {
  80. this.transform.Find($"BoxRight/Player{playerID}/NameBox")
  81. .GetComponentInChildren<Text>().text = active ? nickname : "等待加入";
  82. Transform avatarT = this.transform.Find($"BoxRight/Player{playerID}/MatchHeadBG/Avatar");
  83. avatarT.gameObject.SetActive(active);
  84. avatarT.Find("Sprite").GetComponent<Image>().sprite = avatar;
  85. }
  86. void ChangeMatchHeadBG(int typeIndex) {
  87. Image img = this.transform.Find("BoxRight/Player2/MatchHeadBG").GetComponent<Image>();
  88. img.sprite = matchHeadBGList[typeIndex];
  89. img.GetComponent<Button>().enabled = typeIndex == 1;
  90. }
  91. void RenderTip(string content) {
  92. this.transform.Find("BoxRight/Tip").GetComponentInChildren<Text>().text = content;
  93. }
  94. void HideBtnBack() {
  95. this.transform.Find("Back").gameObject.SetActive(false);
  96. }
  97. public void Back() {
  98. AudioMgr.ins.PlayBtn();
  99. Destroy(this.gameObject);
  100. }
  101. }