SocketPlayer.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 override void onDestroy()
  46. {
  47. Debug.Log("onDestroy");
  48. }
  49. public override void onMiss()
  50. {
  51. Debug.Log("onMiss");
  52. }
  53. //上传玩家用户匹配信息
  54. public void UploadPlayerInfo() {
  55. UserInfo userInfo = LoginMgr.myUserInfo;
  56. call("UploadPlayerInfo", userInfo.id, userInfo.nickname, userInfo.avatarID);
  57. }
  58. //匹配成功后,大家同一开始,才进入游戏场景
  59. public void AgreeStartGame() {
  60. call("AgreeStartGame");
  61. }
  62. public Action onAgreeStartGame;
  63. public void OnAgreeStartGame() {
  64. onAgreeStartGame?.Invoke();
  65. }
  66. //随机匹配
  67. public void RandomMatchRoom() {
  68. call("RandomMatchRoom", GlobalData.matchRoomType);
  69. }
  70. public Action onMatchSuccess;
  71. public void OnMatchSuccess(List<MatchPlayerInfo> matchPlayerInfos, int roomID) {
  72. GlobalData.roomID = roomID;
  73. GlobalData.matchPlayerInfos = matchPlayerInfos;
  74. for (int i = 0; i < matchPlayerInfos.Count; i++) {
  75. if (matchPlayerInfos[i].playerID == LoginMgr.myUserInfo.id) {
  76. GlobalData.playerIndexInRoom = i;
  77. }
  78. }
  79. onMatchSuccess?.Invoke();
  80. }
  81. //上传游戏数据
  82. public void UploadPKGameData(string key, object data) {
  83. if (!isValid) return;
  84. call("UploadPKGameData", key, data);
  85. }
  86. public Action<string, string> onReceivePKGameData;
  87. public void OnReceivePKGameData(string key, string data) {
  88. onReceivePKGameData?.Invoke(key, data);
  89. }
  90. }