using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; /* Socket组件-玩家PK */ public class SocketPlayer : JC.SocketIO.SocketIOClient { public static SocketPlayer ins; public static SocketPlayer NewInstance() { return new GameObject("SocketPlayer").AddComponent(); } void Awake() { ins = this; } public override void OnDestroy() { if (ins == this) ins = null; base.OnDestroy(); } void Start() { connectServer(CommonConfig.gamePKServerWsURL); } public override void onLoad() { Debug.Log("onLoad"); RequestEnterRoom(); } public override void onReload() { Debug.Log("onReload"); } public override void onDestroy() { Debug.Log("onDestroy"); if (OnlinePKTest.isOpen) { return; } if (!isGameOver) { PopupMgr.ins.ShowBGTip(TextAutoLanguage2.GetTextByCNKey("某方退出或掉线,联机游戏终止!")); SceneManager.LoadScene("Home", LoadSceneMode.Single); } } public override void onMiss() { Debug.Log("onMiss"); } //请求加入房间 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 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; } onRoomReadyComplete?.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); } [NonSerialized] public bool isGameOver = false; }