PlayerTempDataComponentSystem.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. self.Player = player;
  18. }
  19. }
  20. public class PlayerTempDataComponentDestroySystem: DestroySystem<PlayerTempDataComponent>
  21. {
  22. /// <summary>
  23. /// 玩家临时数据组件销毁
  24. /// </summary>
  25. /// <param name="self"></param>
  26. protected override void Destroy(PlayerTempDataComponent self)
  27. {
  28. Log.Debug($"玩家临时数据保存");
  29. self?.Save();
  30. }
  31. }
  32. public static async ETTask InitFromDB(this PlayerTempDataComponent self)
  33. {
  34. List<PlayerMapInfo> list = await DBManagerComponent.Instance.GetZoneDB(self.DomainZone())
  35. .Query<PlayerMapInfo>(p => p.Id == self.Player.GetId());
  36. if (list is { Count: > 0 })
  37. {
  38. self.MapData = list[0];
  39. }
  40. else
  41. {
  42. self.MapData = new PlayerMapInfo();
  43. self.MapData.mapId = 10098;
  44. await self.Save();
  45. }
  46. }
  47. public static async ETTask Save(this PlayerTempDataComponent self)
  48. {
  49. if (self.MapData != null)
  50. {
  51. await DBManagerComponent.Instance.GetZoneDB(self.DomainZone()).Save(self.MapData);
  52. }
  53. }
  54. /** 同步场景出生数据 **/
  55. public static void SyncBornData(this PlayerTempDataComponent self, float bornX, float bornY, int bornAreaId)
  56. {
  57. self.MapData.bornX = bornX;
  58. self.MapData.bornY = bornY;
  59. self.MapData.bornMapId = bornAreaId;
  60. }
  61. /** 同步场景临时数据 **/
  62. public static void SyncNowData(this PlayerTempDataComponent self, int areaId, long instanceId, GetPlayerData data)
  63. {
  64. self.MapData.x = data.x;
  65. self.MapData.y = data.y;
  66. self.MapData.direction = data.direction;
  67. self.MapData.hp = data.hp;
  68. self.MapData.mp = data.mp;
  69. self.MapData.mapId = areaId;
  70. self.MapData.mapInstanceId = instanceId;
  71. }
  72. /** 同步场景历史数据 **/
  73. public static void SyncHistoryData(this PlayerTempDataComponent self, MapConfig prop, long instanceId, GetPlayerData data)
  74. {
  75. self.MapData.historyX = data.x;
  76. self.MapData.historyY = data.y;
  77. self.MapData.historyDirection = data.direction;
  78. self.MapData.historyMapId = prop.Id;
  79. self.MapData.historyMapInstanceId = instanceId;
  80. }
  81. }
  82. }