示例#1
0
文件: Unit.cs 项目: femi96/Corvus
    public void DealDamage(float damage, DamageType type = DamageType.True,
                           Affinity affinity             = Affinity.Nu,
                           bool crit = false, bool miss = false)
    {
        // Affinity modifier
        damage *= AffinityCalc.GetModifier(affinity, monster.GetAffinity());

        // Damage effect on energy
        currentEnergy += Mathf.RoundToInt(damage / 200f * monster.EnergyMod());

        // Damage reduction from defense
        float reduction = 1f;

        if (type == DamageType.Physical)
        {
            reduction = 1f - 0.05f * monster.Endurance() / (1 + 0.05f * Mathf.Abs(monster.Endurance()));
        }

        if (type == DamageType.Magical)
        {
            reduction = 1f - 0.05f * monster.Resistance() / (1 + 0.05f * Mathf.Abs(monster.Resistance()));
        }

        damage *= reduction;

        // Round damage
        int roundedDamage = Mathf.RoundToInt(damage / 4);

        // Damage effect on health
        // int preHealth = currentHealth;
        currentHealth = Mathf.Max(currentHealth - roundedDamage, 0);

        // Visual Effects
        GameObject go  = Instantiate(UIPrefabs.instance.textPrefab, UIPrefabs.instance.canvasTransform);
        Text       txt = go.transform.Find("Text").gameObject.GetComponent <Text>();

        uiDamageText.Add(go);

        TextEffectMover em   = go.GetComponent <TextEffectMover>();
        Vector3         rand = new Vector3(Random.Range(0.1f, 1f), 0f, 0f);

        rand         = rand * (Random.Range(0, 2) * 2 - 1);
        em.velocity += rand;

        if (type == DamageType.Physical)
        {
            txt.color = UIColor.DamagePhysical();
        }

        if (type == DamageType.Magical)
        {
            txt.color = UIColor.DamageMagical();
        }

        if (type == DamageType.True)
        {
            txt.color = UIColor.DamageTrue();
        }

        if (miss)
        {
            txt.text  = "Miss";
            txt.color = UIColor.DamageMiss();
            // Debug.Log("Miss!");
        }
        else if (crit)
        {
            txt.text = roundedDamage + "!";
            // Debug.Log("Crit! Unit takes " + roundedDamage + " " + type + " damage. " + preHealth + " -> " + currentHealth);
        }
        else
        {
            txt.text = roundedDamage + "";
            // Debug.Log("Unit takes " + roundedDamage + " " + type + " damage. " + preHealth + " -> " + currentHealth);
        }

        // Death check
        TryDead();
    }