SocketPlayer.cs 3.0 KB

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