SocketPlayer.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. void Awake()
  10. {
  11. GlobalEventCenter.ins.onGameSceneDestroy += DestroySelf;
  12. }
  13. public override void OnDestroy()
  14. {
  15. GlobalEventCenter.ins.onGameSceneDestroy -= DestroySelf;
  16. base.OnDestroy();
  17. }
  18. void DestroySelf() {
  19. if (gameObject) Destroy(gameObject);
  20. }
  21. void Start()
  22. {
  23. openInGameScene = SceneManager.GetActiveScene().name.StartsWith("Game");
  24. // connectServer("ws://192.168.101.14:11811/SmartBowGameServer");
  25. connectServer("ws://81.69.16.183:11811/SmartBowGameServer");
  26. }
  27. public Action onLoad_;
  28. public override void onLoad()
  29. {
  30. }
  31. //方便测试
  32. public bool openInGameScene;
  33. //测试阶段,用这个
  34. public void onLoadForTest(int id, string nickname, int avatarID) {
  35. LoginMgr.myUserInfo.id = id;
  36. if (openInGameScene) {
  37. LoginMgr.myUserInfo.nickname = nickname;
  38. LoginMgr.myUserInfo.avatarID = avatarID;
  39. }
  40. Debug.Log("onLoad");
  41. onLoad_?.Invoke();
  42. }
  43. public override void onReload()
  44. {
  45. Debug.Log("onReload");
  46. }
  47. public Action onDestroy_;
  48. [NonSerialized] public bool canBackHome = true;
  49. public override void onDestroy()
  50. {
  51. Debug.Log("onDestroy");
  52. onDestroy_?.Invoke();
  53. if (canBackHome && UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("Game")) {
  54. PopupMgr.ins.ShowTip("某方退出或掉线,联机游戏终止!");
  55. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  56. }
  57. }
  58. public override void onMiss()
  59. {
  60. Debug.Log("onMiss");
  61. }
  62. //上传玩家用户匹配信息
  63. public void UploadPlayerInfo() {
  64. UserInfo userInfo = LoginMgr.myUserInfo;
  65. call("UploadPlayerInfo", userInfo.id, userInfo.nickname, userInfo.avatarID);
  66. }
  67. //匹配成功后,大家同一开始,才进入游戏场景
  68. public void AgreeStartGame() {
  69. call("AgreeStartGame");
  70. }
  71. public Action onAgreeStartGame;
  72. public void OnAgreeStartGame() {
  73. onAgreeStartGame?.Invoke();
  74. }
  75. //随机匹配
  76. public void RandomMatchRoom() {
  77. call("RandomMatchRoom", GlobalData.matchRoomType);
  78. }
  79. public Action onMatchSuccess;
  80. public void OnMatchSuccess(List<MatchPlayerInfo> matchPlayerInfos, int roomID) {
  81. GlobalData.roomID = roomID;
  82. GlobalData.matchPlayerInfos = matchPlayerInfos;
  83. for (int i = 0; i < matchPlayerInfos.Count; i++) {
  84. if (matchPlayerInfos[i].playerID == LoginMgr.myUserInfo.id) {
  85. GlobalData.playerIndexInRoom = i;
  86. }
  87. }
  88. onMatchSuccess?.Invoke();
  89. }
  90. //上传游戏数据
  91. public void UploadPKGameData(string key, object data) {
  92. if (!isValid) return;
  93. call("UploadPKGameData", key, data);
  94. }
  95. public Action<string, string> onReceivePKGameData;
  96. public void OnReceivePKGameData(string key, string data) {
  97. onReceivePKGameData?.Invoke(key, data);
  98. }
  99. }