コード例 #1
0
    public void Modify(int amount)
    {
        m_currentHealth += amount;

        Mathf.Clamp(m_currentHealth, 0, m_maxHealth);

        if (amount < 0)
        {
            if (OnTakeDamage != null)
            {
                OnTakeDamage.Invoke();
            }
        }
        else if (amount > 0)
        {
            if (OnHeal != null)
            {
                OnHeal.Invoke();
            }
        }
        else
        {
            Debug.Log("Modify called with 0");
        }

        if (OnValueChange != null)
        {
            OnValueChange.Invoke(PercentHealth);
        }

        if (m_currentHealth <= 0 && OnDeath != null)
        {
            OnDeath.Invoke();
        }
    }
コード例 #2
0
        public void Damage(float amount, DamageableLayer layer = null)
        {
            if (!CanInflictDamage(layer))
            {
                return;
            }

            CurrentHealth -= Mathf.Abs(EvaluateDamage(amount));

            healthBar?.SetCurrentHealth(CurrentHealth);

            if (CurrentHealth <= 0f)
            {
                IsDead = true;
                OnDied?.Invoke(this, EventArgs.Empty);

                if (destroyHealthBarOnDied)
                {
                    Destroy(healthBarComponent);
                }
                if (DestroyOnDied)
                {
                    destroyBehaviour.Destroy();
                }
                return;
            }

            if (CurrentHealth < baseHealth)
            {
                OnTakeDamage?.Invoke(this, EventArgs.Empty);
            }
        }
コード例 #3
0
        public void TakeDamage(Stats playerStats)
        {
            if (isDead)
            {
                return;
            }

            if (Time.time < timeUntilCanBeDamaged)
            {
                return;
            }

            if (!target)
            {
                target = playerStats.transform;
            }

            var damage = CalculateDamage(playerStats);

            health.CurrentValue -= damage;

            OnTakeDamage?.Invoke(damage);

            if (health.CurrentValue == 0)
            {
                enemyCollider.enabled = false;
                animator.SetTrigger(Dead);
                isDead = true;
            }
            else
            {
                animator.SetTrigger(Hit);
                timeUntilCanBeDamaged = Time.time + 1.5f;
            }
        }
コード例 #4
0
 public void TakeDamage(float inDamage, Vector2 inDirection)
 {
     if (!IsAlive)
     {
         return;
     }
     Health          -= inDamage;
     LastHitDirection = inDirection;
     OnTakeDamage?.Invoke(this, inDirection);
 }
コード例 #5
0
        public void DealDamage(int amount)
        {
            currentHealth -= amount;
            OnTakeDamage?.Invoke(GetHealthPercent());

            if (currentHealth <= 0)
            {
                Destroy(gameObject);
            }
        }
コード例 #6
0
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.transform.parent != transform &&
         collision.gameObject.CompareTag("Hitbox"))
     {
         TakeDamage();
         PlayHurtAudio();
         OnTakeDamage?.Invoke(transform.position, Vector3.zero);
     }
 }
コード例 #7
0
ファイル: Health.cs プロジェクト: toto9898/Unity-Homewoks
    public void TakeDamage()
    {
        int damage = 10;

        health = Max(health - damage, 0);
        animator.SetInteger("Health", health);
        animator.SetTrigger("TookDamage");

        OnTakeDamage?.Invoke();
    }
コード例 #8
0
 public virtual void TakeDamage(int damage, DamageType damageType, Unit attacker)
 {
     Health -= damage;
     OnCurrentHealthChanged?.Invoke(Health);
     if (Health <= 0)
     {
         OnDefeated?.Invoke();
     }
     OnTakeDamage?.Invoke(damage, damageType, attacker);
 }
コード例 #9
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        MovementController movementController = gameObject.GetComponent <MovementController>();

        if (collision.transform.parent != transform &&
            (collision.gameObject.CompareTag("Hitbox") || collision.gameObject.CompareTag("PunchHitbox")))
        {
            TakeDamage();
            OnTakeDamage?.Invoke();
        }
    }
コード例 #10
0
    public void TakeDamage(float damage)
    {
        Health -= damage;

        if (Dead())
        {
            Health = 0;
        }

        OnTakeDamage?.Invoke(this, EventArgs.Empty);
    }
コード例 #11
0
    public override void TakeDamage(int damage)
    {
        totalDamageTaken += damage;

        if (!isClient)
        {
            OnTakeDamage.Invoke();
        }

        // TODO: Add reaction seen in avatar by other players

        base.RpcTakeDamage(damage);
    }
コード例 #12
0
    void TakeDamage()
    {
        int overallHealth = health + armor;

        if (overallHealth > 0)
        {
            overallHealth -= 10;
        }

        armor  = (overallHealth / 100) * (overallHealth % 100);
        health = overallHealth - armor;

        OnTakeDamage?.Invoke();
    }
コード例 #13
0
ファイル: Health.cs プロジェクト: FSorek/Essence2021
    public void TakeDamage(float damage)
    {
        if (!IsAlive)
        {
            return;
        }

        CurrentHealth -= damage;
        OnTakeDamage?.Invoke();
        if (CurrentHealth <= 0)
        {
            IsAlive = false;
        }
    }
コード例 #14
0
 public override void TakeDamage(float value)
 {
     if (_health <= 0)
     {
         return;
     }
     OnTakeDamage?.Invoke();
     StartCoroutine(Crumble(value));
     if (_health <= 0)
     {
         GetComponent <Collider>().enabled = false;
         OnDeath?.Invoke();
     }
 }
コード例 #15
0
 void Awake()
 {
     if (onDeath == null)
     {
         onDeath = new OnDeath();
     }
     if (onSimpleDeath == null)
     {
         onSimpleDeath = new OnSimpleDeath();
     }
     if (onTakeDamage == null)
     {
         onTakeDamage = new OnTakeDamage();
     }
 }
コード例 #16
0
ファイル: Health.cs プロジェクト: byyjoww/Elysium.Combat
        public bool TakeDamage(int amount, ISource _source)
        {
            if (IsDead)
            {
                Debug.Log($"Health is dead");
                return(false);
            }

            Resource.Lose(amount, _source);
            OnTakeDamage?.Invoke(amount, _source);
            if (Resource.Current <= 0)
            {
                Die();
            }
            return(true);
        }
コード例 #17
0
ファイル: CharacterStats.cs プロジェクト: ktndrnl/BrackeysRPG
    public void TakeDamage(int damage)
    {
        damage -= armor.GetValue();
        damage  = Mathf.Clamp(damage, 0, int.MaxValue);

        currentHealth -= damage;
        Debug.Log($"{transform.name} takes {damage.ToString()} damage.");

        OnHealthChanged?.Invoke(maxHealth, currentHealth);
        OnTakeDamage?.Invoke(damage);

        if (currentHealth <= 0)
        {
            Die();
        }
    }
コード例 #18
0
    protected override void Start()
    {
        base.Start();

        // Add turn aggressive listeners
        TurnAggressive.AddListener(() => {
            TurnAggressiveWrapper(false);
        });
        TurnAggressiveHyperseed.AddListener(() => {
            TurnAggressiveWrapper(true);
        });
        // Aggro enemies when damage is taken & play damaged SFX
        OnTakeDamage.AddListener(() => {
            // If enemy damaged sound is playing, don't repeat
            if (EnemySoundSingleton.instance.DamageTakenSoundActive == false)
            {
                EnemySoundSingleton.instance.DamageTakenSoundActive = true;
                _enemyFX.DamageTaken.Invoke();
            }
            // Aggro group
            GetComponentInParent <EnemyGroup>()?.OnEnemyDamage.Invoke();
        });
        // Death Event
        OnDeath.AddListener(() => {
            if (currentBehavior != null)
            {
                StopCoroutine(currentBehavior);
            }
            currentBehavior = StartCoroutine(Die());
        });

        spawnPosition = transform.position;

        // Start behavior
        if (currentState == EnemyState.Aggressive)
        {
            currentBehavior = StartCoroutine(Idle());
            TurnAggressive.Invoke();
        }
        else
        {
            currentBehavior = StartCoroutine(Idle());
        }

        StartCoroutine(CheckBehavior());
        StartCoroutine(EnemyModifications());
    }
コード例 #19
0
    /// <summary>
    /// Makes this object take damage and decrease its health if it is not invulnerable.
    /// </summary>
    /// <param name="_damage">Amount of damage this inflect to this object.</param>
    /// <returns>Returns true if some damages were inflicted, false if none.</returns>
    public virtual bool TakeDamage(int _damage)
    {
        if (IsInvulnerable || isDead)
        {
            return(false);
        }

        if (PhotonNetwork.isMasterClient)
        {
            TDS_RPCManager.Instance?.RPCPhotonView.RPC("CallMethodOnline", PhotonTargets.Others, TDS_RPCManager.GetInfo(photonView, GetType(), "TakeDamage"), new object[] { _damage });
        }

        HealthCurrent -= _damage;
        OnTakeDamage?.Invoke(_damage);

        return(true);
    }
コード例 #20
0
    public void TakeDamage(int amount)
    {
        int prev = Current;

        Current = Mathf.Clamp(Current - amount, 0, Max);
        Debug.Log($"Takes {amount} damage | Before: {prev} | After: {Current}.");

        OnTakeDamage?.Invoke(prev, Current);
        OnChanged?.Invoke(prev, Current);

        if (Current <= 0)
        {
            Current = 0;
            OnHealthEmpty?.Invoke();
            Debug.Log($"Health Empty = Dead.");
        }
    }
コード例 #21
0
    // Method in charge of making this DamageReceiver have it's health decrement & receive knockback.
    // Takes in parameter damage of type DamagePacket.
    // DamagePackets are 3 parts: DamageType, DamageAmount, and KnockbackVector.
    // DamagePackets MUST define a DamageType, but defining DamageAmount and KnockbackVector are optional (will default to 1 and Vector2.zero, respectively)
    public virtual void TakeDamage(DamagePacket damage, Vector2?hitPoint = null)
    {
        //Already dead don't do anything
        if (!IsAlive)
        {
            return;
        }

        //If this DamageReciever isn't resistant to the type of damage it's taking, subtract the damage from health.
        if (!ResistCollisionDamage && damage.Type == DamageType.COLLISION || !ResistProjectileDamage && damage.Type == DamageType.PROJECTILE || !ResistExplosionDamage && damage.Type == DamageType.EXPLOSION)
        {
            _health -= damage.DamageAmount;
            if (damage.DamageAmount > 0)
            {
                OnTakeDamage?.Invoke(damage);
                Debug.Log(name + " takes " + damage.DamageAmount + " " + damage.Type + " damage from " + damage.DamageDealer);
                damage.DamageDealer?.OnDamageDealtTo(this);
            }
        }

        if (damage.DamageAmount > 0 && DamageFlashCoroutine == null && DoDamageFlash)
        {
            DamageFlashCoroutine = StartCoroutine(DamageFlash());
        }

        //If there is a rigidbody2D on this GameObject and this DamageReciever doesn't resist knockback, apply knockback from DamagePacket.
        Rigidbody2D rb = GetComponent <Rigidbody2D>();

        if (rb && !ResistKnockback)
        {
            if (hitPoint.HasValue)
            {
                rb.AddForceAtPosition(damage.KnockbackVector, hitPoint.Value, ForceMode2D.Impulse);
            }
            else
            {
                rb.AddForce(damage.KnockbackVector, ForceMode2D.Impulse);
            }
        }

        if (!IsAlive && CanDie)
        {
            Die(damage.DamageDealer);
        }
    }
コード例 #22
0
        private void OnCollisionEnter2D(Collision2D collision)
        {
            if (Manager.Game.IsPaused)
            {
                return;
            }

            Enemy worldEnemy = collision.collider.GetComponent <Enemy>();

            if (worldEnemy != null)
            {
                TakeDamage(worldEnemy.TouchDamage * Inventory.DamageModifier);
                OnTakeDamage?.Invoke(this, EventArgs.Empty);
                if (IsDead())
                {
                    DestroySelf();
                }
            }
        }
コード例 #23
0
ファイル: Earth.cs プロジェクト: lynkx21/moon-shield
        public void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.gameObject.tag == "Enemy")
            {
                sfxManager.PlaySound(sfxManager.earthHit);
                Destroy(collision.gameObject);
                hitPoints--;
            }
            else if (collision.gameObject.tag == "Moon")
            {
                Destroy(collision.gameObject);
                hitPoints = 0;
            }

            if (OnTakeDamage != null)
            {
                OnTakeDamage.Invoke(hitPoints);
            }
        }
コード例 #24
0
    public float TakeDamage(DamageInfo info)
    {
        OnTakeDamage?.Invoke(info);
        float dmg = info.Damage * DamageMatrix.GetDamageFactor(info.Type, ArmorType);

        CurrentHealth -= dmg;
        OnDamageTaken?.Invoke(dmg);
        if (CurrentHealth <= 0f && !_isDead)
        {
            Die();
            OnDeath?.Invoke();
            _isDead = true;
        }
        if (CurrentHealth > MaxHealth)
        {
            CurrentHealth = MaxHealth;
        }
        return(CurrentHealth);
    }
コード例 #25
0
    // --v-- Damage Management --v--

    public void TakeDamage(int amountOfDamage = 1)
    {
        // Update life
        _currentLife -= amountOfDamage;
        _currentLife  = Mathf.Max(0, _currentLife);

        OnTakeDamage?.Invoke();

        // Death
        if (_currentLife == 0)
        {
            Instantiate(_explosionPrefab, transform.position, Quaternion.identity);

            DestructionByDamage();
        }
        else // Damage Animation
        {
            StartCoroutine(BlinkCoroutine(0.5f));
        }
    }
コード例 #26
0
    public override void TakeDamage(float value)
    {
        if (_isInvincible)
        {
            return;
        }

        _health -= value;
        if (currentState != PlayerState.Dead)
        {
            OnTakeDamage?.Invoke();
        }
        if (_health <= 0)
        {
            currentState = PlayerState.Dead;
        }
        else
        {
            _animator.SetBool("damageAni", true);
        }

        StartCoroutine("InvincibleAfterDmg");
    }
コード例 #27
0
    public void TakeDamage(int damage)
    {
        if (damage == 0)
        {
            return;
        }

        if (Time.time > damageTime)
        {
            m_life = Mathf.Max(0, m_life - damage);
            OnTakeDamage?.Invoke(damage);

            if (damageCooldown > 0)
            {
                damageTime = Time.time + damageCooldown;
            }

            if (m_life <= 0)
            {
                OnDeath?.Invoke();
                Destroy(gameObject, destroyDelay);
            }
        }
    }
コード例 #28
0
 public void RemoveOnTakeDamage(OnTakeDamage otd)
 {
     on_take_damages.Remove(otd);
 }
コード例 #29
0
 public void AddTakeDamage(OnTakeDamage otd)
 {
     on_take_damages.Add(otd);
 }
コード例 #30
0
 public void TakeDamage()
 {
     OnTakeDamage?.Invoke();
 }