コード例 #1
0
    public void TakeDamage(float damage)
    {
        damage -= armor.GetValue();
        damage  = Mathf.Clamp(damage, 0, int.MaxValue);

        if (damage == 0)
        {
            damage = 1;
        }

        currentHealth -= damage;
        if (onGetDamage != null)
        {
            onGetDamage.Invoke(currentHealth);
        }

        //Debug.Log(transform.name + " takes " + damage + " damage.");

        if (OnHealthChanged != null)
        {
            OnHealthChanged(maxHealth.GetValue(), currentHealth);
        }

        if (currentHealth <= 0)
        {
            if (OnHealthReachedZero != null)
            {
                OnHealthReachedZero();
            }

            Die();
        }
    }
コード例 #2
0
ファイル: Tank.cs プロジェクト: Ali-Dorri/Tank-Game
        public void GetDamage(int damage)
        {
            if (m_Health > 0)
            {
                m_Health -= damage;
                if (m_Health <= 0)
                {
                    m_Health = 0;
                }

                float healthRatio = m_Health / (float)m_MaxHealth;
                OnGetDamage?.Invoke(healthRatio);

                if (m_Health == 0)
                {
                    m_Explosion.transform.SetParent(null, true);
                    m_Explosion.gameObject.SetActive(true);
                    m_Explosion.Play(true);
                    gameObject.SetActive(false);
                    AudioSource.PlayClipAtPoint(m_ExplosionSound.m_Audio, transform.position, m_ExplosionSound.m_Volume);
                }
                else
                {
                    m_DamageSoundPlayer.PlayOneShot(m_DamageSound.m_Audio, m_DamageSound.m_Volume);
                }
            }
        }
コード例 #3
0
    public void GetDamage(int damage, Character from)
    {
        DamageEventArgs damageargs = new DamageEventArgs(damage, from);

        OnGetDamage?.Invoke(this, damageargs);

        if (character.GetShield() != 0)
        {
            int             canShield  = Mathf.Min(damageargs.damage, character.GetShield());
            DamageEventArgs shieldArgs = new DamageEventArgs(canShield, from);
            OnBlockDamage?.Invoke(this, shieldArgs);
            character.ChangeShield(-shieldArgs.damage);
            damageargs.damage -= shieldArgs.damage;

            if (damageargs.damage <= 0)
            {
                GetComponent <AudioSource>().clip = shieldSound;
                GetComponent <AudioSource>().Play();
            }
        }

        if (damageargs.damage > 0)
        {
            DamageEventArgs args = new DamageEventArgs(damageargs.damage, from);
            OnGetDamaged?.Invoke(this, args);
            if (args.damage > 0)
            {
                character.ChangeHealthPoint(-args.damage);
            }

            GetComponent <AudioSource>().clip = damageSound;
            GetComponent <AudioSource>().Play();
        }
    }
コード例 #4
0
ファイル: Stats.cs プロジェクト: AndrewDelarge/unity_rpg_demo
        public virtual void TakeDamage(Damage damage)
        {
            if (currentHealth <= 0)
            {
                return;
            }

            int damageValue = Mathf.FloorToInt(damage.GetValue() * GetArmorMultiplier());

            damageValue = Mathf.Clamp(damageValue, 0, int.MaxValue);

            currentHealth -= damageValue;

            var finalDamage = new Damage(damageValue, damage.GetOwner(), damage.IsCrit());

            lastDamage = finalDamage;

            if (currentHealth <= 0)
            {
                Die();
            }

            onGetDamage?.Invoke(finalDamage);

            HealthChangeEventArgs args = new HealthChangeEventArgs
            {
                healthChange = -damageValue, actor = actor, modifier = finalDamage
            };

            OnHealthChange?.Invoke(this, args);
        }
コード例 #5
0
 private void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag("attack"))
     {
         OnGetDamage.Invoke();
     }
 }
コード例 #6
0
        public virtual void TakeDamage(Damage damage)
        {
            if (currentHealth <= 0)
            {
                return;
            }
            int damageValue = Mathf.FloorToInt(damage.GetValue() * GetArmorMultiplier());

            damageValue    = Mathf.Clamp(damageValue, 0, int.MaxValue);
            currentHealth -= damageValue;
            lastDamage     = damage;
            if (currentHealth <= 0)
            {
                Die();
            }

            onGetDamage?.Invoke(damage);


            HealthChangeEventArgs args = new HealthChangeEventArgs();

            args.healthChange = -damageValue;
            args.initiator    = damage.GetOwner();
            args.modifier     = damage;

            OnHealthChange?.Invoke(this, args);
        }
コード例 #7
0
        public void Damage(float damage)
        {
            float d = CalculateDamage(damage);

            _currentHealth -= d;

            OnGetDamage?.Invoke(this, d);
            if (_currentHealth <= 0)
            {
                OnDeath?.Invoke(this);
            }
        }
コード例 #8
0
        public void GetDamage(int amount)
        {
            this.HP -= amount;
            if (this.HP <= 0)
            {
                this.HP = 0;
            }

            // Tigger when unit died
            if (!IsDead)
            {
                OnGetDamage?.Invoke(this, amount);
            }
            else if (IsDead)
            {
                OnGetDamage?.Invoke(this, amount);
                OnUnitDied?.Invoke(this, EventArgs.Empty);
            }
        }