private void HealthChanged(float prevHitPoints, float nextHitPoints) { if (Mathf.Approximately(nextHitPoints, 0f)) { OnHealthZero.Invoke(prevHitPoints, 0f); } else if (Mathf.Approximately(nextHitPoints, hitPoints)) { OnHealthFull.Invoke(prevHitPoints, hitPoints); } else { OnHealthChanged.Invoke(prevHitPoints, nextHitPoints); } }
/// <summary> /// Lose health. The health loss is never more than zero /// </summary> /// <param name="value"></param> public void LoseHealth(Attack attack) { if (health - attack.value < 0) { attack.value = health; } health -= attack.value; OnHealthChanged?.Invoke(attack); // In the case of the health going down to zero if (health <= 0) { OnHealthZero?.Invoke(attack.owner); } }
public void Damage(int damage) { if (_health - damage >= 0) { _health -= damage; } else { _health = 0; } OnHealthChanges?.Invoke(this, EventArgs.Empty); if (_health == 0) { OnHealthZero?.Invoke(this, EventArgs.Empty); } }