using XGame.Framework.Components;

namespace FL.Battle.Components
{
    public interface IHpContext
    {
        IEntity Entity { get; }
        EEntityState State { get; set; }
    }
    public class HpComponent : Component<IHpContext>
    {

        protected override void OnDispose()
        {
        }
        /// <summary>
        /// 计算血量
        /// 正数表示加血
        /// 负数表示扣血
        /// </summary>
        /// <param name="damage"></param>
        /// <returns></returns>
        public int CalculateHP(int damage)
        {
            if (Context.State == EEntityState.Dead)
            { // 已经死亡
                return 0;
            }
            var lastHp = Context.Entity.Attr.Hp;
            var nextHp = lastHp + damage;
            if (nextHp > Context.Entity.Attr.HpLimit)
            {
                nextHp = Context.Entity.Attr.HpLimit;
            }
            else if (nextHp < 0)
            {
                nextHp = 0;
            }
            Context.Entity.Attr.Hp = nextHp;
            if (nextHp == 0)
            {
                Context.State = EEntityState.Dead;
            }
            return (int)(nextHp - lastHp);

        }
    }
}
