学习游戏制作记录(暴击,魔法伤害,元素异常状态以及疾病效果与持续时间)8.8

1.实现暴击效果

State脚本:

    public void SetDefaultValue(int _value)//设置默认值
    {
        baseValue = _value;
    }

CharactorState脚本:

    [Header("Offensive stats")]
    public State damage;
    public State critChance;//暴击率
    public State critPower;//暴击伤害

    protected virtual void Start()
    {
        critPower.SetDefaultValue(150);//默认暴击伤害基础值150
        currentHealth = maxHealth.GetValue();

    }

    public virtual bool CanCrit()//是否可以暴击
    {
        int totalCritChance=critChance.GetValue()+agility.GetValue();//基础暴击加上闪避属性给的暴击

        if(Random.Range(0,100)<=totalCritChance)
        {
            return true;
        }

        return false;
    }

    public virtual int CaculateCritDamage(int _damage)
    {
        float totalCritPower=(critPower.GetValue()+strength.GetValue())*.01f;//暴击伤害由基础伤害和力量属性提供

        float totalDamage = _damage*totalCritPower;

        return Mathf.RoundToInt(totalDamage);//取整
    }

        if(CanCrit())//Dodamage()函数中
        {
            totalDamage = CaculateCritDamage(totalDamage);

            Debug.Log(totalDamage);
        }

2.实现魔法伤害效果

CharactorState脚本:

    public State magicResistence;//魔法抗性

    [Header("Magic stats")]
    public State fireDamage;//各种元素伤害
    public State iceDamage;
    public State lightingDamage;


    public bool isInigted;//是否造成元素伤害
    public bool isChilled;
    public bool isShocked;

    public virtual void DoMagicDamage(CharactorState _target)
    {
        int _fireDamage=fireDamage.GetValue();
        int _iceDamage=iceDamage.GetValue();
        int _lightingDamage=lightingDamage.GetValue();

        int totalMagicDamage=_fireDamage+_iceDamage+_lightingDamage+intelligence.GetValue();//最终元素伤害等于各种元素伤害加上智力属性

        totalMagicDamage-=_target.magicResistence.GetValue()+(_target.intelligence.GetValue()*3);//敌人的魔法防御等于基础魔法抗性加上智力*3
        totalMagicDamage = Mathf.Clamp(totalMagicDamage, 0, int.MaxValue);//防止为负

        _target.TakeDamage(totalMagicDamage);
    }

    public void  ApplyAilments(bool _isInigted,bool _isChilled,bool _isShocked)//给敌人应用元素状态
    {
        if (isInigted || isChilled || isShocked)
            return;

        isInigted = _isInigted;
        isChilled = _isChilled;
        isShocked = _isShocked;
    }

 DoMagicDamage(_target);//Dodamage()中暂时调用

3.实现元素异常状态

哪种元素伤害最高取那种对应的异常状态

CharactorState脚本:

DoMagicDamage(CharactorState _target)函数

 if (Mathf.Max(_fireDamage, _iceDamage, _lightingDamage) <= 0)//如果无异常则直接返回
     return;

 bool canApplyIgnite = _fireDamage > _iceDamage && _fireDamage > _lightingDamage;//最高异常

 bool canApplyChill =_iceDamage >_fireDamage &&_iceDamage > _lightingDamage;

 bool canApplyShock = _lightingDamage > _fireDamage && _lightingDamage > _iceDamage;

 while(!canApplyIgnite && !canApplyChill && !canApplyShock)//处理元素伤害相等时的状态
 {
     if(Random.value<.5f&&_fireDamage > 0)
     {
         canApplyIgnite = true;
         _target.ApplyAilments(canApplyIgnite,canApplyChill,canApplyShock);//给目标应用对应的异常状态
         Debug.Log("fire");
         return;
     }

     if (Random.value < .5f && _iceDamage > 0)
     {
         canApplyChill = true;
         _target.ApplyAilments(canApplyIgnite, canApplyChill, canApplyShock);
         Debug.Log("chill");
         return;
     }

     if (Random.value < .5f && _lightingDamage > 0)
     {
         canApplyShock = true;
         _target.ApplyAilments(canApplyIgnite, canApplyChill, canApplyShock);
         Debug.Log("shock");
         return;
     }

}

_target.ApplyAilments(canApplyIgnite, canApplyChill, canApplyShock);

4.实现疾病效果以及其持续时间

CharactorState脚本:

燃烧会造成持续伤害

冰冻会削减敌方护甲

闪电会降低命中即提高敌方闪避

    private float ingitedTimer;//各种状态的持续时间
    private float chilledTimer;
    private float shockedTimer;

    private float ingitedDamageCooldown = .3f;//燃烧的冷却时间
    private float ingitedDamageTimer;
    public float ingitedDamage;//燃烧伤害

    protected virtual void Update()
    {
        ingitedTimer -= Time.deltaTime;//计时器的处理
        chilledTimer -= Time.deltaTime;
        shockedTimer -= Time.deltaTime;
        ingitedDamageTimer -= Time.deltaTime;

        if(ingitedTimer < 0)
        {
            isInigted = false;
        }

        if(chilledTimer < 0)
        {
            isChilled = false;
        }

        if(shockedTimer < 0)
        {
            isShocked = false;
        }

        if(ingitedDamageTimer<0&&isInigted)//持续燃烧
        {
            Debug.Log("take the ingitedamage" + ingitedDamage);

            currentHealth -= ingitedDamage;

            if(currentHealth < 0)
            {
                Die();
            }

            ingitedDamageTimer = ingitedDamageCooldown;
        }
    }

        if(canApplyIgnite)//DoMagicDamage(CharactorState _target)函数
        {
            _target.SetupIngitedDamage(Mathf.RoundToInt(_fireDamage * .2f));//设置燃烧伤害为火焰伤害的20%
        }

    public void  ApplyAilments(bool _isInigted,bool _isChilled,bool _isShocked)
    {
        if (isInigted || isChilled || isShocked)
            return;

        if(_isInigted)
        {
        isInigted = _isInigted;
            ingitedTimer = 2f;//设置持续时间
        }
        if(_isChilled)
        {
        isChilled = _isChilled;
            chilledTimer = 2f;
        }
        if (_isShocked)
        {
        isShocked = _isShocked;
            shockedTimer = 2f;
        }
    }

        if (_target.isChilled)//TargetCheckArmor(CharactorState _target,int totalDamage)函数
        {
            totalDamage -= Mathf.RoundToInt(_target.armor.GetValue()*.8f);//冰冻削减护甲
        }
        else
        {
            totalDamage -= _target.armor.GetValue();
        }

        if (isShocked)//TargetCanAvoidAttack(CharactorState _target)函数
            totalEvasion += 20;//麻痹增加闪避

private void SetupIngitedDamage(int _damage) => ingitedDamage = _damage;//设置燃烧伤害的函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值