104 lines
3.7 KiB
C#
104 lines
3.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Invector;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
namespace Beyond
|
|
{
|
|
/// <summary>
|
|
/// Damage Modifier. You can use this with <see cref="vIDamageReceiver.onStartReceiveDamage"/> to modificate the damage result
|
|
/// </summary>
|
|
[System.Serializable]
|
|
public class bDamageModifier
|
|
{
|
|
public enum FilterMethod
|
|
{
|
|
ApplyToAll,
|
|
ApplyToAllInList,
|
|
ApplyToAllOutList
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class DamageModifierEvent : UnityEngine.Events.UnityEvent<bDamageModifier> { }
|
|
public string name = "MyModifier";
|
|
public FilterMethod filterMethod;
|
|
[Tooltip("List of Damage type that this can modify, keep empty if the filter will be applied to all types of damage")] public List<string> damageTypes = new List<string>();
|
|
[Tooltip("Modifier value")] public int value;
|
|
[Tooltip("true: Reduce a percentage of damage value\nfalse: Reduce da damage value directly")] public bool percentage;
|
|
[Tooltip("The Filter will receive all damage and decrease your self resistance")] public bool destructible = true;
|
|
public int resistance = 100;
|
|
public int maxResistance = 100;
|
|
public UnityEngine.UI.Slider.SliderEvent onChangeResistance;
|
|
public DamageModifierEvent onBroken;
|
|
|
|
public bool isBroken => destructible && resistance <= 0;
|
|
/// <summary>
|
|
/// Apply modifier to damage
|
|
/// </summary>
|
|
/// <param name="damage">Damage to modify</param>
|
|
public virtual void ApplyModifier(vDamage damage)
|
|
{
|
|
///Apply modifier conditions
|
|
if (damage.damageValue > 0 && (CanFilterDamage(damage.damageType)) && (!destructible || resistance > 0))
|
|
{
|
|
int modifier = 0;
|
|
if (percentage)
|
|
{
|
|
modifier = (int)((float)damage.damageValue / 100 * value); ///Calculate Percentage of the damage
|
|
}
|
|
else
|
|
{
|
|
modifier = value;/// default value
|
|
}
|
|
///apply damage to resistance
|
|
if (destructible)
|
|
{
|
|
resistance -= damage.damageValue;
|
|
onChangeResistance.Invoke(Mathf.Max((float)resistance, 0));
|
|
if (resistance <= 0) onBroken.Invoke(this);
|
|
}
|
|
///apply modifier to damage value
|
|
if ((!destructible || resistance > 0)) damage.damageValue += modifier;
|
|
}
|
|
}
|
|
|
|
protected virtual bool CanFilterDamage(string damageType)
|
|
{
|
|
switch (filterMethod)
|
|
{
|
|
case FilterMethod.ApplyToAll:
|
|
return true;
|
|
|
|
case FilterMethod.ApplyToAllInList:
|
|
return damageTypes.Contains(damageType);
|
|
case FilterMethod.ApplyToAllOutList:
|
|
return !damageTypes.Contains(damageType);
|
|
}
|
|
return true;
|
|
}
|
|
public virtual void ResetModifier()
|
|
{
|
|
if (destructible)
|
|
{
|
|
resistance = maxResistance;
|
|
onChangeResistance.Invoke(Mathf.Max(resistance, 0));
|
|
}
|
|
}
|
|
|
|
public void AddDamageType(string type)
|
|
{
|
|
if (!damageTypes.Contains(type))
|
|
{
|
|
damageTypes.Add(type);
|
|
}
|
|
}
|
|
|
|
public void RemoveDamageType(string type)
|
|
{
|
|
if (damageTypes.Contains(type))
|
|
{
|
|
damageTypes.Remove(type);
|
|
}
|
|
}
|
|
}
|
|
} |