SocketPlayer.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7. public class SocketPlayer : JC.SocketIO.SocketIOClient
  8. {
  9. public static SocketPlayer ins;
  10. void Awake()
  11. {
  12. ins = this;
  13. GlobalEventCenter.ins.onGameSceneDestroy += DestroySelf;
  14. }
  15. public override void OnDestroy()
  16. {
  17. if (ins == this) ins = null;
  18. GlobalEventCenter.ins.onGameSceneDestroy -= DestroySelf;
  19. base.OnDestroy();
  20. }
  21. void DestroySelf() {
  22. if (gameObject) Destroy(gameObject);
  23. }
  24. void Start()
  25. {
  26. openInGameScene = SceneManager.GetActiveScene().name.StartsWith("Game");
  27. connectServer(CommonConfig.gamePKServerWsURI);
  28. }
  29. public Action onLoad_;
  30. public override void onLoad()
  31. {
  32. }
  33. //方便测试
  34. public bool openInGameScene;
  35. //测试阶段,用这个
  36. public void onLoadForTest(int id, string nickname, int avatarID) {
  37. if (openInGameScene) {
  38. LoginMgr.myUserInfo.id = id;
  39. LoginMgr.myUserInfo.nickname = nickname;
  40. LoginMgr.myUserInfo.avatarID = avatarID;
  41. }
  42. Debug.Log("onLoad");
  43. onLoad_?.Invoke();
  44. }
  45. public override void onReload()
  46. {
  47. Debug.Log("onReload");
  48. }
  49. public Action onDestroy_;
  50. [NonSerialized] public bool canBackHome = true;
  51. public override void onDestroy()
  52. {
  53. Debug.Log("onDestroy");
  54. onDestroy_?.Invoke();
  55. if (canBackHome && UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("Game") && !openInGameScene) {
  56. PopupMgr.ins.ShowBGTip("某方退出或掉线,联机游戏终止!");
  57. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  58. }
  59. }
  60. public override void onMiss()
  61. {
  62. Debug.Log("onMiss");
  63. }
  64. //上传玩家用户匹配信息
  65. public void UploadPlayerInfo() {
  66. UserInfo userInfo = LoginMgr.myUserInfo;
  67. call("UploadPlayerInfo", userInfo.id, userInfo.nickname, userInfo.avatarID);
  68. }
  69. //匹配成功后,大家同一开始,才进入游戏场景
  70. public void AgreeStartGame() {
  71. call("AgreeStartGame");
  72. }
  73. public Action onAgreeStartGame;
  74. public void OnAgreeStartGame() {
  75. onAgreeStartGame?.Invoke();
  76. }
  77. //随机匹配
  78. public void RandomMatchRoom() {
  79. call("RandomMatchRoom", GlobalData.matchRoomType);
  80. }
  81. public Action onMatchSuccess;
  82. public void OnMatchSuccess(List<MatchPlayerInfo> matchPlayerInfos, int roomID) {
  83. GlobalData.roomID = roomID;
  84. GlobalData.matchPlayerInfos = matchPlayerInfos;
  85. for (int i = 0; i < matchPlayerInfos.Count; i++) {
  86. if (matchPlayerInfos[i].playerID == LoginMgr.myUserInfo.id) {
  87. GlobalData.playerIndexInRoom = i;
  88. }
  89. }
  90. onMatchSuccess?.Invoke();
  91. }
  92. //上传游戏数据
  93. public void UploadPKGameData(string key, object data) {
  94. if (!isValid) return;
  95. call("UploadPKGameData", key, data);
  96. }
  97. public Action<string, string> onReceivePKGameData;
  98. public void OnReceivePKGameData(string key, string data) {
  99. onReceivePKGameData?.Invoke(key, data);
  100. }
  101. //创建好友PK房间
  102. public void CreateFriendPKRoom() {
  103. call("CreateFriendPKRoom", GlobalData.matchRoomType);
  104. }
  105. public Action<int> onCreateFriendPKRoom;
  106. public void OnCreateFriendPKRoom(int roomID) {
  107. onCreateFriendPKRoom?.Invoke(roomID);
  108. }
  109. //加入好友PK房间
  110. public void JoinFriendPKRoom(int roomID) {
  111. call("JoinFriendPKRoom", roomID);
  112. }
  113. }