using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; /* 界面-PK匹配中 */ public class PKMatchingView : MonoBehaviour, MenuBackInterface { [SerializeField] Sprite[] matchHeadBGList; float waitingTime = 0; //viewUUID [NonSerialized] public string viewUUID = System.Guid.NewGuid().ToString(); //邀请者需要输入的信息-好友PK [NonSerialized] public int targetInvitePlayerID; [NonSerialized] public bool isFriendPKInviter; //被邀请者需要输入的信息-好友PK [NonSerialized] public string targetJoinRoomKey; [NonSerialized] public bool isFriendPKInvitee; //邀请者需要输入的信息-再来一次 [NonSerialized] public bool isTryAgainInviter; //被邀请者需要输入的信息-再来一次 [NonSerialized] public string targetJoinTryAgainRoomKey; [NonSerialized] public bool isTryAgainInvitee; public static PKMatchingView Create() { if (ins && ins.gameObject) Destroy(ins.gameObject); GameObject o = GameObject.Instantiate(Resources.Load("Prefabs/Views/PKMatchingView")); o.SetActive(true); return o.GetComponent(); } public void InitForInviterToTryAgain() { //get other info int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2; MatchPlayerInfo info = GlobalData.matchPlayerInfos[otherIndex]; //set config this.isTryAgainInviter = true; } public static PKMatchingView ins; public void Awake() { ins = this; } public void OnDestroy() { if (ins == this) ins = null; PersistenHandler.ins?.menuBackCtr.views.Remove(this); PKComp.Instance.cancelRandomMatch(); } void Start() { PersistenHandler.ins?.menuBackCtr.views.Add(this); int avatarID = LoginMgr.myUserInfo.avatarID; string avatarUrl = LoginMgr.myUserInfo.avatarUrl; string nickname = LoginMgr.myUserInfo.nickname; RenderPlayerInfo(1, avatarID, avatarUrl, nickname, true); RenderPlayerInfo(2, RoleMgr.NullAvatarID, "", "", false); if (isFriendPKInviter || isFriendPKInvitee) { RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-waiting1")); } else if (isTryAgainInviter || isTryAgainInvitee) { RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-waiting2")); } else { RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-waiting3")); } if (isFriendPKInviter) { PKComp.Instance.inviteFriendGamePK(targetInvitePlayerID, viewUUID); } else if (isFriendPKInvitee) { PKComp.Instance.acceptFriendGamePK(targetJoinRoomKey); } else if (isTryAgainInviter) { PKComp.Instance.inviteOtherTryAgainGamePK(viewUUID); } else if (isTryAgainInvitee) { PKComp.Instance.acceptOtherTryAgainGamePK(targetJoinTryAgainRoomKey); } else { PKComp.Instance.randomMatch(); } } public bool OnMenuBack() { var isActive = this.transform.Find("Back").gameObject.activeSelf; if (isActive) Destroy(gameObject); return isActive; } public void EnterGameSceneOnMatchSuccess() { int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2; MatchPlayerInfo info = GlobalData.matchPlayerInfos[otherIndex]; RenderPlayerInfo(2, info.avatarID, info.avatarUrl, info.nickname, true); RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-success")); HideBtnBack(); PauseWaitingTime(); JCUnityLib.CoroutineStarter.Start(LoadSceneOnEnterGame()); } IEnumerator LoadSceneOnEnterGame() { yield return new WaitForSecondsRealtime(1); GameMgr.gameType = GlobalData.matchGameType; if (GameMgr.gameType == 9) { SceneManager.LoadScene("Game", LoadSceneMode.Single); } else { SceneManager.LoadScene("GameChallenge", LoadSceneMode.Single); } } void Update() { if (waitingTime >= 0) { waitingTime += Time.deltaTime; this.transform.Find("BoxRight/TimeBG").GetComponentInChildren().text = TimeUtil.GetTimeStr(waitingTime, false); } } void PauseWaitingTime() { waitingTime = -1; } void RenderPlayerInfo(int playerID, int avatarID, string avatarUrl, string nickname, bool active) { Text _textTemp = transform.Find($"BoxRight/Player{playerID}/NameBox").GetComponentInChildren(); string inputText = active? nickname : TextAutoLanguage2.GetTextByKey("pk-match_wait-to-join"); TextEllipsis.SetTextWithEllipsis(_textTemp, inputText); Transform avatarT = this.transform.Find($"BoxRight/Player{playerID}/MatchHeadBG/Avatar"); avatarT.gameObject.SetActive(active); RoleMgr.SetAvatarToImage( avatarT.Find("Sprite").GetComponent(), avatarID, avatarUrl ); } void ChangeMatchHeadBG(int typeIndex) { Image img = this.transform.Find("BoxRight/Player2/MatchHeadBG").GetComponent(); img.sprite = matchHeadBGList[typeIndex]; img.GetComponent