Skip to content

Instantly share code, notes, and snippets.

@Kryzarel
Last active November 30, 2017 19:31
Show Gist options
  • Save Kryzarel/6f0446f73066503661cdb52d1fc45636 to your computer and use it in GitHub Desktop.
Save Kryzarel/6f0446f73066503661cdb52d1fc45636 to your computer and use it in GitHub Desktop.
Added: Add(), Remove() and CalculateFinalValue()
public float Value { get { return CalculateFinalValue(); } }
public void AddModifier(StatModifier mod)
{
statModifiers.Add(mod);
}
public bool RemoveModifier(StatModifier mod)
{
return statModifiers.Remove(mod);
}
private float CalculateFinalValue()
{
float finalValue = BaseValue;
for (int i = 0; i < statModifiers.Count; i++)
{
finalValue += statModifiers[i].Value;
}
// Rounding gets around float calculation errors (like getting 12.0001f, instead of 12f)
// 4 digits is usually precise enough, but feel free to change this to fit your needs
return (float)Math.Round(finalValue, 4);
}
// We also need to add this outside the class curly braces
// to be able to use the Math.Round method
using System;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment