EntityAttributes.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using XGame.Database;
  3. using XGame.Framework;
  4. namespace FL.Battle
  5. {
  6. public class EntityAttributes : IDisposable
  7. {
  8. public int TableId { get; private set; }
  9. public long UID { get; private set; }
  10. public string Name { get; set; }
  11. /// <summary>
  12. /// 基础生命
  13. /// </summary>
  14. public long BasicHp => GetValue(EAttributeType.Hp);
  15. public long HpLimit => GetValue(EAttributeType.HpTotal);
  16. /// <summary>
  17. /// 当前生命值
  18. /// </summary>
  19. public long CurrentHp { get; set; }
  20. public long Mp { get; set; }
  21. public long MpLimit { get; set; }
  22. public float Radius { get; set; }
  23. /// <summary>
  24. /// 移动速度,不带缩放
  25. /// </summary>
  26. public float MoveSpeed => GetValue(EAttributeType.Speed);
  27. /// <summary>
  28. /// 移动速度缩放值
  29. /// EAttributeType.SpeedAdd 最低值必须大于 -10000
  30. /// </summary>
  31. public float MoveSpeedScale
  32. {
  33. get
  34. {
  35. var scale = 1 + (float)GetValue(EAttributeType.SpeedAdd).ToRealDouble();
  36. return scale < 0 ? 0 : scale;
  37. }
  38. }
  39. /// <summary>
  40. /// 移动时间缩放值
  41. /// EAttributeType.SpeedAdd 最低值必须大于 -10000
  42. /// </summary>
  43. public float MoveTimeScale => 1 / MoveSpeedScale;
  44. /// <summary>
  45. /// 真实的移动速度
  46. /// 慎用
  47. /// </summary>
  48. public float MoveSpeedReal => MoveSpeed * MoveSpeedScale;
  49. public int Attack { get => (int)GetValue(EAttributeType.Atk); }
  50. public int AttackCD { get; set; }
  51. private Attributes _staticAttr;
  52. /// <summary>
  53. /// 静态属性,战斗外基础数值
  54. /// 战斗内不会变
  55. /// </summary>
  56. public Attributes Static => _staticAttr ??= ObjectPool.Acquire<Attributes>();
  57. private Attributes _dynamicAttr;
  58. /// <summary>
  59. /// 动态属性,战斗内属性加成
  60. /// </summary>
  61. public Attributes Dynamic => _dynamicAttr ??= ObjectPool.Acquire<Attributes>();
  62. public EntityAttributes(int tableId, long uid)
  63. {
  64. TableId = tableId;
  65. UID = uid;
  66. }
  67. public long GetValue(EAttributeType type)
  68. {
  69. return Static.GetValue(type) + (_dynamicAttr?.GetValue(type) ?? 0);
  70. }
  71. void IDisposable.Dispose()
  72. {
  73. if (_staticAttr != null)
  74. {
  75. ObjectPool.Recycle(_staticAttr);
  76. _staticAttr = null;
  77. }
  78. if (_dynamicAttr != null)
  79. {
  80. ObjectPool.Recycle(_dynamicAttr);
  81. _dynamicAttr = null;
  82. }
  83. }
  84. public void CopyFrom(Attributes attribute)
  85. {
  86. attribute.CopyTo(Static);
  87. CurrentHp = HpLimit;
  88. }
  89. public void ResetHp()
  90. {
  91. CurrentHp = HpLimit;
  92. }
  93. }
  94. }