SocketPlayer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 (willTryAgain) {
  23. willTryAgain = false;
  24. return;
  25. }
  26. if (gameObject) Destroy(gameObject);
  27. }
  28. void Start()
  29. {
  30. openInGameScene = SceneManager.GetActiveScene().name.StartsWith("Game");
  31. connectServer(CommonConfig.gamePKServerWsURI);
  32. }
  33. public Action onLoad_;
  34. public override void onLoad()
  35. {
  36. }
  37. //方便测试
  38. public bool openInGameScene;
  39. //测试阶段,用这个
  40. public void onLoadForTest(int id, string nickname, int avatarID) {
  41. if (openInGameScene) {
  42. LoginMgr.myUserInfo.id = id;
  43. LoginMgr.myUserInfo.nickname = nickname;
  44. LoginMgr.myUserInfo.avatarID = avatarID;
  45. }
  46. Debug.Log("onLoad");
  47. onLoad_?.Invoke();
  48. }
  49. public override void onReload()
  50. {
  51. Debug.Log("onReload");
  52. }
  53. public Action onDestroy_;
  54. public override void onDestroy()
  55. {
  56. Debug.Log("onDestroy");
  57. onDestroy_?.Invoke();
  58. if (!isGameOver && UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("Game") && !openInGameScene) {
  59. PopupMgr.ins.ShowBGTip("某方退出或掉线,联机游戏终止!");
  60. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  61. }
  62. }
  63. public override void onMiss()
  64. {
  65. Debug.Log("onMiss");
  66. }
  67. //上传玩家用户匹配信息
  68. public void UploadPlayerInfo() {
  69. UserInfo userInfo = LoginMgr.myUserInfo;
  70. call("UploadPlayerInfo", userInfo.id, userInfo.nickname, userInfo.avatarID);
  71. }
  72. //匹配成功后,大家同一开始,才进入游戏场景
  73. public void AgreeStartGame() {
  74. call("AgreeStartGame");
  75. }
  76. public Action onAgreeStartGame;
  77. public void OnAgreeStartGame() {
  78. onAgreeStartGame?.Invoke();
  79. }
  80. //随机匹配
  81. public void RandomMatchRoom() {
  82. call("RandomMatchRoom", GlobalData.matchRoomType);
  83. }
  84. public Action onMatchSuccess;
  85. public void OnMatchSuccess(List<MatchPlayerInfo> matchPlayerInfos, int roomID) {
  86. GlobalData.roomID = roomID;
  87. GlobalData.matchPlayerInfos = matchPlayerInfos;
  88. for (int i = 0; i < matchPlayerInfos.Count; i++) {
  89. if (matchPlayerInfos[i].playerID == LoginMgr.myUserInfo.id) {
  90. GlobalData.playerIndexInRoom = i;
  91. }
  92. }
  93. onMatchSuccess?.Invoke();
  94. }
  95. //上传游戏数据
  96. public void UploadPKGameData(string key, object data) {
  97. if (!isValid) return;
  98. call("UploadPKGameData", repeatID.ToString() + key, data);
  99. }
  100. public Action<string, string> onReceivePKGameData;
  101. public void OnReceivePKGameData(string key, string data) {
  102. if (key.StartsWith(repeatID_str)) {
  103. key = key.Substring(repeatID_str.Length);
  104. } else {
  105. return;
  106. }
  107. try
  108. {
  109. onReceivePKGameData?.Invoke(key, data);
  110. }
  111. catch (System.Exception e)
  112. {
  113. Debug.LogError(e.Message);
  114. Debug.LogError(e.StackTrace);
  115. }
  116. }
  117. //创建好友PK房间
  118. public void CreateFriendPKRoom() {
  119. call("CreateFriendPKRoom", GlobalData.matchRoomType);
  120. }
  121. public Action<int> onCreateFriendPKRoom;
  122. public void OnCreateFriendPKRoom(int roomID) {
  123. onCreateFriendPKRoom?.Invoke(roomID);
  124. }
  125. //加入好友PK房间
  126. public void JoinFriendPKRoom(int roomID) {
  127. call("JoinFriendPKRoom", roomID);
  128. }
  129. //游戏结束标记
  130. [NonSerialized] public bool isGameOver = false;
  131. //再来一次
  132. bool willTryAgain = false;
  133. int repeatID = 0;
  134. string repeatID_str = "0";
  135. public void TryAgain(string sceneName) {
  136. isGameOver = false;
  137. willTryAgain = true;
  138. repeatID++;
  139. repeatID_str = repeatID.ToString();
  140. call("TryAgain", repeatID_str, sceneName);
  141. }
  142. public void OnTryAgain(string repeatID_s, string sceneName) {
  143. if (repeatID_str == repeatID_s) return;
  144. repeatID = int.Parse(repeatID_s);
  145. repeatID_str = repeatID_s;
  146. willTryAgain = true;
  147. isGameOver = false;
  148. SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
  149. }
  150. }