Modifiers
A modifier is a temporary effect on a stat.
Examples
- An armor with a max health bonus applies a modifier on the max health stat. If the armor is removed, the modifier is removed.
- Max health applies a clamp modifier [0, MaxStat.Value] on the health stat.
- A boost spell adds power on the Attack stat for x seconds...
Modifier types
A modifier has a priority and one or more Apply method (for int, float, Vector...).
The basic list:
Add(float)
, just adds a value to a stat, [priotity = 0]AddHigh(float)
, ignores all AddHigh on a stat except the highest value and adds it, [priotity = 2]AddLow(float)
, ignores all AddLow on a stat except the lowest value and adds it, [priotity = 4]Clamp(float, float)
, clamps the stat, [priotity = 100]Mult(float)
, mults the stat, [priotity = 1]MultHigh(float)
, ignores all MultHigh on a stat except the highest and mults stat with it, [priotity = 5]MultList(float)
, stat *= 1f+(all MultList modifier), [priotity = 3]MultLow(float)
, ignores all MultLow on a stat except the lowest and mults stat with it, [priotity = 7]
A modifier store its value in a Vector4. Used values are type dependant.
A Vector clamp is a magnitude clamp
A Color clamp is a greyscale clamp
Use modifier
using EODE.Wonderland.Modifier;
var health = GetComponent<HealthStat>();
Debug.Log(health.Value); // 100f
var mod1 = new MultList(0.5f);
var mod2 = new MultList(0.2f);
health.Add(mod1);
health.Add(mod2);
Debug.Log(health.Value); // 170f
health.Add(mod2);
Debug.Log(health.Value); // 170f, modifier list is a hashset
health.Remove(mod1);
Debug.Log(health.Value); // 120f
Modifier config
Use the field ModifierConfig
to define a modifier in a MonoBehaviour or a ScriptableObject. (for example to define the effect of a spell)
Create your own modifier
Standard modifier
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace EODE.Wonderland.Modifier {
public class MyModifier : StatModifier {
public float Value;
... my value list
public MyModifier(float value, ...) {
Value = value;
}
// draw in inspector
public override string ToString() {
return "+ "+Value;
}
// execution priority, default is 0
public override int Priority => 2;
// list of applies (the effect of the modifier)
public override int Apply(int value) {
return value+Mathf.RoundToInt(Value);
}
public override float Apply(float value) {
return value+Value;
}
}
}
Modifier list
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace EODE.Wonderland.Modifier {
public class MyModifier : StatModifier {
...
// try to call Apply methods
public override bool UseApply => false;
// try to call ApplyAll methods
public override bool UseApplyAll => true;
// like Apply but calls 1 time per type after all Apply with sms = list of MyModifier
public override float ApplyAll(float value, List<StatModifier> sms) {
// example : the AddHigh function
float v = Value;
foreach (var sm in sms) {
var ml = sm as EODE.Wonderland.Modifier.AddHigh;
if (ml != null) {
v = Mathf.Max(ml.Value, v);
}
}
return value+v;
}
public override int ApplyAll(int value, List<StatModifier> sms) {
return Mathf.RoundToInt(ApplyAll((float)value, sms));
}
}
}
Apply/ApplyAll method list
#region Apply
public virtual int Apply(int value) {return value;}
public virtual float Apply(float value) {return value;}
public virtual short Apply(short value) {return value;}
public virtual string Apply(string value) {return value;}
public virtual Vector2 Apply(Vector2 value) {return value;}
public virtual Vector3 Apply(Vector3 value) {return value;}
public virtual Vector4 Apply(Vector4 value) {return value;}
public virtual Color Apply(Color value) {return value;}
#endregion
#region Apply all
// Apply modifiers 1 time
public virtual int ApplyAll(int value, List<StatModifier> sms) {return value;}
public virtual float ApplyAll(float value, List<StatModifier> sms) {return value;}
public virtual short ApplyAll(short value, List<StatModifier> sms) {return value;}
public virtual string ApplyAll(string value, List<StatModifier> sms) {return value;}
public virtual Vector2 ApplyAll(Vector2 value, List<StatModifier> sms) {return value;}
public virtual Vector3 ApplyAll(Vector3 value, List<StatModifier> sms) {return value;}
public virtual Vector4 ApplyAll(Vector4 value, List<StatModifier> sms) {return value;}
public virtual Color ApplyAll(Color value, List<StatModifier> sms) {return value;}
#endregion