using System.Collections;
using System.Collections.Generic;
using Invector;
using Sirenix.OdinInspector;
using UnityEngine;
namespace Beyond
{
///
/// Damage Modifier. You can use this with to modificate the damage result
///
[System.Serializable]
public class bDamageModifier
{
public enum FilterMethod
{
ApplyToAll,
ApplyToAllInList,
ApplyToAllOutList
}
[System.Serializable]
public class DamageModifierEvent : UnityEngine.Events.UnityEvent { }
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 damageTypes = new List();
[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;
///
/// Apply modifier to damage
///
/// Damage to modify
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);
}
}
}
}