コード例 #1
0
ファイル: Health.cs プロジェクト: vcmiller/SlightlyBetterRats
        /// <summary>
        /// Apply damage to the Health.
        /// </summary>
        /// <param name="dmg">The damage to apply.</param>
        /// <returns>The actual damage amount dealt.</returns>
        public virtual float Damage(Damage dmg)
        {
            if (enabled && dmg.amount > 0 && hitInvulnTimer.Use())
            {
                DamageModifier?.Invoke(ref dmg);

                float prevHealth = health;
                health    -= dmg.amount;
                health     = Mathf.Max(health, 0);
                dmg.amount = prevHealth - health;

                healthRegenTimer.Set();
                SendMessage("OnDamage", dmg, SendMessageOptions.DontRequireReceiver);
                Damaged?.Invoke(dmg);
                if (damageSound)
                {
                    damageSound.Play();
                }

                if (health == 0 && !dead)
                {
                    dead = true;
                    SendMessage("OnZeroHealth", SendMessageOptions.DontRequireReceiver);
                    ZeroHealth?.Invoke();
                }
            }
            else
            {
                dmg.amount = 0;
            }

            return(dmg.amount);
        }
コード例 #2
0
        /// <summary>
        /// Called when damage is done to an entity.
        /// </summary>
        /// <param name="damage"></param>
        /// <param name="force"></param>
        /// <param name="from"></param>
        /// <returns></returns>
        public virtual bool DealDamage(int damage, float force, Vector2 from)
        {
            //status effects
            IterateStatusEffects(s => s.OnDamage(this, damage));

            //apply damage modifiers
            damageModifiers?.Invoke(ref damage);

            hp -= damage;

            //prevent overheal
            hp = Mathf.Clamp(hp, 0, MaxHP);

            //updates the healthbar
            healthBar.SetHealth(hp, MaxHP);

            //hit marker
            CameraReference.Instance.InstantiateHitMarker(damage, transform.position);

            if (hp <= 0)
            {
                Die();
            }
            else
            {
                StartCoroutine("DamageFlash");
                //If not dead play hurt
                if (hurtSound != null)
                {
                    audioSrc.PlayOneShot(hurtSound);
                }
            }

            //apply impulse
            ApplyImpulse(force, from);

            return(true);
        }