Beispiel #1
0
        public void OnDamage(float amount)
        {
            if (_isDying)
            {
                return;
            }

            health -= amount;
            OnHealthChanged?.Invoke();
            if (health <= 0)
            {
                _isDying = true;
                animator.SetTrigger(Death);
                if (gameObject.CompareTag("Enemy"))
                {
                    StartCoroutine(DestroySelfAfterTime());
                    DropLoot();
                }
                else
                {
                    Destroy(gameObject);
                }
                return;
            }

            animator.SetTrigger(Hit);
        }
Beispiel #2
0
        public void TakeDamage(Damage damage)
        {
            OnPreDamageTaken?.Invoke(this, damage);

            if (damage.DamageLeft > 0)
            {
                CurrentHealth -= damage.DamageLeft;
                if (CurrentHealth <= 0)
                {
                    damage.DamageDealt += damage.DamageLeft + CurrentHealth;
                    damage.DamageLeft   = -CurrentHealth;
                    CurrentHealth       = 0;

                    OnPostDamageTaken?.Invoke(this, damage);
                    OnHealthChanged?.Invoke(this);
                    Die();
                }
                else
                {
                    damage.DamageDealt += damage.DamageLeft;
                    damage.DamageLeft   = 0;

                    OnPostDamageTaken?.Invoke(this, damage);
                    OnHealthChanged?.Invoke(this);
                }
            }
        }
Beispiel #3
0
    public void TakeDamage(int damage)
    {
        damage = Mathf.Clamp(damage, 0, int.MaxValue); // Damage should never be negative.

        // Calculate damage modifiers
        bool isCriticalHit = UnityEngine.Random.Range(0, 100) < 30;
        int  damageTotal   = UnityEngine.Random.Range(1, damage);

        if (isCriticalHit)
        {
            damageTotal *= 2;
        }

        healthSystem.Damage(damageTotal);
        OnHealthChanged?.Invoke();
        Debug.Log($"{charName} take {damageTotal} points of damage.");

        CombatText.Create(GetPosition() + new Vector3(0, .5f, .5f), damageTotal, isCriticalHit,
                          new Color32(255, 128, 0, 255));

        if (healthSystem.GetHealth() <= 0)
        {
            Die();
        }
    }
Beispiel #4
0
        public float Heal(HealRequest healRequest)
        {
            if (IsDead)
            {
                return(0);
            }

            var healAmount = healRequest.CalculateAmount(System.AttributeSystem);

            currentHealth += healAmount;

            if (currentHealth <= MaxHealth)
            {
                healRequest.AmountDealt = healAmount;
                OnHealingReceived?.Invoke(healRequest);
                OnHealthChanged?.Invoke(CurrentHealth);
                return(healAmount);
            }

            var actualHealAmount = healAmount - (currentHealth - MaxHealth);

            currentHealth           = MaxHealth;
            healRequest.AmountDealt = actualHealAmount;
            OnHealingReceived?.Invoke(healRequest);
            OnHealthChanged?.Invoke(CurrentHealth);
            return(actualHealAmount);
        }
Beispiel #5
0
    private void SetStats(StatsEffect effect)
    {
        PlayerStatsDict.Instance.NormalizeStats(ref effect);

        if (effect.health != health)
        {
            health = effect.health;
            player.Health.SetMax(PlayerStatsDict.Instance.GetHealth(health));
            OnHealthChanged?.Invoke(health);
        }
        if (effect.speed != speed)
        {
            speed = effect.speed;
            OnSpeedChanged?.Invoke(speed);
        }
        if (effect.luck != luck)
        {
            luck = effect.luck;
            OnLuckChanged?.Invoke(luck);
        }
        if (effect.dodge != dodge)
        {
            dodge = effect.dodge;
            OnDodgeChanged?.Invoke(dodge);
        }
    }
Beispiel #6
0
        private void Damage(GameObject obj, object amount)
        {
            if (m_LastDamagedTime > Time.fixedTime && m_RestartSceneAfterDie)
            {
                return;
            }

            if (obj != gameObject)
            {
                if (allColliders.Find(x => x.gameObject == obj) == null)
                {
                    return;
                }
            }

            m_CurrentHealth -= (float)amount;
            if (m_CurrentHealth <= 0)
            {
                m_CurrentHealth = 0;
                Die();
            }
            else
            {
                OnReceiveDamage.Invoke();
                OnCharacterDamage?.Invoke();
                if (m_HurtClip != null && m_AudioManager != null)
                {
                    m_AudioManager.PlayVoiceSound(m_HurtClip);
                }
            }

            m_LastDamagedTime = Time.fixedTime + 0.1f;
            OnHealthChanged?.Invoke();
        }
Beispiel #7
0
        public int AddHealth(int value)
        {
            if (IsDead)
            {
                return(0);
            }

            Health += value;
            if (Health >= c_MaxHealth)
            {
                Health = c_MaxHealth;
            }

            if (Health <= 0)
            {
                Health = 0;
                IsDead = true;
            }

            OnHealthChanged.Invoke(Health);

            if (IsDead)
            {
                Image.Load(m_DeathBitmap);
                OnDie.Invoke();
            }

            return(Health);
        }
Beispiel #8
0
    public void Damage(int damage)
    {
        currentHealth -= damage;
        HitEffect();

        if (hitEvent != null)
        {
            hitEvent.Invoke();
        }

        if (currentHealth <= 0)
        {
            currentHealth = 0;
            DeathEffect();
            if (deathEvent != null)
            {
                deathEvent.Invoke();
            }
            Destroy(gameObject);
        }

        if (invTimer > 0)
        {
            invTimer         = invTimeAfterHit;
            gameObject.layer = INV_LAYER;
        }

        if (OnHealthChanged != null)
        {
            OnHealthChanged.Invoke(this);
        }
    }
Beispiel #9
0
 private void Start()
 {
     _maxHealth     = 80;
     _currentHealth = 80;
     OnHealthChanged?.Invoke(_currentHealth, _maxHealth);
     _timeSinceDamaged = _damageCd;
 }
Beispiel #10
0
 public virtual void Hurt(int damage)
 {
     _healthPoints -= damage;
     OnHealthChanged?.Invoke(this, new OnHealthChangedEventArgs {
         currentHealth = _healthPoints
     });                                                                                            //Apply UI;
 }
Beispiel #11
0
    public void TakeDamage(int damage)
    {
        damage -= armor.GetValue();
        if (!player)
        {
            damage *= GlobalConfigs.instance.ENEMY_LIFE_MODIFIER;
        }

        //damage=Mathf.Clamp(damage,1,int.MaxValue);
        if (damage <= 0)
        {
            damage = 1;
        }
        currentHealth -= damage;
        Debug.Log(transform.name + "takes " + damage + " damage.");
        if (currentHealth <= 0)
        {
            Die();
        }

        if (OnHealthChanged != null)
        {
            OnHealthChanged.Invoke(maxHealth, currentHealth);
        }
    }
Beispiel #12
0
    public void TakeDamage(int demage = 30)
    {
        this.PlayerHealth -= demage;
        AudioController.instance.PlayOneShot(this.damageClip);

        OnHealthChanged?.Invoke(this.PlayerHealth);
    }
Beispiel #13
0
        public float Damage(DamageRequest damageRequest)
        {
            if (IsDead)
            {
                return(0);
            }

            var damageAmount = damageRequest.CalculateAmount(System.AttributeSystem);

            currentHealth -= damageAmount;

            if (currentHealth > 0)
            {
                damageRequest.AmountDealt = damageAmount;
                OnDamageReceived?.Invoke(damageRequest);
                OnHealthChanged?.Invoke(CurrentHealth);
                return(damageAmount);
            }

            var actualDamageDealt = damageAmount + currentHealth;

            currentHealth             = 0;
            damageRequest.AmountDealt = actualDamageDealt;
            OnDamageReceived?.Invoke(damageRequest);
            OnHealthChanged?.Invoke(CurrentHealth);
            OnDied?.Invoke(damageRequest);
            return(actualDamageDealt);
        }
Beispiel #14
0
 public void Heal(float healAmount)
 {
     currentHP += healAmount;
     ClampHP();
     OnHealthChanged?.Invoke(healAmount);
     OnAnyCharacterHealthChanged?.Invoke(healAmount, this);
 }
Beispiel #15
0
 private void Start()
 {
     if (setHealthOnStart)
     {
         CurrentHealth = maxHealth;
         OnHealthChanged?.Invoke();
     }
 }
Beispiel #16
0
 private void HandleHealthChanged(float oldValue, float newValue)
 {
     OnHealthChanged?.Invoke(this, new HealthChangedEventArgs
     {
         MaxHealth     = maxHealth,
         CurrentHealth = currentHealth
     });
 }
Beispiel #17
0
 private void HandleHealthUpdated(int oldValue, int newValue)
 {
     OnHealthChanged?.Invoke(this, new HealthChangedEventArgs
     {
         CurrentHealth = currentHealth,
         MaxHealth     = maxHealth
     });
 }
Beispiel #18
0
 public void TakeDamage(float damage)
 {
     currentHP -= damage;
     ClampHP();
     OnHealthChanged?.Invoke(-damage);
     OnAnyCharacterHealthChanged?.Invoke(-damage, this);
     onDamageTaken?.Invoke(damage);
 }
Beispiel #19
0
 public void RemoveHealth(int amount)
 {
     currentHealth -= amount;
     currentHealth  = Mathf.Clamp(currentHealth, 0, maxHealth);
     OnHealthChanged?.Invoke(this, new OnHealthChangedEventArgs {
         newHealthAmount = currentHealth, maxHealthAmount = maxHealth
     });
 }
Beispiel #20
0
 public void TakeDamage(float damage)
 {
     if (_currentHealth > 0)
     {
         _currentHealth -= damage;
         OnHealthChanged?.Invoke(_currentHealth);
     }
 }
 private void Player2HealthChanged(int oldValue, int newValue)
 {
     OnHealthChanged?.Invoke(1, newValue, Player2Wards);
     if (newValue == 0)
     {
         OnDeathAction(1);
     }
 }
Beispiel #22
0
 public void TakeHeal(int heal)
 {
     if (_health < 100)
     {
         _health += heal;
         OnHealthChanged?.Invoke(_health);
     }
 }
Beispiel #23
0
 public void TakeAttack(int damage)
 {
     if (_health > 0)
     {
         _health -= damage;
         OnHealthChanged?.Invoke(_health);
     }
 }
Beispiel #24
0
 public void RestoreState(object state)
 {
     _health = (int)state;
     if (_health == 0)
     {
         Die();
     }
     OnHealthChanged?.Invoke(_health);
 }
Beispiel #25
0
 public void TakeDamage(int damage)
 {
     _health = Mathf.Max(_health - damage, 0);
     OnHealthChanged?.Invoke(_health);
     if (_health == 0)
     {
         Die();
     }
 }
Beispiel #26
0
 public void Damage(int damageAmount)
 {
     health -= damageAmount;
     if (health < 0)
     {
         health = 0;
     }
     OnHealthChanged?.Invoke();
 }
Beispiel #27
0
 public void Heal(int amount)
 {
     _health += amount;
     if (_health > _maxHealth)
     {
         _health = _maxHealth;
     }
     OnHealthChanged?.Invoke();
 }
Beispiel #28
0
 public void Damage(int amount)
 {
     _health -= amount;
     if (_health < 0)
     {
         _health = 0;
     }
     OnHealthChanged?.Invoke();
 }
Beispiel #29
0
 public void Heal(int healAmount)
 {
     health += healAmount;
     if (health > healthMax)
     {
         health = healthMax;
     }
     OnHealthChanged?.Invoke();
 }
Beispiel #30
0
 private void OnEnable()
 {
     currentHealth = stats.maxHealth;
     if (GameManager.Instance != null && this == GameManager.Instance.CurrentViking)
     {
         OnHealthChanged?.Invoke(this, currentHealth, stats.maxHealth);
         OnStaminaChanged?.Invoke(this, currentStamina, stats.maxStamina);
     }
 }