mirror of
https://github.com/Steffo99/octogem.git
synced 2024-11-25 14:34:19 +00:00
64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
public class Unit : MonoBehaviour
|
|||
|
{
|
|||
|
private UnitPosition position;
|
|||
|
private UnitHealth health;
|
|||
|
private UnitStats stats;
|
|||
|
private List<GenericEffect> activeEffects;
|
|||
|
|
|||
|
void Start()
|
|||
|
{
|
|||
|
position = GetComponent<UnitPosition>();
|
|||
|
health = GetComponent<UnitHealth>();
|
|||
|
stats = GetComponent<UnitStats>();
|
|||
|
}
|
|||
|
|
|||
|
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); });
|
|||
|
}
|
|||
|
}
|