HpComponent.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using XGame.Framework.Components;
  2. namespace FL.Battle
  3. {
  4. public interface IHpContext
  5. {
  6. IEntity Entity { get; }
  7. EEntityState State { get; set; }
  8. }
  9. internal class HpComponent : Component<IHpContext>
  10. {
  11. public override int Order => 0;
  12. protected override void OnDispose()
  13. {
  14. }
  15. public int CalculateHit(int hitValue)
  16. {
  17. if (Context.State == EEntityState.Dead)
  18. { // 已经死亡
  19. return 0;
  20. }
  21. var lastHp = Context.Entity.Attr.Hp;
  22. var nextHp = lastHp + hitValue;
  23. if (nextHp > Context.Entity.Attr.HpLimit)
  24. {
  25. nextHp = Context.Entity.Attr.HpLimit;
  26. }
  27. else if (nextHp < 0)
  28. {
  29. nextHp = 0;
  30. }
  31. Context.Entity.Attr.Hp = nextHp;
  32. if (nextHp == 0)
  33. {
  34. Context.State = EEntityState.Dead;
  35. }
  36. return (int)(nextHp - lastHp);
  37. }
  38. }
  39. }