SocketPlayer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. [NonSerialized] public bool canBackHome = true;
  55. public override void onDestroy()
  56. {
  57. Debug.Log("onDestroy");
  58. onDestroy_?.Invoke();
  59. if (canBackHome && 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 (!isValid) return;
  99. call("UploadPKGameData", repeatID.ToString() + key, data);
  100. }
  101. public Action<string, string> onReceivePKGameData;
  102. public void OnReceivePKGameData(string key, string data) {
  103. if (key.StartsWith(repeatID_str)) {
  104. key = key.Substring(repeatID_str.Length);
  105. } else {
  106. return;
  107. }
  108. onReceivePKGameData?.Invoke(key, data);
  109. }
  110. //创建好友PK房间
  111. public void CreateFriendPKRoom() {
  112. call("CreateFriendPKRoom", GlobalData.matchRoomType);
  113. }
  114. public Action<int> onCreateFriendPKRoom;
  115. public void OnCreateFriendPKRoom(int roomID) {
  116. onCreateFriendPKRoom?.Invoke(roomID);
  117. }
  118. //加入好友PK房间
  119. public void JoinFriendPKRoom(int roomID) {
  120. call("JoinFriendPKRoom", roomID);
  121. }
  122. //再来一次
  123. bool willTryAgain = false;
  124. int repeatID = 0;
  125. string repeatID_str = "0";
  126. public void TryAgain(string sceneName) {
  127. willTryAgain = true;
  128. repeatID++;
  129. repeatID_str = repeatID.ToString();
  130. call("TryAgain", repeatID_str, sceneName);
  131. }
  132. public void OnTryAgain(string repeatID_s, string sceneName) {
  133. if (repeatID_str == repeatID_s) return;
  134. repeatID = int.Parse(repeatID_s);
  135. repeatID_str = repeatID_s;
  136. willTryAgain = true;
  137. SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
  138. }
  139. }