| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- 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<SocketPlayer>();
- }
- 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>();
- 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<string, string> onReceivePKGameData;
- public void OnReceivePKGameData(string key, string data) {
- onReceivePKGameData?.Invoke(key, data);
- }
- [NonSerialized] public bool isGameOver = false;
- }
|