Ejemplo n.º 1
0
    public void DealDamage(int damage, BaseUnit attacker)
    {
        // Check attacker multiplicative debuff malus
        if (attacker)
        {
            WeaknessStatus weaknessStatus = attacker.SearchStatusLike(typeof(WeaknessStatus)) as WeaknessStatus;
            if (weaknessStatus != null)
            {
                damage = (int)((1.0f - weaknessStatus.Multiplier) * damage);
            }
        }

        // Check if switching target if linked
        HealthLinkStatus healthLink = SearchStatusLike(typeof(HealthLinkStatus)) as HealthLinkStatus;

        if (healthLink != null)
        {
            int newdamage = (int)healthLink.Multiplier * damage;
            healthLink.Target.DealDamage(newdamage, healthLink.Owner);

            // If health link is less than 100% continue with the remaining damage
            if (newdamage < damage)
            {
                damage -= newdamage;
            }
            else
            {
                return;
            }
        }

        // Apply global damage modifiers
        damage = GlobalsManager.Instance.ApplyDamageModifiers(damage, attacker, this);

        // Calculate multiplicative debuffs bonus
        VulnerableStatus vulnerableStatus = SearchStatusLike(typeof(VulnerableStatus)) as VulnerableStatus;

        if (vulnerableStatus != null)
        {
            damage = (int)((1.0f + vulnerableStatus.Multiplier) * damage);
        }

        // Calculate the unit's block stacks.
        int totalBlock = 0;

        foreach (BaseStatus status in _statuses)
        {
            if (status.GetType() == typeof(BlockStatus))
            {
                totalBlock = status.Strength;
                goto damageCalc;
            }
        }

        // Damage calculation including block mitigation.
damageCalc:


        // Add link damage if attacker has it
        if (attacker)
        {
            BaseStatus linkDamage = attacker.SearchStatusLike(typeof(LinkDamageStatus));
            if (linkDamage != null)
            {
                damage += linkDamage.Strength;
            }

            // Execute Link event
            attacker.ExecuteLinkEvent();
        }

        int totalDamage = damage - totalBlock;

        if (totalDamage < 0)
        {
            totalDamage = 0;
            SpawnBattleText("Blocked (-" + damage + ")");
        }
        else
        {
            _actualHP -= totalDamage;

            if (totalBlock > 0)
            {
                // Partial Block
                SpawnBattleText(totalDamage.ToString() + " (-" + (damage - totalDamage) + ")");

                // Shake Camera
                Camera.main.GetComponent <CameraRumble>().Rumble();

                // Play Sound Effect
                _audioSource.clip = Resources.Load <AudioClip>("Sounds/fx/attackHitArmor01");
                _audioSource.Play();
            }
            else
            {
                // Clean Hit
                SpawnBattleText(totalDamage.ToString());

                // Shake Camera
                Camera.main.GetComponent <CameraRumble>().Rumble();

                // Play Sound Effect
                _audioSource.clip = Resources.Load <AudioClip>("Sounds/fx/attackHitFlesh");
                _audioSource.Play();
            }
        }

        // Recalculate block stacks here.
        DamageBlock(damage);

        Debug.Log(UnitName + " takes " + totalDamage + " Damage.");

        if (_actualHP > MaxHP)
        {
            _actualHP = MaxHP;
        }
        if (_actualHP < 1)
        {
            OnDeath();
        }

        UpdateUI();
    }