251 lines
7.5 KiB
C#
251 lines
7.5 KiB
C#
using Kryz.CharacterStats.Examples;
|
|
using Photon.Pun;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class Health : Resource
|
|
{
|
|
protected CharacterStats character;
|
|
|
|
protected AbsorbEffectInstance absorbEffectInstance;
|
|
protected DamageIncomeModifierEffectInstance damageIncomeModifierEffectInstance;
|
|
|
|
public UnityEvent<float> onMaxHealthChanged = new UnityEvent<float>();
|
|
public UnityEvent onDeath = new UnityEvent();
|
|
|
|
[HideInInspector]
|
|
public PhotonView photonView;
|
|
|
|
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 void SetIsDeadState(bool isDead)
|
|
{
|
|
this.isDead = isDead;
|
|
canRegen = !isDead;
|
|
}
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
character = GetComponent<CharacterStats>();
|
|
photonView = GetComponent<PhotonView>();
|
|
absorbEffectInstance = GetComponent<AbsorbEffectInstance>();
|
|
damageIncomeModifierEffectInstance = GetComponent<DamageIncomeModifierEffectInstance>();
|
|
|
|
|
|
baseFlatRegen = flatRegen;
|
|
|
|
character.MaxHealth.BaseValue = baseMaxValue;
|
|
|
|
character.onUpdateStatValues.AddListener(CalculateMaxValueBasedOnStat);
|
|
}
|
|
|
|
protected override void Start()
|
|
{
|
|
if (!photonView.IsMine) return;
|
|
|
|
base.Start();
|
|
}
|
|
|
|
public void SetInvulnerabilityState(bool isInvulnerable)
|
|
{
|
|
if (!photonView.IsMine) return;
|
|
|
|
photonView.RPC(nameof(RPC_SetInvulnerabilityState), RpcTarget.All, isInvulnerable);
|
|
}
|
|
|
|
[PunRPC]
|
|
public void RPC_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 HandleStatMitigation(DamageType dmgType)
|
|
{
|
|
if (incomingValue < 0)
|
|
{
|
|
switch (dmgType)
|
|
{
|
|
case DamageType.Attack:
|
|
default:
|
|
{
|
|
percentStatMitigation = Mathf.Clamp((character.Armor.Value * GameConstants.CharacterStatsBalancing.PercentArmorIntoDamageReduction), 0,GameConstants.CharacterStatsBalancing.MaximumPercentDamageReductionFromArmor);
|
|
}
|
|
break;
|
|
case DamageType.Spell:
|
|
{
|
|
percentStatMitigation = Mathf.Clamp((character.MagicResistance.Value * GameConstants.CharacterStatsBalancing.PercentMagicResistanceIntoDamageReduction), 0, GameConstants.CharacterStatsBalancing.MaximumPercentDamageReductionFromMagicResistance);
|
|
}
|
|
break;
|
|
}
|
|
|
|
|
|
//Debug.Log(gameObject.name + " mitigating = " + percentStatMitigation + " percent");
|
|
reducedDamage = incomingValue * percentStatMitigation;
|
|
incomingValue += Mathf.Abs(reducedDamage);
|
|
//Debug.Log(gameObject.name + " receiving dmg = " + incomingValue + $" after mitigations from {(dmgType == DamageType.Attack ? nameof(character.Armor) : nameof(character.MagicResistance))}");
|
|
|
|
if (incomingValue > 0) //avoid damage ultra mitigated turning into healing
|
|
incomingValue = 0;
|
|
}
|
|
}
|
|
protected void HandleMagicResistanceMitigation()
|
|
{
|
|
|
|
}
|
|
|
|
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 override void ChangeValue(float value)
|
|
{
|
|
if (!photonView.IsMine) return;
|
|
//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 ChangeValue(float value, int dmgType)
|
|
{
|
|
if (!photonView.IsMine) return;
|
|
//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;
|
|
|
|
HandleDamageIncomeModifierEffects();
|
|
|
|
HandleStatMitigation((DamageType)dmgType);
|
|
|
|
HandleAbsorbEffects();
|
|
|
|
currentValue += incomingValue;
|
|
|
|
currentValue = Mathf.Clamp(currentValue, 0, maxValue);
|
|
if (currentValue == 0)
|
|
{
|
|
//dead;
|
|
onDeath.Invoke();
|
|
}
|
|
//Debug.Log("CurrentHealth: " + currentValue);
|
|
onResourceChanged.Invoke(currentValue);
|
|
}
|
|
|
|
public bool EnoughHealth(float cost)
|
|
{
|
|
if (invulnerable) return true;
|
|
|
|
return cost <= currentValue;
|
|
}
|
|
|
|
|
|
[PunRPC]
|
|
public void RPC_ChangeValueHealth(float value, int dmgType)
|
|
{
|
|
if (!photonView.IsMine) return;
|
|
//Debug.Log("Received ChangeValue from RPC from someone");
|
|
ChangeValue(value, dmgType);
|
|
}
|
|
|
|
public override float GetMaxValue()
|
|
{
|
|
return base.GetMaxValue();
|
|
}
|
|
|
|
public virtual void CalculateMaxValueBasedOnStat()
|
|
{
|
|
maxValue = character.MaxHealth.Value;
|
|
|
|
CalculateRegenValueBasedOnStat();
|
|
|
|
onMaxHealthChanged.Invoke(maxValue);
|
|
}
|
|
public void CalculateRegenValueBasedOnStat()
|
|
{
|
|
flatRegen = baseFlatRegen +
|
|
(character.Strength.Value - character.Strength.BaseValue) * GameConstants.CharacterStatsBalancing.BonusStrengthToFlatRegenRate +
|
|
(character.Vitality.Value - character.Vitality.BaseValue) * GameConstants.CharacterStatsBalancing.BonusVitalityToFlatRegenRate
|
|
;
|
|
//percentRegen = (character.Vitality.Value - character.Vitality.BaseValue) * GameConstants.CharacterBalancing.BonusVitalityToPercentRegenRate;
|
|
|
|
//Debug.Log(this.gameObject.name + " regens health " + flatRegen + " " + percentRegen + " = " + flatRegen + (maxValue * percentRegen / 100f));
|
|
}
|
|
|
|
public override void SetMaxValue(float value)
|
|
{
|
|
base.SetMaxValue(value);
|
|
|
|
onMaxHealthChanged.Invoke(maxValue);
|
|
}
|
|
|
|
|
|
}
|