SessionPlayerComponentSystem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /// <summary>
  11. /// session玩家组件创建
  12. /// </summary>
  13. /// <param name="self"></param>
  14. protected override void Awake(SessionPlayerComponent self)
  15. {
  16. Log.Info($"创建session玩家绑定组件...");
  17. self.PlayerId = 0;
  18. self.PlayerList = new List<PlayerInfo>();
  19. }
  20. }
  21. public class SessionPlayerComponentDestroySystem: DestroySystem<SessionPlayerComponent>
  22. {
  23. protected override void Destroy(SessionPlayerComponent self)
  24. {
  25. WNPlayer player = self.GetMyPlayer();
  26. if (player == null)
  27. {
  28. return;
  29. }
  30. Log.Info($"玩家断线了, playerId={self.PlayerId}, name={player.GetName()}");
  31. // 玩家离开
  32. player.GetComponent<PlayerTempDataComponent>().MapData.ready = false;
  33. Map map = player.Map;
  34. if (map != null)
  35. {
  36. // 记录玩家历史
  37. map.SyncPlayerHistoryData(player);
  38. // 场景移除玩家
  39. map.RemovePlayer(player, false);
  40. map.PlayerLeaveRequest(player, false);
  41. }
  42. self.DomainScene().GetComponent<GamePlayerComponent>().Remove(player.GetId());
  43. player.Dispose();
  44. }
  45. }
  46. /// <summary>
  47. /// 绑定玩家id
  48. /// </summary>
  49. /// <param name="self"></param>
  50. /// <param name="playerId"></param>
  51. public static void BindPlayerId(this SessionPlayerComponent self, long playerId)
  52. {
  53. Log.Debug($"session玩家绑定组件 绑定玩家id={playerId}");
  54. self.PlayerId = playerId;
  55. }
  56. /// <summary>
  57. /// 是否存在
  58. /// </summary>
  59. /// <param name="self"></param>
  60. /// <param name="playerId"></param>
  61. /// <returns></returns>
  62. public static bool IsExist(this SessionPlayerComponent self, long playerId)
  63. {
  64. return self.PlayerList.Any(info => info != null && info.Id == playerId);
  65. }
  66. /// <summary>
  67. /// 获取
  68. /// </summary>
  69. /// <param name="self"></param>
  70. /// <param name="playerId"></param>
  71. /// <returns></returns>
  72. public static PlayerInfo Get(this SessionPlayerComponent self, long playerId)
  73. {
  74. return self.PlayerList.FirstOrDefault(info => info != null && info.Id == playerId);
  75. }
  76. /// <summary>
  77. /// 添加
  78. /// </summary>
  79. /// <param name="self"></param>
  80. /// <param name="info"></param>
  81. public static void Add(this SessionPlayerComponent self, PlayerInfo info)
  82. {
  83. if (info != null)
  84. {
  85. self.PlayerList.Add(info);
  86. }
  87. }
  88. /// <summary>
  89. /// 获取当前玩家实体
  90. /// </summary>
  91. /// <param name="self"></param>
  92. /// <returns></returns>
  93. public static WNPlayer GetMyPlayer(this SessionPlayerComponent self)
  94. {
  95. return self.DomainScene().GetComponent<GamePlayerComponent>().Get(self.PlayerId);
  96. }
  97. }
  98. }