This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private float CalculateFinalValue() | |
| { | |
| float finalValue = BaseValue; | |
| float sumPercentAdd = 0; // This will hold the sum of our "PercentAdd" modifiers | |
| for (int i = 0; i < statModifiers.Count; i++) | |
| { | |
| StatModifier mod = statModifiers[i]; | |
| if (mod.Type == StatModType.Flat) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public readonly float Value; | |
| public readonly StatModType Type; | |
| public readonly int Order; | |
| public readonly object Source; // Added this variable | |
| // "Main" constructor. Requires all variables. | |
| public StatModifier(float value, StatModType type, int order, object source) // Added "source" input parameter | |
| { | |
| Value = value; | |
| Type = type; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Hypothetical item class | |
| public class Item | |
| { | |
| public void Equip(Character c) | |
| { | |
| // We need to store our modifiers in variables before adding them to the stat. | |
| mod1 = new StatModifier(10, StatModType.Flat); | |
| mod2 = new StatModifier(0.1, StatModType.Percent); | |
| c.Strength.AddModifier(mod1); | |
| c.Strength.AddModifier(mod2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public bool RemoveAllModifiersFromSource(object source) | |
| { | |
| bool didRemove = false; | |
| for (int i = statModifiers.Count - 1; i >= 0; i--) | |
| { | |
| if (statModifiers[i].Source == source) | |
| { | |
| isDirty = true; | |
| didRemove = true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Item | |
| { | |
| public void Equip(Character c) | |
| { | |
| // Create the modifiers and set the Source to "this" | |
| // Note that we don't need to store the modifiers in variables anymore | |
| c.Strength.AddModifier(new StatModifier(10, StatModType.Flat, this)); | |
| c.Strength.AddModifier(new StatModifier(0.1, StatModType.Percent, this)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public bool RemoveModifier(StatModifier mod) | |
| { | |
| if (statModifiers.Remove(mod)) | |
| { | |
| isDirty = true; | |
| return true; | |
| } | |
| return false; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public readonly ReadOnlyCollection<StatModifier> StatModifiers; // Add this variable | |
| public CharacterStat(float baseValue) | |
| { | |
| BaseValue = baseValue; | |
| statModifiers = new List<StatModifier>(); | |
| StatModifiers = statModifiers.AsReadOnly(); // Add this line to the constructor | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Add this variable | |
| private float lastBaseValue = float.MinValue; | |
| // Change Value | |
| public float Value { | |
| get { | |
| if(isDirty || lastBaseValue != BaseValue) { | |
| lastBaseValue = BaseValue; | |
| _value = CalculateFinalValue(); | |
| isDirty = false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public enum StatModType | |
| { | |
| Flat = 100, | |
| PercentAdd = 200, | |
| PercentMult = 300, | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protected bool isDirty = true; | |
| protected float lastBaseValue; | |
| protected float _value; | |
| public virtual float Value {} | |
| protected readonly List<StatModifier> statModifiers; | |
| public virtual void AddModifier(StatModifier mod); | |
| public virtual bool RemoveModifier(StatModifier mod); | |
| public virtual bool RemoveAllModifiersFromSource(object source); |