using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class SocketPlayer : JC.SocketIO.SocketIOClient { public static SocketPlayer ins; void Awake() { ins = this; GlobalEventCenter.ins.onGameSceneDestroy += DestroySelf; } public override void OnDestroy() { if (ins == this) ins = null; GlobalEventCenter.ins.onGameSceneDestroy -= DestroySelf; base.OnDestroy(); } void DestroySelf() { if (willTryAgain) { willTryAgain = false; 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(); } 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) { 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 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; } } onMatchSuccess?.Invoke(); } //上传游戏数据 public void UploadPKGameData(string key, object data) { if (!isValid) return; call("UploadPKGameData", repeatID.ToString() + key, data); } public Action 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 onCreateFriendPKRoom; public void OnCreateFriendPKRoom(int roomID) { onCreateFriendPKRoom?.Invoke(roomID); } //加入好友PK房间 public void JoinFriendPKRoom(int roomID) { call("JoinFriendPKRoom", roomID); } //游戏结束标记 [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); } }