/// <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); }
private void HandleHealthChange(bool fireZeroHealthEvent = true) { healthChanged?.Invoke(_currentHealth, _maxHealth); if (fireZeroHealthEvent && _currentHealth <= 0 && !_zeroHealthEventFired) { zeroHealth?.Invoke(); _zeroHealthEventFired = true; } }
public void ChangeHealth(IDamageDealer changer, double damage) { if (!IsVulnerable) { return; } if (owner is EnemySystem enemy) { enemy.LastDamageDealer = changer; if (enemy.Data.Get(Enums.Enemy.Health).AppliedValue > 0) { enemy.Data.Get(Enums.Enemy.Health).AppliedValue -= damage; } else { enemy.Data.Get(Enums.Enemy.Health).Value -= damage; } var remainingHealth = enemy.Data.Get(Enums.Enemy.Health).AppliedValue > 0 ? enemy.Data.Get(Enums.Enemy.Health).AppliedValue : enemy.Data.Get(Enums.Enemy.Health).Value; if (remainingHealth <= 0) { GiveResources(); IsVulnerable = false; ZeroHealth?.Invoke(owner); } } #region Helper functions void GiveResources() { if (enemy.LastDamageDealer is SpiritSystem spirit) { spirit.AddExp((int)enemy.Data.Get(Numeral.Exp).Sum); } } #endregion }