|
|
@@ -8,154 +8,85 @@ using UnityEngine.SceneManagement;
|
|
|
public class SocketPlayer : JC.SocketIO.SocketIOClient
|
|
|
{
|
|
|
public static SocketPlayer ins;
|
|
|
+
|
|
|
+ public static SocketPlayer NewInstance() {
|
|
|
+ return new GameObject("SocketPlayer").AddComponent<SocketPlayer>();
|
|
|
+ }
|
|
|
+
|
|
|
void Awake()
|
|
|
{
|
|
|
ins = this;
|
|
|
- GlobalEventCenter.ins.onGameSceneLoad += ResetTryAgain;
|
|
|
- GlobalEventCenter.ins.onGameSceneDestroy += DestroySelf;
|
|
|
}
|
|
|
+
|
|
|
public override void OnDestroy()
|
|
|
{
|
|
|
if (ins == this) ins = null;
|
|
|
- GlobalEventCenter.ins.onGameSceneLoad -= ResetTryAgain;
|
|
|
- GlobalEventCenter.ins.onGameSceneDestroy -= DestroySelf;
|
|
|
base.OnDestroy();
|
|
|
}
|
|
|
- void DestroySelf() {
|
|
|
- if (willTryAgain) {
|
|
|
- return;
|
|
|
- }
|
|
|
- if (gameObject) Destroy(gameObject);
|
|
|
- }
|
|
|
|
|
|
void Start()
|
|
|
{
|
|
|
- openInGameScene = SceneManager.GetActiveScene().name.StartsWith("Game");
|
|
|
connectServer(CommonConfig.gamePKServerWsURI);
|
|
|
}
|
|
|
- public Action onLoad_;
|
|
|
+
|
|
|
public override void onLoad()
|
|
|
{
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- //方便测试
|
|
|
- public bool openInGameScene;
|
|
|
- //测试阶段,用这个
|
|
|
- public void onLoadForTest(int id, string nickname, int avatarID) {
|
|
|
- if (openInGameScene) {
|
|
|
- LoginMgr.myUserInfo.id = id;
|
|
|
- LoginMgr.myUserInfo.nickname = nickname;
|
|
|
- LoginMgr.myUserInfo.avatarID = avatarID;
|
|
|
- }
|
|
|
Debug.Log("onLoad");
|
|
|
- onLoad_?.Invoke();
|
|
|
+ RequestEnterRoom();
|
|
|
}
|
|
|
+
|
|
|
public override void onReload()
|
|
|
{
|
|
|
Debug.Log("onReload");
|
|
|
}
|
|
|
- public Action onDestroy_;
|
|
|
+
|
|
|
public override void onDestroy()
|
|
|
{
|
|
|
Debug.Log("onDestroy");
|
|
|
- onDestroy_?.Invoke();
|
|
|
- if (!isGameOver && UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("Game") && !openInGameScene) {
|
|
|
+ if (OnlinePKTest.isOpen) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!isGameOver) {
|
|
|
PopupMgr.ins.ShowBGTip("某方退出或掉线,联机游戏终止!");
|
|
|
SceneManager.LoadScene("Home", LoadSceneMode.Single);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
public override void onMiss()
|
|
|
{
|
|
|
Debug.Log("onMiss");
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- //上传玩家用户匹配信息
|
|
|
- public void UploadPlayerInfo() {
|
|
|
- UserInfo userInfo = LoginMgr.myUserInfo;
|
|
|
- call("UploadPlayerInfo", userInfo.id, userInfo.nickname, userInfo.avatarID);
|
|
|
- }
|
|
|
- //匹配成功后,大家同一开始,才进入游戏场景
|
|
|
- public void AgreeStartGame() {
|
|
|
- call("AgreeStartGame");
|
|
|
- }
|
|
|
- public Action onAgreeStartGame;
|
|
|
- public void OnAgreeStartGame() {
|
|
|
- onAgreeStartGame?.Invoke();
|
|
|
- }
|
|
|
- //随机匹配
|
|
|
- public void RandomMatchRoom() {
|
|
|
- call("RandomMatchRoom", GlobalData.matchRoomType);
|
|
|
- }
|
|
|
- public Action onMatchSuccess;
|
|
|
- public void OnMatchSuccess(List<MatchPlayerInfo> matchPlayerInfos, int roomID) {
|
|
|
- GlobalData.roomID = roomID;
|
|
|
- GlobalData.matchPlayerInfos = matchPlayerInfos;
|
|
|
- for (int i = 0; i < matchPlayerInfos.Count; i++) {
|
|
|
- if (matchPlayerInfos[i].playerID == LoginMgr.myUserInfo.id) {
|
|
|
- GlobalData.playerIndexInRoom = i;
|
|
|
- }
|
|
|
+ //请求加入房间
|
|
|
+ public void RequestEnterRoom() {
|
|
|
+ call("RequestEnterRoom", OnlinePKTest.isOpen ? "test" : GlobalData.roomKey);
|
|
|
+ }
|
|
|
+ //房间准备完毕,即房间内的玩家都准备好了,接到该通知就可以开始游戏啦
|
|
|
+ public Action onRoomReadyComplete;
|
|
|
+ public void OnRoomReadyComplete(int playerIndexInRoom) {
|
|
|
+ Debug.Log("OnRoomReadyComplete" + ", playerIndexInRoom: " + playerIndexInRoom);
|
|
|
+ if (OnlinePKTest.isOpen) {
|
|
|
+ GlobalData.matchPlayerInfos = new List<MatchPlayerInfo>();
|
|
|
+ MatchPlayerInfo p1 = new MatchPlayerInfo();
|
|
|
+ p1.nickname = "玩家1";
|
|
|
+ p1.avatarID = 10;
|
|
|
+ GlobalData.matchPlayerInfos.Add(p1);
|
|
|
+ MatchPlayerInfo p2 = new MatchPlayerInfo();
|
|
|
+ p2.nickname = "玩家2";
|
|
|
+ p2.avatarID = 20;
|
|
|
+ GlobalData.matchPlayerInfos.Add(p2);
|
|
|
+ GlobalData.playerIndexInRoom = playerIndexInRoom;
|
|
|
}
|
|
|
- onMatchSuccess?.Invoke();
|
|
|
+ onRoomReadyComplete?.Invoke();
|
|
|
}
|
|
|
//上传游戏数据
|
|
|
public void UploadPKGameData(string key, object data) {
|
|
|
- if (willTryAgain) return;
|
|
|
if (!isValid) return;
|
|
|
- call("UploadPKGameData", repeatID.ToString() + key, data);
|
|
|
+ call("UploadPKGameData", key, data);
|
|
|
}
|
|
|
public Action<string, string> onReceivePKGameData;
|
|
|
public void OnReceivePKGameData(string key, string data) {
|
|
|
- if (key.StartsWith(repeatID_str)) {
|
|
|
- key = key.Substring(repeatID_str.Length);
|
|
|
- } else {
|
|
|
- return;
|
|
|
- }
|
|
|
- try
|
|
|
- {
|
|
|
- onReceivePKGameData?.Invoke(key, data);
|
|
|
- }
|
|
|
- catch (System.Exception e)
|
|
|
- {
|
|
|
- Debug.LogError(e.Message);
|
|
|
- Debug.LogError(e.StackTrace);
|
|
|
- }
|
|
|
- }
|
|
|
- //创建好友PK房间
|
|
|
- public void CreateFriendPKRoom() {
|
|
|
- call("CreateFriendPKRoom", GlobalData.matchRoomType);
|
|
|
- }
|
|
|
- public Action<int> onCreateFriendPKRoom;
|
|
|
- public void OnCreateFriendPKRoom(int roomID) {
|
|
|
- onCreateFriendPKRoom?.Invoke(roomID);
|
|
|
- }
|
|
|
- //加入好友PK房间
|
|
|
- public void JoinFriendPKRoom(int roomID) {
|
|
|
- call("JoinFriendPKRoom", roomID);
|
|
|
+ onReceivePKGameData?.Invoke(key, data);
|
|
|
}
|
|
|
- //游戏结束标记
|
|
|
[NonSerialized] public bool isGameOver = false;
|
|
|
- //再来一次
|
|
|
- bool willTryAgain = false;
|
|
|
- int repeatID = 0;
|
|
|
- string repeatID_str = "0";
|
|
|
- public void TryAgain(string sceneName) {
|
|
|
- isGameOver = false;
|
|
|
- willTryAgain = true;
|
|
|
- repeatID++;
|
|
|
- repeatID_str = repeatID.ToString();
|
|
|
- call("TryAgain", repeatID_str, sceneName);
|
|
|
- }
|
|
|
- public void OnTryAgain(string repeatID_s, string sceneName) {
|
|
|
- if (repeatID_str == repeatID_s) return;
|
|
|
- repeatID = int.Parse(repeatID_s);
|
|
|
- repeatID_str = repeatID_s;
|
|
|
- willTryAgain = true;
|
|
|
- isGameOver = false;
|
|
|
- SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
|
|
|
- }
|
|
|
- void ResetTryAgain() {
|
|
|
- willTryAgain = false;
|
|
|
- }
|
|
|
}
|