using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class SocketPlayer : JC.SocketIO.SocketIOClient { void Awake() { InitDestroySelf(); } void InitDestroySelf() { GlobalEventCenter.ins.onGameSceneDestroy += DestroySelf; } void DestroySelf() { GlobalEventCenter.ins.onGameSceneDestroy -= DestroySelf; if (gameObject) Destroy(gameObject); } 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(); } public override void onReload() { Debug.Log("onReload"); } public override void onDestroy() { Debug.Log("onDestroy"); } 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", key, data); } public Action onReceivePKGameData; public void OnReceivePKGameData(string key, string data) { onReceivePKGameData?.Invoke(key, data); } }