private void OnCollisionEnter2D(Collision2D collision)
    {
        GameObject target    = collision.gameObject;
        string     targetTag = target.tag;

        if (target.GetComponent <Rigidbody2D>().bodyType != RigidbodyType2D.Static)
        {
            if (targetTag == "NPC")
            {
                OnHitEnemyBodyVFX?.Invoke(collision.gameObject, collision.relativeVelocity,
                                          collision.transform.position);
            }
            else if (targetTag == "Rocket")
            {
                OnHitEnemyRocket?.Invoke(collision.transform.position);
                if (!GameController.gameEnded)
                {
                    OnHealthChange?.Invoke(reduceHealthBy(20));
                }
            }
            else if (targetTag == "pickup_health")
            {
                if (!GameController.gameEnded && !(getHealthPrcnt() == 1))
                {
                    OnHealthChange?.Invoke(increaseHealthBy(20));
                    GameObject hitText = Pool.Instance.Get(Pool.GameObjectType.hitText);
                    hitText.GetComponent <HitTextController>()
                    .Setup(20, transform.position, HitTextController.UNIT.HP);
                }
            }
        }
    }
    public bool TakeDamage(int dmg)
    {
        // Decrease help by damage
        // -----------------------
        health -= ComputeDamage(dmg);

        // Play audioclip
        // ---------------
        audioSource.Play();
        onHealthChange?.Invoke(health);

        // Early exit (Still alive)
        // -----------------------
        if (health > 0)
        {
            return(false);
        }

        // Call Die
        // --------
        Die();

        // Return true
        // -----------
        return(true);
    }
    public void TakeDamage(float amount)
    {
        if (amount < 0)
        {
            throw new System.Exception("You can't deal a negative damage value to the character health");
        }

        if (OnReceiveDamage != null)
        {
            OnReceiveDamage.Invoke(amount);
        }

        _health -= amount;
        _health  = Mathf.Clamp(_health, 0, maxHealth);

        if (OnHealthChange != null)
        {
            OnHealthChange.Invoke(_health);
        }

        if (_health == 0 && OnDie != null)
        {
            OnDie.Invoke();
        }
    }
Exemple #4
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);
        }
        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);
        }
Exemple #6
0
    public override void ModifyStat(string statName, Modifier mod)
    {
        var valids = new List <string>(Enum.GetNames(typeof(StatType)));

        if (!valids.Contains(statName))
        {
            Debug.LogWarningFormat("stat:: {0}, is not a valid stat to modify", statName);
            return;
        }

        PlayerStats.AddModifier(modcount++, mod);

        if (statName == "Health")
        {
            onHealthChange.Invoke(PlayerStats[statName].Value);
        }
        onStatModify.Invoke(statName);
    }
Exemple #7
0
 private void PlayerHitByObstacle(int currentHealth)
 {
     OnHealthChange?.Invoke(currentHealth);
     if (currentHealth <= 0)
     {
         gameOver = true;
         OnGameOver?.Invoke();
     }
 }
Exemple #8
0
    public void ChangeHealth(int value)
    {
        var last_health = current_health;

        current_health = Mathf.Clamp(current_health + value, 0, max_health);
        var health_diff = current_health - last_health;

        on_health_change?.Invoke(health_diff);
    }
Exemple #9
0
 public void TakeDamage(int damage)
 {
     if (damage != 0)
     {
         _currentHealth -= damage;
         _currentHealth  = Mathf.Clamp(_currentHealth, 0, _maxHealth);
         OnHealthChange?.Invoke(_currentHealth);
     }
 }
Exemple #10
0
        public void TakeDamage(Damage damage)
        {
            if (IsDead())
            {
                return;
            }

            currentHealth -= damage.GetValue();
            OnHealthChange?.Invoke(this, new HealthChangeEventArgs());
        }
Exemple #11
0
 public void TakeDamage(int amount)
 {
     currentHealth -= amount;
     Debug.Log(gameObject + " take damage");
     OnHealthChange?.Invoke(this, EventArgs.Empty);
     if (currentHealth <= 0)
     {
         currentHealth = 0;
         UnitDie();
     }
 }
        public void Heal(int healAmount = 1)
        {
            health += healAmount;

            if (health > maxHealth || health < 0)
            {
                health = Mathf.Clamp(health, 0, maxHealth);
            }

            OnHealthChange?.Invoke(CurrentHealthPercentage());
        }
Exemple #13
0
    public void SetHealth(int newHealth)
    {
        newHealth = Math.Max(0, Math.Min(maxHealth, newHealth));

        if (newHealth == current)
        {
            return;
        }

        current = newHealth;
        OnHealthChange?.Invoke(this);
    }
Exemple #14
0
    public void AdjustHitpoints(int delta)
    {
        if (hitPoints <= 0)
        {
            return;
        }

        int prevHealth = hitPoints;

        hitPoints += delta;
        OnHealthChange.Invoke(prevHealth, hitPoints);
    }
Exemple #15
0
 public void RestoreHealth(float amount)
 {
     if (amount > 0)
     {
         currentHealth += amount;
         if (currentHealth > maxHealth)
         {
             currentHealth = maxHealth;
         }
     }
     OnHealthChange?.Invoke(currentHealth, amount);
 }
Exemple #16
0
    void HandleHealthChange(IDamageable entity)
    {
        if (entity.Health <= 0)
        {
            Debug.Log("You died. We haven't done anything past this point ;)");
            // Unsubscribe so runtime errors don't happen.
            entity.OnHealthChanged -= HandleHealthChange;
            OnPlayerDeath?.Invoke(this);
        }

        OnHealthChange?.Invoke(entity);
    }
Exemple #17
0
        protected void UpdateCurrentStats()
        {
            currentMaxHealth = GetMaxHealth();

            if (currentHealth > currentMaxHealth)
            {
                currentHealth = currentMaxHealth;
            }

            HealthChangeEventArgs args = new HealthChangeEventArgs();

            OnHealthChange?.Invoke(this, args);
        }
Exemple #18
0
 public void DealDamage(float damage)
 {
     _currentHealth -= damage;
     OnHealthChange?.Invoke(this);
     if (_currentHealth <= 0f)
     {
         HandleWaveDeath();
     }
     else if (!_isFlashing)
     {
         StartCoroutine(nameof(FlashSpriteRenders));
     }
 }
Exemple #19
0
 public void TakeDamage(int damage)
 {
     CurrentValue -= damage;
     if (CurrentValue <= 0)
     {
         CurrentValue = 0;
         OnHealthChange?.Invoke(this, CurrentValue);
         _onZero?.Invoke(this, null);
     }
     else
     {
         OnHealthChange?.Invoke(this, CurrentValue);
     }
 }
    // Change the player's health by the given value
    public void ChangeHealth(int value)
    {
        if (OnHealthChange != null)
        {
            OnHealthChange.Invoke(value);
        }

        health += value;
        health  = Mathf.Clamp(health, 0, maxHealth);
        if (health <= 0 && !isDead)
        {
            Kill();
        }
    }
        public bool TakeDamage(int damage = 1)
        {
            hit.Play();
            health -= damage;

            OnHealthChange?.Invoke(CurrentHealthPercentage());

            if (health >= 0)
            {
                return(false);
            }

            OnDeath?.Invoke();
            return(true);
        }
Exemple #22
0
    public override void Sethealth(float health)
    {
        if (health >= maxHp)
        {
            health = maxHp;
        }
        else if (health <= 0)
        {
            Die();
        }

        this.health = health;
        UpdateHealthBar();
        OnHealthChange?.Invoke(this, EventArgs.Empty);
    }
    public void ChangeMaxHealth(int value)
    {
        if (!isDead)
        {
            if (OnHealthChange != null)
            {
                OnHealthChange.Invoke(value);
            }

            maxHealth += value;
            if (maxHealth < 1)
            {
                maxHealth = 1;
            }
        }
    }
Exemple #24
0
        public void Heal(Heal heal)
        {
            float healAmount = heal.GetValue() / 100f * GetMaxHealth();

            currentHealth += Mathf.FloorToInt(healAmount);

            if (currentHealth > currentMaxHealth)
            {
                currentHealth = currentMaxHealth;
            }

            HealthChangeEventArgs args = new HealthChangeEventArgs();

            args.healthChange = Mathf.FloorToInt(healAmount);
            args.modifier     = heal;
            OnHealthChange?.Invoke(this, args);
        }
    public void Restore(float amount)
    {
        if (amount < 0)
        {
            throw new System.Exception("You can't restore negative value to the character health");
        }

        if (OnRestoreHealth != null)
        {
            OnRestoreHealth.Invoke(amount);
        }

        _health += amount;
        _health  = Mathf.Clamp(_health, 0, maxHealth);

        if (OnHealthChange != null)
        {
            OnHealthChange.Invoke(_health);
        }
    }
        /// <summary>
        /// Add health to this fighter.
        /// </summary>
        /// <param name="healthAmount">The amount of health to add.</param>
        public void Heal(int healthAmount)
        {
            if (!IsAlive)
            {
                return;
            }

            int previousHealth = CurrentHealth;

            CurrentHealth += healthAmount;

            // Clamp health
            if (CurrentHealth > MaxHealth)
            {
                CurrentHealth = MaxHealth;
            }

            PointEventArgs args = new PointEventArgs(previousHealth, CurrentHealth);

            OnHealthChange?.Invoke(this, args);
        }
Exemple #27
0
        public virtual void Init()
        {
            // Lvl coefs
            stamina.onChange = null;
            int startStamina = 21 + ((level - 1) * STAMINA_TO_LVL_COEF);
            int startAP      = 20 + ((level - 1) * ATTACK_POWER_TO_LVL_COEF);

            stamina.RemoveModifier(startStamina);
            attackPower.RemoveModifier(startAP);
            stamina.AddModifier(startStamina);
            attackPower.AddModifier(startAP);

            currentHealth     = GetMaxHealth();
            currentMaxHealth  = GetMaxHealth();
            stamina.onChange += UpdateCurrentStats;
            actor             = GetComponent <Actor>();
            OnHealthChange?.Invoke(this, new HealthChangeEventArgs());
            if (isDead)
            {
                onDied?.Invoke(gameObject);
            }
        }
        public virtual void TakeDamage(int damage)
        {
            if (!IsAlive)
            {
                return;
            }

            int previousHealth = CurrentHealth;

            CurrentHealth -= damage;

            // Check for defeat
            if (CurrentHealth <= 0)
            {
                CurrentHealth = 0;
                OnDefeat?.Invoke(this, EventArgs.Empty);
            }

            PointEventArgs args = new PointEventArgs(previousHealth, CurrentHealth);

            OnHealthChange?.Invoke(this, args);
        }
Exemple #29
0
    public void TakeDamage(float amount)
    {
        ResolveStreak();

        if (isImmortal)
        {
            Debug.Log("Did not die, is immortal");
            return;
        }
        if (amount > 0)
        {
            currentHealth -= amount;
            if (currentHealth <= 0)
            {
                // GiveCurrencyBasedOnTimeAlive();
                GiveCurrencyBasedOnKills();
                SceneWrangler.Instance.LoadScene("Scenes/HoldingCell");
            }
        }
        FindObjectOfType <HurtScreen>().ShowHurtScreen();
        sfx.PlayAudioOneshot(hurtSound, 0.7f, 1.0f, 0.9f, 1.1f);
        OnHealthChange?.Invoke(currentHealth, -amount);
    }
Exemple #30
0
    public void OnTakeDamage(float damage, GameObject _instigator)
    {
        if (_instigator && _instigator.TryGetComponent <HealthController>(out var instigatorHealthController))
        {
            if (instigatorHealthController.TeamNumber == TeamNumber)
            {
                return;
            }
        }

        if (lastDamageTime + immunityAfterDamageTimer > Time.time)
        {
            return;
        }

        onDamageTextSpawn.Invoke(damage);

        health -= damage;

        OnHealthChange?.Invoke(this, new OnHealthChangeEventArgs {
            damageAmount = damage, currentHealth = health, owner = gameObject, instigator = _instigator
        });

        if (health <= 0)
        {
            bIsDead         = true;
            OnHealthChange -= HealthChange;
            return;
        }

        //Don't play the blinking when the gameobject is dead
        lastDamageTime = Time.time;
        if (flashMaterial)
        {
            StartCoroutine(Blink());
        }
    }