lvjincheng 4 anos atrás
pai
commit
469c413a9d

+ 9 - 3
Assets/BowArrow/Scripts/Manager/GameMode/PKGameMode_OnlinePK.cs

@@ -15,10 +15,17 @@ public class PKGameMode_OnlinePK : GameMode {
     public bool IsMyPlayerRunning() {
         return myPlayerIndex == gameLogic.currentPlayerIndex;
     }
-
     public PKGameMode_OnlinePK(GameMgr gameMgr) : base(gameMgr) {
         GlobalData.pkMatchType = PKMatchType.OnlinePK;
-        myPlayerIndex = GlobalData.playerIndexInRoom;
+
+        
+        if (GameObject.Find("SocketPlayer") == null) {
+            PKMatchingView.MoniMatchForTestInGameScene(() => {
+                 myPlayerIndex = GlobalData.playerIndexInRoom;
+            });
+        } else {
+            myPlayerIndex = GlobalData.playerIndexInRoom;
+        }
 
         gameLogic.InitAppearPlayerIndexes();
         gameLogic.SetCurrentPlayerIndexToNext();
@@ -34,7 +41,6 @@ public class PKGameMode_OnlinePK : GameMode {
 
         AutoSwitchBanUserControlBow();
 
-        // socketPlayer = new GameObject("SocketPlayer").AddComponent<SocketPlayer>();
         socketPlayer = GameObject.Find("SocketPlayer").GetComponent<SocketPlayer>();
         socketPlayer.onReceivePKGameData = onReceivePKGameData;
     }

+ 18 - 3
Assets/BowArrow/Scripts/Network/SocketPlayer.cs

@@ -3,6 +3,7 @@ using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
+using UnityEngine.SceneManagement;
 
 public class SocketPlayer : JC.SocketIO.SocketIOClient
 {
@@ -19,11 +20,24 @@ public class SocketPlayer : JC.SocketIO.SocketIOClient
     }
     void Start()
     {
+        openInGameScene = SceneManager.GetActiveScene().name.StartsWith("Game");
         connectServer("ws://192.168.101.14:8000/SmartBowGameServer");
     }
     public Action onLoad_;
     public override void onLoad()
     {
+        
+    }
+
+    //方便测试
+    public bool openInGameScene;
+    //测试阶段,用这个
+    public void onLoadForTest(int id, string nickname, int avatarID) {
+        LoginMgr.myUserInfo.id = id;
+        if (openInGameScene) {
+            LoginMgr.myUserInfo.nickname = nickname;
+            LoginMgr.myUserInfo.avatarID = avatarID;
+        }
         Debug.Log("onLoad");
         onLoad_?.Invoke();
     }
@@ -40,12 +54,13 @@ public class SocketPlayer : JC.SocketIO.SocketIOClient
         Debug.Log("onMiss");
     }
 
-    //主动接口
+    
+    //上传玩家用户匹配信息
     public void UploadPlayerInfo() {
         UserInfo userInfo = LoginMgr.myUserInfo;
-        userInfo.id = int.Parse(JC.CS.Utility.GetTimestamp().ToString().Substring(4));
-        call("UploadPlayerInfo", userInfo.id, "P" + userInfo.id,  UnityEngine.Random.Range(0, 7));
+        call("UploadPlayerInfo", userInfo.id, userInfo.nickname,  userInfo.avatarID);
     }
+    //匹配成功后,大家同一开始,才进入游戏场景
     public void AgreeStartGame() {
         call("AgreeStartGame");
     }

+ 20 - 0
Assets/BowArrow/Scripts/View/PKMatchingView.cs

@@ -50,6 +50,26 @@ public class PKMatchingView : MonoBehaviour
         };
     }
 
+    //通过此方法,不需要经过匹配页面进行匹配,可以直接在游戏场景匹配,方便测试
+    public static void MoniMatchForTestInGameScene(Action onAgreeStartGame) {
+        SocketPlayer socketPlayer = new GameObject("SocketPlayer").AddComponent<SocketPlayer>();
+        socketPlayer.onLoad_ = () => {
+            socketPlayer.UploadPlayerInfo();
+            socketPlayer.RandomMatchRoom();
+        };
+        socketPlayer.onMatchSuccess = () => {
+            int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2;
+            MatchPlayerInfo info = GlobalData.matchPlayerInfos[otherIndex];
+            (Sprite avatar, string nickname)  =  RoleMgr.GetRoleInfo(info.avatarID);
+            nickname = LoginMgr.myUserInfo.nickname;
+            socketPlayer.AgreeStartGame();
+        };
+        socketPlayer.onAgreeStartGame = () => {
+            DontDestroyOnLoad(socketPlayer);
+            if (onAgreeStartGame != null) onAgreeStartGame();
+        };
+    }
+
     void Update() {
         if (waitingTime >= 0) {
             waitingTime += Time.deltaTime;