private void OnDestroy()
 {
     if (explosionSound != null)
     {
         AudioSource.PlayClipAtPoint(explosionSound.GetRandom(), transform.position);
     }
 }
Example #2
0
        public virtual int Damage(int amount, IDamageSource damageSource)
        {
            if (animation.GetCurrentFrameFlags().HasFlag(FrameFlags.INVULNERABLE))
            {
                return(0);
            }


            if (amount > 0)
            {
                if (onDamageTaken != null)
                {
                    amount = onDamageTaken(amount, damageSource);
                }
            }
            else if (amount < 0)
            {
                if (onHealed != null)
                {
                    amount = onHealed(amount, damageSource);
                }
            }

            int originalAmount = amount;

            if (amount > hpMax)
            {
                amount = hpMax;
            }
            else if (amount > hp)
            {
                amount = hp;
            }

            if (amount > 0)
            {
                audioSource.PlayOneShot(soundHurt.GetRandom());
            }

            hp -= amount;

            UI.DamageNumber.Display(transform, originalAmount);



            if (IsDead)
            {
                timeOfDeath = Time.time;
                damageSource.OnKill(this);
                animation.SetAnimation("Death", true);
                if (onKilledBy != null)
                {
                    onKilledBy(damageSource);
                }
            }
            else
            {
                animation.SetAnimation("Hurt");
            }

            UpdateLiquid();

            return(amount);
        }