SocketPlayer.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. public static SocketPlayer NewInstance() {
  11. return new GameObject("SocketPlayer").AddComponent<SocketPlayer>();
  12. }
  13. void Awake()
  14. {
  15. ins = this;
  16. }
  17. public override void OnDestroy()
  18. {
  19. if (ins == this) ins = null;
  20. base.OnDestroy();
  21. }
  22. void Start()
  23. {
  24. connectServer(CommonConfig.gamePKServerWsURI);
  25. }
  26. public override void onLoad()
  27. {
  28. Debug.Log("onLoad");
  29. RequestEnterRoom();
  30. }
  31. public override void onReload()
  32. {
  33. Debug.Log("onReload");
  34. }
  35. public override void onDestroy()
  36. {
  37. Debug.Log("onDestroy");
  38. if (OnlinePKTest.isOpen) {
  39. return;
  40. }
  41. if (!isGameOver) {
  42. PopupMgr.ins.ShowBGTip(TextAutoLanguage2.GetTextByCNKey("某方退出或掉线,联机游戏终止!"));
  43. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  44. }
  45. }
  46. public override void onMiss()
  47. {
  48. Debug.Log("onMiss");
  49. }
  50. //请求加入房间
  51. public void RequestEnterRoom() {
  52. call("RequestEnterRoom", OnlinePKTest.isOpen ? "test" : GlobalData.roomKey);
  53. }
  54. //房间准备完毕,即房间内的玩家都准备好了,接到该通知就可以开始游戏啦
  55. public Action onRoomReadyComplete;
  56. public void OnRoomReadyComplete(int playerIndexInRoom) {
  57. Debug.Log("OnRoomReadyComplete" + ", playerIndexInRoom: " + playerIndexInRoom);
  58. if (OnlinePKTest.isOpen) {
  59. GlobalData.matchPlayerInfos = new List<MatchPlayerInfo>();
  60. MatchPlayerInfo p1 = new MatchPlayerInfo();
  61. p1.nickname = "玩家1";
  62. p1.avatarID = 10;
  63. GlobalData.matchPlayerInfos.Add(p1);
  64. MatchPlayerInfo p2 = new MatchPlayerInfo();
  65. p2.nickname = "玩家2";
  66. p2.avatarID = 20;
  67. GlobalData.matchPlayerInfos.Add(p2);
  68. GlobalData.playerIndexInRoom = playerIndexInRoom;
  69. }
  70. onRoomReadyComplete?.Invoke();
  71. }
  72. //上传游戏数据
  73. public void UploadPKGameData(string key, object data) {
  74. if (!isValid) return;
  75. call("UploadPKGameData", key, data);
  76. }
  77. public Action<string, string> onReceivePKGameData;
  78. public void OnReceivePKGameData(string key, string data) {
  79. onReceivePKGameData?.Invoke(key, data);
  80. }
  81. [NonSerialized] public bool isGameOver = false;
  82. }