Unity类银河恶魔城学习记录10-5 p93 Major stats and defensive stats源代码

时间:2024-03-14 12:08:40

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码

【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili

CharacterStats.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterStats : MonoBehaviour
{
    [Header("Major stats")]
    public Stat strength; // 力量 增伤1点 临界值power 1% 物抗
    public Stat agility;// 敏捷 闪避 1% 临界chance 1%
    public Stat intelgence;// 1 点 魔法伤害 1点魔抗 
    public Stat vitality;//加血的

    [Header("Defensive stats")]
    public Stat maxHealth;
    public Stat armor;
    public Stat evasion;//闪避值


    public Stat damage;


    [SerializeField]private int currentHealth;


    protected virtual void Start()
    {
        currentHealth = maxHealth.GetValue();     
    }

    public virtual void DoDamage(CharacterStats _targetStats)
    {
        if (TargetCanAvoidAttack(_targetStats))
        {
            return;
        }


        int totleDamage = damage.GetValue() + strength.GetValue();

        totleDamage = CheckTargetArmor(_targetStats, totleDamage);

        _targetStats.TakeDamage(totleDamage);
    }

    private static int CheckTargetArmor(CharacterStats _targetStats, int totleDamage)
    {
        totleDamage -= _targetStats.armor.GetValue();
        totleDamage = Mathf.Clamp(totleDamage, 0, int.MaxValue);
        return totleDamage;
    }

    private bool TargetCanAvoidAttack(CharacterStats _targetStats)//设置闪避
    {
        int totleEvation = _targetStats.evasion.GetValue() + _targetStats.agility.GetValue();

        if (Random.Range(0, 100) < totleEvation)
        {
            return true;
        }
        return false;
    }

    protected virtual void TakeDamage(int _damage)
    {
        currentHealth -= _damage;

        Debug.Log(_damage);

        if (currentHealth < 0)
            Die();
        
    }

    protected virtual void Die()
    {

    }
}