This file contains 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
using System.Collections.Generic; | |
public class CharacterStat | |
{ | |
public float BaseValue; | |
private readonly List<StatModifier> statModifiers; | |
public CharacterStat(float baseValue) | |
{ |
This file contains 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 StatModifier | |
{ | |
public readonly float Value; | |
public StatModifier(float value) | |
{ | |
Value = value; | |
} | |
} |
This file contains 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 float Value { get { return CalculateFinalValue(); } } | |
public void AddModifier(StatModifier mod) | |
{ | |
statModifiers.Add(mod); | |
} | |
public bool RemoveModifier(StatModifier mod) | |
{ | |
return statModifiers.Remove(mod); |
This file contains 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 these variables | |
private bool isDirty = true; | |
private float _value; | |
// Change the Value property to this | |
public float Value { | |
get { | |
if(isDirty) { | |
_value = CalculateFinalValue(); | |
isDirty = false; |
This file contains 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, | |
Percent, | |
} |
This file contains 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 StatModifier | |
{ | |
public readonly float Value; | |
public readonly StatModType Type; | |
public StatModifier(float value, StatModType type) | |
{ | |
Value = value; | |
Type = type; | |
} |
This file contains 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; | |
for (int i = 0; i < statModifiers.Count; i++) | |
{ | |
StatModifier mod = statModifiers[i]; | |
if (mod.Type == StatModType.Flat) | |
{ |
This file contains 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 to the top of the class | |
public readonly int Order; | |
// Change the existing constructor to look like this | |
public StatModifier(float value, StatModType type, int order) | |
{ | |
Value = value; | |
Type = type; | |
Order = order; | |
} |
This file contains 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
// Change the AddModifiers method to this | |
public void AddModifier(StatModifier mod) | |
{ | |
isDirty = true; | |
statModifiers.Add(mod); | |
statModifiers.Sort(CompareModifierOrder); | |
} | |
// Add this method to the CharacterStat class | |
private int CompareModifierOrder(StatModifier a, StatModifier b) |
This file contains 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, | |
PercentAdd, // Add this new type. | |
PercentMult, // Change our old Percent type to this. | |
} |
OlderNewer