lvjincheng 3 лет назад
Родитель
Сommit
f2eb24b6f0
2 измененных файлов с 40 добавлено и 89 удалено
  1. 9 4
      Assets/BowArrow/Scripts/GlobalData.cs
  2. 31 85
      Assets/BowArrow/Scripts/View/PKMatchingView.cs

+ 9 - 4
Assets/BowArrow/Scripts/GlobalData.cs

@@ -8,9 +8,8 @@ public class GlobalData
     public static PKMatchType pkMatchType = PKMatchType.None;
     //本地双人PK,双方选择的角色ID
     public static int[] localPK_playerRoleIDs = new int[] {1, 2};
-    //双人PK匹配结果
-    public static int matchRoomType = 0; //Pk匹配房间类型
-    public static int roomID;
+    public static string roomKey;
+    public static int matchGameType;
     public static int playerIndexInRoom;
     public static List<MatchPlayerInfo> matchPlayerInfos;
 }
@@ -20,7 +19,7 @@ public class GlobalData
 public class GlobalDataTemp
 {
     public static PKMatchType pkMatchType = PKMatchType.None;
-    public static int matchRoomType = 0; //Pk匹配房间类型
+    public static int matchGameType;
 }
 public enum PKMatchType {
     None, //不是PK,就是单击单人
@@ -31,4 +30,10 @@ public enum PKMatchType {
     public int playerID;
     public int avatarID;
     public string nickname;
+    public MatchPlayerInfo() {}
+    public MatchPlayerInfo(int playerID, int avatarID, string nickname) {
+        this.playerID = playerID;
+        this.avatarID = avatarID;
+        this.nickname = nickname;
+    }
 }

+ 31 - 85
Assets/BowArrow/Scripts/View/PKMatchingView.cs

@@ -14,10 +14,10 @@ public class PKMatchingView : MonoBehaviour
     SocketPlayer socketPlayer;
     
     //邀请者需要输入的信息
-    public PKFriendInfoPack pkFriendInfoPack = null;
+    [NonSerialized] public int targetInvitePlayerID;
     [NonSerialized] public bool isFriendPKInviter;
     //被邀请者需要输入的信息
-    [NonSerialized] public int targetJoinRoomID;
+    [NonSerialized] public string targetJoinRoomKey;
     [NonSerialized] public bool isFriendPKInvitee;  
 
     public static PKMatchingView Create() {
@@ -34,6 +34,7 @@ public class PKMatchingView : MonoBehaviour
 
     public void OnDestroy() {
         if (ins == this) ins = null;
+        PKComp.ins.cancelRandomMatch();
     }
 
     void Start()
@@ -48,90 +49,46 @@ public class PKMatchingView : MonoBehaviour
         } else {
             RenderTip("正在搜索实力相当的对手···");
         }
+        
+        if (isFriendPKInviter) {
+            PKComp.ins.inviteFriendGamePK(targetInvitePlayerID);
+        } else if (isFriendPKInvitee) {
+            PKComp.ins.acceptFriendGamePK(targetJoinRoomKey);
+        } else {
+            PKComp.ins.randomMatch();
+        }
+    }
+
+    public void EnterGameSceneOnMatchSuccess() {
+        int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2;
+        MatchPlayerInfo info = GlobalData.matchPlayerInfos[otherIndex];
+        RenderPlayerInfo(2, RoleMgr.GetAvatar(info.avatarID), info.nickname, true);
+        RenderTip("匹配成功,即将开始游戏!");
+        HideBtnBack();
+        PauseWaitingTime();
+        JC.Unity.CoroutineStarter.Start(LoadSceneOnEnterGame());
+    }
 
-        socketPlayer = new GameObject("SocketPlayer").AddComponent<SocketPlayer>();
-        socketPlayer.onLoad_ = () => {
-            socketPlayer.UploadPlayerInfo();
-            if (isFriendPKInviter) {
-                socketPlayer.CreateFriendPKRoom();
-            } else if (isFriendPKInvitee) {
-                socketPlayer.JoinFriendPKRoom(targetJoinRoomID);
-            } else {
-                socketPlayer.RandomMatchRoom();    
-            }
-        };
-        socketPlayer.onCreateFriendPKRoom = (roomID) => {
-            UserPlayer.ins.call("friendComp.invitePK", 
-                pkFriendInfoPack.id, pkFriendInfoPack.avatarID, pkFriendInfoPack.nickname, 
-                roomID, GlobalData.matchRoomType
-            );
-        };
-        socketPlayer.onMatchSuccess = () => {
-            int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2;
-            MatchPlayerInfo info = GlobalData.matchPlayerInfos[otherIndex];
-            (Sprite avatar, string nickname)  =  RoleMgr.GetRoleInfo(info.avatarID);
-            nickname = info.nickname;
-            RenderPlayerInfo(2, avatar, nickname, true);
-            RenderTip("匹配成功,即将开始游戏!");
-            HideBtnBack();
-            PauseWaitingTime();
-            socketPlayer.AgreeStartGame();
-        };
-        socketPlayer.onAgreeStartGame = () => {
-            DontDestroyOnLoad(socketPlayer);
-            toLoadGame = true;
-        };
-        socketPlayer.onDestroy_ += () => {
-            if (this && this.gameObject) {
-                Destroy(this.gameObject);
-                Destroy(socketPlayer.gameObject);
-                PopupMgr.ins.ShowBGTip("网络发生意外!");
-            }
-        };
+    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);
+        }
     }
 
     //通过此方法,不需要经过匹配页面进行匹配,可以直接在游戏场景匹配,方便测试
     public static void MoniMatchForTestInGameScene(Action onAgreeStartGame) {
-        SocketPlayer socketPlayer = new GameObject("SocketPlayer").AddComponent<SocketPlayer>();
-        socketPlayer.onLoad_ = () => {
-            socketPlayer.UploadPlayerInfo();
-            socketPlayer.RandomMatchRoom();
-        };
-        socketPlayer.onMatchSuccess = () => {
-            socketPlayer.AgreeStartGame();
-        };
-        socketPlayer.onAgreeStartGame = () => {
-            DontDestroyOnLoad(socketPlayer);
-            if (onAgreeStartGame != null) onAgreeStartGame();
-        };
+
     }
 
-    bool toLoadGame = false;
-    float loadGameSceneCountdown = 1;
     void Update() {
         if (waitingTime >= 0) {
             waitingTime += Time.deltaTime;
             this.transform.Find("BoxRight/TimeBG").GetComponentInChildren<Text>().text = TimeUtil.GetTimeStr(waitingTime, false);
         }
-        if (toLoadGame) {
-            loadGameSceneCountdown -= Time.deltaTime;
-            if (loadGameSceneCountdown <= 0) {
-                toLoadGame = false;
-                //loadscene
-                if (GlobalData.matchRoomType == 0) {
-                    GameMgr.gameType = 9;
-                    SceneManager.LoadScene("Game", LoadSceneMode.Single);
-                }
-                //challenge-game
-                int[] roomTypes = {1, 2, 3};
-                int[] gameTypes = {10, 11, 12};
-                int curIndex = Array.IndexOf(roomTypes, GlobalData.matchRoomType);
-                if (curIndex >= 0) {
-                    GameMgr.gameType = gameTypes[curIndex];
-                    SceneManager.LoadScene("GameChallenge", LoadSceneMode.Single);
-                }
-            }
-        }
     }
 
     void PauseWaitingTime() {
@@ -166,14 +123,3 @@ public class PKMatchingView : MonoBehaviour
         Destroy(socketPlayer.gameObject);
     }
 }
-public class PKFriendInfoPack {
-    public int id;
-    public int avatarID;
-    public string nickname = ""; 
-    public PKFriendInfoPack() {}
-    public PKFriendInfoPack(int id, int avatarID, string nickname) {
-        this.id = id;
-        this.avatarID = avatarID;
-        this.nickname = nickname;
-    }
-}