| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- 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()
- {
- GlobalEventCenter.ins.onGameSceneDestroy += DestroySelf;
- }
- public override void OnDestroy()
- {
- GlobalEventCenter.ins.onGameSceneDestroy -= DestroySelf;
- base.OnDestroy();
- }
- void DestroySelf() {
- 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_;
- [NonSerialized] public bool canBackHome = true;
- public override void onDestroy()
- {
- Debug.Log("onDestroy");
- onDestroy_?.Invoke();
- if (canBackHome && UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("Game")) {
- 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;
- }
- }
- onMatchSuccess?.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);
- }
- //创建好友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);
- }
- }
|