59 lines
1.6 KiB
C#
59 lines
1.6 KiB
C#
using Kryz.CharacterStats.Examples;
|
|
using UnityEngine;
|
|
|
|
public class BlockInterceptor : BrokerInterceptor
|
|
{
|
|
float percentStatMitigation;
|
|
float finalValue;
|
|
float reducedDamage;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
|
|
broker.OnIncomingDamage.Subscribe(HandleBlockMitigation, GameConstants.BrokerEventPriority.Block);
|
|
}
|
|
|
|
|
|
protected void HandleBlockMitigation(DamageArgs args)
|
|
{
|
|
if (args.currentValue >= 0) return;
|
|
if (args.applicationMethod == EffectApplicationMethod.Tick) return;
|
|
|
|
BlockArgs blockArgs = CObjectPool<BlockArgs>.Get();
|
|
|
|
blockArgs.user = user;
|
|
blockArgs.blockedSuccessfully = false;
|
|
|
|
|
|
if(HasBlocked())
|
|
{
|
|
Debug.Log("Blocked!");
|
|
|
|
percentStatMitigation = MathHelpers.NormalizePercentageDecimal(stats.GetStat("blockeffectiveness").Value);
|
|
|
|
percentStatMitigation = Mathf.Clamp(percentStatMitigation, 0, GameConstants.CharacterStatsBalancing.MaximumPercentDamageReductionFromBlock);
|
|
|
|
reducedDamage = args.currentValue * percentStatMitigation;
|
|
finalValue += Mathf.Abs(reducedDamage);
|
|
|
|
if (finalValue > 0) //avoid damage ultra mitigated turning into healing
|
|
finalValue = 0;
|
|
|
|
blockArgs.blockedSuccessfully = true;
|
|
|
|
args.currentValue = finalValue;
|
|
}
|
|
|
|
broker.OnBlock.Invoke(blockArgs);
|
|
|
|
CObjectPool<BlockArgs>.Release(blockArgs);
|
|
}
|
|
|
|
|
|
protected bool HasBlocked()
|
|
{
|
|
return MathHelpers.RollChancePercent(stats.GetStat("blockchance").Value);
|
|
}
|
|
}
|