| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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<GameObject>("Prefabs/Views/PKMatchingView"));
- o.SetActive(true);
- return o.GetComponent<PKMatchingView>();
- }
- 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>().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<Text>();
- 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<Image>(),
- avatarID, avatarUrl
- );
- }
- void ChangeMatchHeadBG(int typeIndex) {
- Image img = this.transform.Find("BoxRight/Player2/MatchHeadBG").GetComponent<Image>();
- img.sprite = matchHeadBGList[typeIndex];
- img.GetComponent<Button>().enabled = typeIndex == 1;
- }
- void RenderTip(string content) {
- this.transform.Find("BoxRight/Tip").GetComponentInChildren<Text>().text = content;
- }
- void HideBtnBack() {
- this.transform.Find("Back").gameObject.SetActive(false);
- }
- [NonSerialized] public bool banBackBtnLogic;
- public void Back() {
- AudioMgr.ins.PlayBtn();
- if (banBackBtnLogic) return;
- Destroy(this.gameObject);
- }
- [NonSerialized] public System.Action eventOnRejectPKInvite;
- }
|