Example #1
0
    public float TakeDamage(float dmg)
    {
        Sounds sounds = GetComponent <Sounds>();

        if (sounds != null)
        {
            sounds.play(gameObject, sounds.gotHit);
        }

        if (eq != null)
        {
            float totalArmor = armorValue;
            Item  armor      = eq.GetArmor();
            if (armor != null)
            {
                totalArmor += armor.armorValue;
            }
            foreach (GameObject tmp in this.activeBuffs)
            {
                Item tempBuff = tmp.GetComponent <Item>();
                totalArmor += tempBuff.armorValue;
            }

            dmg = Mathf.Max(0, dmg - totalArmor * dmg);

            Item shield = eq.GetShield();
            if (shield != null)
            {
                if (shieldActive)
                {
                    float shieldedDmg = Mathf.Min(shield.shieldHp, shield.shieldProtection * dmg);
                    dmg             = Mathf.Max(0, dmg - shieldedDmg);
                    shield.shieldHp = Mathf.Max(0, shield.shieldHp - shieldedDmg);
                    if (shield.shieldHp == 0)
                    {
                        shieldActive = false;
                        eq.RemoveItem(shield);
                    }
                }
            }
        }

        if (dmg > 0)
        {
            GameObject blood = Instantiate(Resources.Load("SmallBloodSplash")) as GameObject;
            blood.transform.localPosition = transform.localPosition;
            blood.transform.localRotation = transform.localRotation;
            Destroy(blood, 1);
        }

        float overkill = dmg - hp;

        hp -= dmg;
        if (hp <= 0)
        {
            Sounds _sounds = GetComponent <Sounds>();
            if (_sounds != null)
            {
                _sounds.dieSound(gameObject);
            }
            gameObject.SendMessage("LetMeDie");
        }
        return(overkill);
    }