PlayerTempDataComponentSystem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections.Generic;
  2. using BattleIce;
  3. namespace ET.Server
  4. {
  5. [FriendOf(typeof (PlayerTempDataComponent))]
  6. public static class PlayerTempDataComponentSystem
  7. {
  8. public class PlayerTempDataComponentAwakeSystem: AwakeSystem<PlayerTempDataComponent, WNPlayer>
  9. {
  10. /// <summary>
  11. /// 玩家临时数据组件创建
  12. /// </summary>
  13. /// <param name="self"></param>
  14. /// <param name="player"></param>
  15. protected override void Awake(PlayerTempDataComponent self, WNPlayer player)
  16. {
  17. Log.Info($"创建玩家临时数据组件...");
  18. self.Player = player;
  19. }
  20. }
  21. public class PlayerTempDataComponentDestroySystem: DestroySystem<PlayerTempDataComponent>
  22. {
  23. /// <summary>
  24. /// 玩家临时数据组件销毁
  25. /// </summary>
  26. /// <param name="self"></param>
  27. protected override void Destroy(PlayerTempDataComponent self)
  28. {
  29. Log.Debug($"玩家临时数据保存");
  30. self?.Save();
  31. }
  32. }
  33. /// <summary>
  34. /// 初始化
  35. /// </summary>
  36. /// <param name="self"></param>
  37. public static async ETTask Init(this PlayerTempDataComponent self)
  38. {
  39. List<PlayerMapInfo> list = await DBManagerComponent.Instance.GetZoneDB(self.DomainZone())
  40. .Query<PlayerMapInfo>(p => p.Id == self.Player.GetId());
  41. if (list is { Count: > 0 })
  42. {
  43. self.MapData = list[0];
  44. }
  45. if (self.MapData != null)
  46. {
  47. return;
  48. }
  49. self.MapData = new PlayerMapInfo();
  50. self.MapData.mapId = 10098;
  51. self?.Save();
  52. }
  53. private static async ETTask Save(this PlayerTempDataComponent self)
  54. {
  55. if (self.MapData == null)
  56. {
  57. Log.Debug($"保存玩家临时数据组件数据, Data is null");
  58. return;
  59. }
  60. self.MapData.Id = self.Player.GetId();
  61. await DBManagerComponent.Instance.GetZoneDB(self.DomainZone()).Save(self.Player.GetId(), self.MapData);
  62. }
  63. /** 同步场景出生数据 **/
  64. public static void SyncBornData(this PlayerTempDataComponent self, float bornX, float bornY, int bornAreaId)
  65. {
  66. self.MapData.bornX = bornX;
  67. self.MapData.bornY = bornY;
  68. self.MapData.bornMapId = bornAreaId;
  69. }
  70. /** 同步场景临时数据 **/
  71. public static void SyncNowData(this PlayerTempDataComponent self, int areaId, long instanceId, GetPlayerData data)
  72. {
  73. self.MapData.x = data.x;
  74. self.MapData.y = data.y;
  75. self.MapData.direction = data.direction;
  76. self.MapData.hp = data.hp;
  77. self.MapData.mp = data.mp;
  78. self.MapData.mapId = areaId;
  79. self.MapData.mapInstanceId = instanceId;
  80. }
  81. /** 同步场景历史数据 **/
  82. public static void SyncHistoryData(this PlayerTempDataComponent self, MapConfig prop, long instanceId, GetPlayerData data)
  83. {
  84. self.MapData.historyX = data.x;
  85. self.MapData.historyY = data.y;
  86. self.MapData.historyDirection = data.direction;
  87. self.MapData.historyMapId = prop.Id;
  88. self.MapData.historyMapInstanceId = instanceId;
  89. }
  90. }
  91. }