Exemple #1
0
    /// <summary>
    /// 真正执行伤害/加血逻辑
    /// </summary>
    /// <param name="damageHP">伤害值,可以正负,如果小于1,则按百分比执行</param>
    /// <param name="effectFollowMaxHP">是否以最大生命值为准</param>
    public void OnDamage(float damageHP, bool effectFollowMaxHP = false)
    {
        int changeHP = 0;

        // 按百分比改变当前血量
        if ((damageHP > 0 && damageHP < 1) || (damageHP < 0 && damageHP > -1))
        {
            if (effectFollowMaxHP)
            {
                changeHP = (int)(fightEntityData.MaxHP * damageHP);
            }
            else
            {
                changeHP = (int)(fightEntityData.HP * damageHP);
            }
        }
        else
        {
            changeHP = (int)damageHP;
        }

        bool forceAdd = false;

        // 伤害
        if (changeHP > 0)
        {
            changeHP -= fightEntityData.Def;

            if (changeHP < 0)
            {
                changeHP = 1;
            }

            if (changeHP > 0)
            {
                fightEntityData.HP -= changeHP;

                OnHurt();
            }
        }
        // 加血
        else if (changeHP < 0)
        {
            forceAdd = true;

            if (fightEntityData.HP - changeHP > fightEntityData.MaxHP)
            {
                changeHP = fightEntityData.HP - fightEntityData.MaxHP;
            }

            fightEntityData.HP += -changeHP;

            OnCure();
        }

        FlowTextData flowTextData = new FlowTextData(EntityExtension.GenerateSerialId(), this.Id, -changeHP, forceAdd);

        flowTextData.Position = this.CachedTransform.position + new Vector3(0.4f, 1, 0);
        flowTextData.Rotation = Camera.main.transform.rotation;

        EntityExtension.ShowFlowText(
            typeof(FlowText),
            "FlowTextGroup", flowTextData
            );

        // 更新血量条
        RefreshHPBar();

        if (fightEntityData.HP <= 0)
        {
            OnDead();
        }
    }