/// <summary> /// Unsubscribe all subscribers from this instance events. /// </summary> private void UnsubscribeEvents() { if (OnHealthChanges != null) { foreach (Action <ChangedHealthArgs> handler in OnHealthChanges.GetInvocationList()) { OnHealthChanges -= handler; } } if (OnStopShowing != null) { foreach (Action <IShowable> handler in OnStopShowing.GetInvocationList()) { OnStopShowing -= handler; } } if (OnShowableDataChanges != null) { foreach (Action <ShowableData> handler in OnShowableDataChanges.GetInvocationList()) { OnShowableDataChanges -= handler; } } }
/// <summary> /// Updates the showable health in the UI. /// </summary> protected void UpdateShowableHealth() { var changedHealth = new ChangedHealthArgs( currentHealth: HealPoints, fullness: HealPoints > 0 ? (float)HealPoints / Parameters.HealPointsMax : 0, description: UnitDescription ); OnHealthChanges?.Invoke(changedHealth); }
public void Heal(int amount) { if (_health + amount <= _maxHealth) { _health += amount; } else { _health = _maxHealth; } OnHealthChanges?.Invoke(this, EventArgs.Empty); }
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); } }
/// <summary> /// Called when any squad unit has died. /// </summary> /// <param name="died">The died.</param> private void OnUnitDieHandler(IDying died) { died.OnDie -= OnUnitDieHandler; if (died is Unit unit) { unit.OnHealthChanges -= OnUnitHealthChangesHandler; units.Remove(unit); if (units.Count > 0) { var changedHealth = GetChangedHealthArgs(); OnHealthChanges?.Invoke(changedHealth); } else { OnStopShowing?.Invoke(this); OnDie?.Invoke(this); } } }
/// <summary> /// Applies the damage. /// </summary> /// <param name="attacker">The attacker.</param> /// <param name="damage">The damage.</param> /// <exception cref="ArgumentOutOfRangeException">damage - Must be greater than or equal to 0.</exception> public void ApplyDamage(ITeamMember attacker, float damage) { if (damage < 0) { throw new ArgumentOutOfRangeException(nameof(damage), "Must be greater than or equal to 0."); } HealPoints -= (int)damage; if (HealPoints <= 0) { Die(); } var changedHealth = new ChangedHealthArgs( currentHealth: HealPoints, fullness: HealPoints > 0 ? (float)HealPoints / healPointsMax : 0f, description: GetHealthDescription() ); OnHealthChanges?.Invoke(changedHealth); }
/// <summary> /// Called when the health of any unit within the squad has changed. /// </summary> /// <param name="changedHealthArgs">The changed health arguments.</param> private void OnUnitHealthChangesHandler(ChangedHealthArgs changedHealthArgs) { var changedHealth = GetChangedHealthArgs(); OnHealthChanges?.Invoke(changedHealth); }