293 lines
7.1 KiB
C#
293 lines
7.1 KiB
C#
using Kryz.CharacterStats.Examples;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class Health : Resource
|
|
{
|
|
protected CharacterStats character;
|
|
protected EntityEventBroker broker;
|
|
|
|
protected AbsorbEffectInstance absorbEffectInstance;
|
|
protected DamageIncomeModifierEffectInstance damageIncomeModifierEffectInstance;
|
|
|
|
public UnityEvent<float> onMaxHealthChanged = new UnityEvent<float>();
|
|
public UnityEvent onDeath = new UnityEvent();
|
|
|
|
private bool noCost = false;
|
|
public bool NoCost => noCost;
|
|
|
|
protected bool invulnerable = false;
|
|
public bool Invulnerable => invulnerable;
|
|
|
|
float incomingValue;
|
|
float percentStatMitigation;
|
|
float reducedDamage;
|
|
|
|
private bool isDead;
|
|
|
|
public UnityEvent<bool> OnInvulnerabilityStateChanged = new UnityEvent<bool>();
|
|
public UnityEvent OnDodgedSuccessfully = new UnityEvent();
|
|
|
|
public void SetIsDeadState(bool isDead)
|
|
{
|
|
this.isDead = isDead;
|
|
canRegen = !isDead;
|
|
}
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
broker = GetComponent<EntityEventBroker>();
|
|
character = GetComponent<CharacterStats>();
|
|
absorbEffectInstance = GetComponent<AbsorbEffectInstance>();
|
|
damageIncomeModifierEffectInstance = GetComponent<DamageIncomeModifierEffectInstance>();
|
|
|
|
|
|
|
|
character.onAllStatsUpdated.AddListener(CalculateMaxValueBasedOnStat);
|
|
}
|
|
|
|
protected override void Start()
|
|
{
|
|
baseRegenValue = character.GetStat("healthregen").BaseValue;
|
|
|
|
baseMaxValue = character.GetStat("maxhealth").BaseValue;
|
|
|
|
base.Start();
|
|
}
|
|
|
|
public bool EnoughHealth(float cost)
|
|
{
|
|
if (noCost || invulnerable) return true;
|
|
|
|
return cost <= currentValue;
|
|
}
|
|
|
|
public void SetInvulnerabilityState(bool isInvulnerable)
|
|
{
|
|
invulnerable = isInvulnerable;
|
|
OnInvulnerabilityStateChanged.Invoke(invulnerable);
|
|
}
|
|
|
|
|
|
protected void HandleDamageIncomeModifierEffects()
|
|
{
|
|
if (incomingValue < 0)
|
|
{
|
|
|
|
if (damageIncomeModifierEffectInstance.IsActive)
|
|
{
|
|
//Debug.Log("Incoming damage b4 mitigation: " + incomingValue);
|
|
|
|
incomingValue = damageIncomeModifierEffectInstance.ModifyDamageIncome(incomingValue);
|
|
|
|
if (incomingValue > 0) //avoid damage ultra mitigated turning into healing
|
|
incomingValue = 0;
|
|
}
|
|
//Debug.Log("Incoming damage after mitigation: " + incomingValue);
|
|
}
|
|
}
|
|
|
|
protected void HandleAbsorbEffects()
|
|
{
|
|
if (incomingValue < 0)
|
|
{
|
|
if (absorbEffectInstance.IsActive)
|
|
{
|
|
//Debug.Log("Incoming damage b4 absorbs: " + incomingValue);
|
|
|
|
incomingValue = absorbEffectInstance.AbsorbDamage(incomingValue);
|
|
|
|
if (incomingValue > 0) //avoid complete absorbs turning into healing
|
|
incomingValue = 0;
|
|
}
|
|
//Debug.Log("Incoming damage after absorbs: " + incomingValue);
|
|
}
|
|
}
|
|
|
|
public void ApplyDamage(DamageArgs args)
|
|
{
|
|
if (isDead) return;
|
|
|
|
broker.OnIncomingDamage.Invoke(args);
|
|
|
|
UpdateValue(args.currentValue);
|
|
|
|
CheckForDeath(args);
|
|
|
|
broker.OnIncomingDamageProcessed.Invoke(args);
|
|
}
|
|
public void ApplyHeal(HealArgs args)
|
|
{
|
|
if (isDead) return;
|
|
|
|
broker.OnIncomingHeal.Invoke(args);
|
|
|
|
UpdateValue(args.currentValue);
|
|
|
|
broker.OnIncomingHealProcessed.Invoke(args);
|
|
}
|
|
|
|
private void UpdateValue(float incomingValue)
|
|
{
|
|
currentValue += incomingValue;
|
|
|
|
currentValue = Mathf.Clamp(currentValue, 0, maxValue);
|
|
|
|
//Debug.Log("CurrentHealth: " + currentValue);
|
|
onResourceChanged.Invoke(currentValue);
|
|
}
|
|
private void CheckForDeath(DamageArgs args)
|
|
{
|
|
if (currentValue == 0)
|
|
{
|
|
//dead;
|
|
isDead = true;
|
|
args.targetDead = true;
|
|
onDeath.Invoke();
|
|
}
|
|
}
|
|
|
|
public override void ChangeValue(float value)
|
|
{
|
|
//Debug.Log("Value to change: " + value);
|
|
|
|
if (isDead) return;
|
|
|
|
incomingValue = value;
|
|
|
|
if (invulnerable && incomingValue < 0) return;
|
|
|
|
HandleDamageIncomeModifierEffects();
|
|
|
|
HandleAbsorbEffects();
|
|
|
|
currentValue += incomingValue;
|
|
|
|
currentValue = Mathf.Clamp(currentValue, 0, maxValue);
|
|
if (currentValue == 0)
|
|
{
|
|
//dead;
|
|
onDeath.Invoke();
|
|
}
|
|
//Debug.Log("CurrentHealth: " + currentValue);
|
|
onResourceChanged.Invoke(currentValue);
|
|
}
|
|
|
|
public void ChangeValueDirect(float value)
|
|
{
|
|
//Debug.Log("Value to change: " + value);
|
|
|
|
if (isDead) return;
|
|
|
|
incomingValue = value;
|
|
|
|
if (invulnerable && incomingValue < 0) return;
|
|
|
|
currentValue += incomingValue;
|
|
|
|
currentValue = Mathf.Clamp(currentValue, 0, maxValue);
|
|
if (currentValue == 0)
|
|
{
|
|
//dead;
|
|
onDeath.Invoke();
|
|
}
|
|
//Debug.Log("CurrentHealth: " + currentValue);
|
|
onResourceChanged.Invoke(currentValue);
|
|
}
|
|
|
|
public void ChangeValue(float value, int dmgType, bool isCrit)
|
|
{
|
|
//Debug.Log("Value to change: " + value);
|
|
|
|
if (isDead) return;
|
|
|
|
incomingValue = value;
|
|
|
|
//Debug.Log(gameObject.name + " receiving dmg = " + incomingValue + " before mitigations " + (DamageType)dmgType);
|
|
|
|
if (invulnerable && incomingValue < 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (incomingValue < 0)
|
|
{
|
|
HandleNegativeValue(dmgType, isCrit);
|
|
}
|
|
else
|
|
{
|
|
HandlePositiveValue();
|
|
}
|
|
}
|
|
|
|
protected void HandleNegativeValue(int dmgType, bool isCrit)
|
|
{
|
|
if (incomingValue >= 0) return;
|
|
|
|
|
|
HandleDamageIncomeModifierEffects();
|
|
|
|
|
|
HandleAbsorbEffects();
|
|
|
|
currentValue += incomingValue;
|
|
|
|
currentValue = Mathf.Clamp(currentValue, 0, maxValue);
|
|
|
|
|
|
|
|
if (currentValue == 0)
|
|
{
|
|
//dead;
|
|
onDeath.Invoke();
|
|
}
|
|
//Debug.Log("CurrentHealth: " + currentValue);
|
|
onResourceChanged.Invoke(currentValue);
|
|
}
|
|
|
|
protected void HandlePositiveValue()
|
|
{
|
|
currentValue += incomingValue;
|
|
|
|
currentValue = Mathf.Clamp(currentValue, 0, maxValue);
|
|
|
|
|
|
if (currentValue == 0)
|
|
{
|
|
//dead;
|
|
onDeath.Invoke();
|
|
}
|
|
//Debug.Log("CurrentHealth: " + currentValue);
|
|
onResourceChanged.Invoke(currentValue);
|
|
}
|
|
|
|
public override float GetMaxValue()
|
|
{
|
|
return base.GetMaxValue();
|
|
}
|
|
|
|
public virtual void CalculateMaxValueBasedOnStat()
|
|
{
|
|
maxValue = character.GetStat("maxhealth").Value;
|
|
|
|
CalculateRegenValueBasedOnStat();
|
|
|
|
onMaxHealthChanged.Invoke(maxValue);
|
|
}
|
|
public void CalculateRegenValueBasedOnStat()
|
|
{
|
|
flatRegen = character.GetStat("healthregen").Value;
|
|
}
|
|
|
|
public override void SetMaxValue(float value)
|
|
{
|
|
base.SetMaxValue(value);
|
|
|
|
onMaxHealthChanged.Invoke(maxValue);
|
|
}
|
|
|
|
|
|
}
|