I moved to a new URL! Check it out!

Dev Log: Fun with Stats

Dev Log: Fun with Stats
I haven't made much progress on my system to set up triggers and actions for Enchantments, but I did make some progress on my player stats system. I coded up a pretty generic Stat class that allows StatMods to be applied. Check it out!
class Stat {

public float BaseValue;
public string Name;

public bool Capped;
public float Cap;

public List<StatMod> StatMods { get; private set; }

bool needsUpdate = false;
float cachedValue;

public Stat(string name, float basevalue = 0) {
Name = name;
BaseValue = basevalue;
Cap = BaseValue * 2;
StatMods = new List<StatMod>();
cachedValue = basevalue;
}

public Stat(float basevalue) : this("", basevalue) { }

public StatMod AddMod(StatMod mod) {
StatMods.Add(mod);
needsUpdate = true;
return mod;
}

public StatMod RemoveMod(StatMod mod) {
StatMods.Remove(mod);
needsUpdate = true;
return mod;
}

public void ClearMods() {
StatMods.Clear();
cachedValue = BaseValue;
}

public float FinalValue {
get {
if (needsUpdate) {
float v = BaseValue;
var m = 1;
foreach (var s in StatMods) {
v += s.Add;
}
foreach (var s in StatMods) {
v += BaseValue * s.Multiply;
}
cachedValue = v;
needsUpdate = false;
}
if (Capped && cachedValue > Cap) return Cap;
return cachedValue;
}
}

public static implicit operator float(Stat stat) {
return stat.FinalValue;
}


}

class StatMod {
public float Multiply = 1;
public float Add;

public StatMod(float add = 0, float multiply = 0) {
Multiply = multiply;
Add = add;
}
}
I'll probably add this into the Utility folder of Otter at some point. Basically you can make a stat, like new Stat(10), then apply modifiers and get the final modified value back. You can also treat it as a float and it will spit out the modified value as well.

Multiply might not work the way you think at first though. Setting Multiply to 1 in a StatMod actually means to Add 100% of the stat to itself. For example, a stat of 100 with a multiplier mod of 1 will calculate as 200. It's as if the mod is saying "BaseStat +100%" when you set it to 1. So 0.5f would be interpreted as +50%, -0.5f would be -50%, and so on.

Modifiers will always be applied with addition, then multiplication. For example if a stat had a base value of 100, and then you apply a modifier with Add set to 50, and then another modifier with Multiply set to 1, then the final result will be 300. It applies the +50, and then the +100%. It doesn't matter which order they're applied in.

Now I'll get back to work on my dumb trigger and action system that I have no idea how to implement right now, yahoo!
new comment!

Post your comment!

Name
Email
Comment