I need character/item stats system with ability to modify stats during game.
For instance imagine Bow item with stats like Range, Damage, Reload Speed.
Those attributes can be modified during game with bonuses like: +20% more damage, +5 range.
So far my implementation looked like this:
public class FloatStat
{
protected float baseValue;
protected float flat = 0f;
protected float additive = 1f;
protected float multiplicative = 1f;
protected float value;
// [...]
public void AddFlat(float modifier)
{
flat += modifier;
InvokeOnChanged();
}
public void RemoveFlat(float modifier)
{
flat -= modifier;
InvokeOnChanged();
}
public void AddAdditive(float modifier)
{
additive += modifier;
InvokeOnChanged();
}
public void RemoveAdditive(float modifier)
{
additive -= modifier;
InvokeOnChanged();
}
public void AddMultiplicative(float modifier)
{
multiplicative *= modifier;
InvokeOnChanged();
}
public void RemoveMultiplicative(float modifier)
{
multiplicative /= modifier;
InvokeOnChanged();
}
public float CalculateValue(float v)
{
return (v + flat) * additive * multiplicative;
}
}
My plan is to add 3 dictionaries to keep modifiers with their owners, so they can be recalculated from scratch, plus to make debugging more easy.
However my question is - what is disadvantage of the current system, do you have experience with this kind of implementation?
For sure it is simple and fast, but it requires all modifiers to be removed like events (they must be removed anyway actually?).
So the only potential problem is that after adding and multiplying those numbers could lose precision, but is that actually the case? I think game would need to run quite long.