SessionPlayerComponentSystem.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace ET.Server
  4. {
  5. [FriendOf(typeof (SessionPlayerComponent))]
  6. public static class SessionPlayerComponentSystem
  7. {
  8. public class SessionPlayerComponentAwakeSystem: AwakeSystem<SessionPlayerComponent>
  9. {
  10. protected override void Awake(SessionPlayerComponent self)
  11. {
  12. self.PlayerList = new List<PlayerInfo>();
  13. self.PlayerId = 0;
  14. }
  15. }
  16. public class SessionPlayerComponentDestroySystem: DestroySystem<SessionPlayerComponent>
  17. {
  18. protected override void Destroy(SessionPlayerComponent self)
  19. {
  20. WNPlayer player = self.GetMyPlayer();
  21. if (player == null)
  22. {
  23. return;
  24. }
  25. Log.Info($"玩家断线了, playerId={self.PlayerId}, name={player.GetName()}");
  26. // 玩家离开
  27. player.GetComponent<PlayerTempDataComponent>().MapData.ready = false;
  28. Map map = player.Map;
  29. if (map != null)
  30. {
  31. // 记录玩家历史
  32. map.SyncPlayerHistoryData(player);
  33. // 场景移除玩家
  34. map.RemovePlayer(player, false);
  35. map.PlayerLeaveRequest(player, false);
  36. }
  37. self.DomainScene().GetComponent<GamePlayerComponent>().Remove(player.GetId());
  38. player.Dispose();
  39. }
  40. }
  41. /// <summary>
  42. /// 是否存在
  43. /// </summary>
  44. /// <param name="self"></param>
  45. /// <param name="playerId"></param>
  46. /// <returns></returns>
  47. public static bool IsExist(this SessionPlayerComponent self, long playerId)
  48. {
  49. return self.PlayerList.Any(info => info != null && info.PlayerId == playerId);
  50. }
  51. /// <summary>
  52. /// 获取
  53. /// </summary>
  54. /// <param name="self"></param>
  55. /// <param name="playerId"></param>
  56. /// <returns></returns>
  57. public static PlayerInfo Get(this SessionPlayerComponent self, long playerId)
  58. {
  59. return self.PlayerList.FirstOrDefault(info => info != null && info.PlayerId == playerId);
  60. }
  61. /// <summary>
  62. /// 添加
  63. /// </summary>
  64. /// <param name="self"></param>
  65. /// <param name="info"></param>
  66. public static void Add(this SessionPlayerComponent self, PlayerInfo info)
  67. {
  68. if (info != null)
  69. {
  70. self.PlayerList.Add(info);
  71. }
  72. }
  73. /// <summary>
  74. /// 获取玩家实体
  75. /// </summary>
  76. /// <param name="self"></param>
  77. /// <returns></returns>
  78. public static WNPlayer GetMyPlayer(this SessionPlayerComponent self)
  79. {
  80. return self.DomainScene().GetComponent<GamePlayerComponent>().Get(self.PlayerId);
  81. }
  82. }
  83. }