SocketPlayer.cs 4.9 KB

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