Skip to content

Instantly share code, notes, and snippets.

@immrsv
Created August 4, 2017 02:02
Show Gist options
  • Save immrsv/e0bd77bb33fe9458f28197d14d0a3030 to your computer and use it in GitHub Desktop.
Save immrsv/e0bd77bb33fe9458f28197d14d0a3030 to your computer and use it in GitHub Desktop.
Indexing compound stat values
#region " Stat collection "
[System.Serializable]
public class Stat {
public float Base;
public float Modifier;
public float Value { get { return Base + Base * Modifier; } }
}
protected Dictionary<StatType, Stat> _Stats = new Dictionary<StatType, Stat>();
public float this[StatType stat, StatValueType type = StatValueType.Value] {
get {
switch (type) {
case StatValueType.Base: { return _Stats[stat].Base; }
case StatValueType.Modifier: { return _Stats[stat].Modifier; }
default: { return _Stats[stat].Value; }
}
}
set {
if (!_Stats.ContainsKey(stat))
_Stats.Add(stat, new Stat());
float old = 0;
switch (type) {
case StatValueType.Base:
old = _Stats[stat].Base;
_Stats[stat].Base = value;
break;
case StatValueType.Modifier:
old = _Stats[stat].Modifier;
_Stats[stat].Modifier = value;
break;
case StatValueType.Value:
throw new System.InvalidOperationException("Cannot set EntityStat (" + stat.ToString() + ") value directly. MUST set Base or Modifier instead.");
}
RaiseStatChanged(stat, type, old);
}
}
public bool Remove(StatType stat) {
return _Stats.Remove(stat);
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment