using System.Collections; using System.Collections.Generic; using UnityEngine; public class Unit : MonoBehaviour { private UnitPosition position; private UnitHealth health; private UnitStats stats; private List activeEffects; void Start() { position = GetComponent(); health = GetComponent(); stats = GetComponent(); } public void Heal(int amount) { activeEffects.ForEach(delegate (GenericEffect effect) { effect.BeforeHeal(this, amount); }); if (health != null) { health.Heal(amount); } activeEffects.ForEach(delegate (GenericEffect effect) { effect.AfterHeal(this, amount); }); } void Damage(int amount) { activeEffects.ForEach(delegate (GenericEffect effect) { effect.BeforeDamage(this, amount); }); if (health != null) { health.Damage(amount); if (health.currentHealth == 0) { Die(); } } activeEffects.ForEach(delegate (GenericEffect effect) { effect.AfterDamage(this, amount); }); } void Die() { bool deathPrevented = activeEffects.TrueForAll(delegate (GenericEffect effect) { return effect.BeforeDeath(this); }); if(deathPrevented) { if(health.currentHealth <= 0) { health.currentHealth = 1; } return; } //Die. For real. } void ApplyNewEffect(GenericEffect newEffect) { activeEffects.ForEach(delegate (GenericEffect existingEffect) { existingEffect.BeforeNewEffect(this, newEffect); }); activeEffects.Add(newEffect); activeEffects.ForEach(delegate (GenericEffect existingEffect) { existingEffect.AfterNewEffect(this, newEffect); }); } }