Skip to content

Instantly share code, notes, and snippets.

@Kryzarel
Kryzarel / CharacterStat.cs
Last active November 30, 2017 19:26
Base CharacterStat class
using System.Collections.Generic;
public class CharacterStat
{
public float BaseValue;
private readonly List<StatModifier> statModifiers;
public CharacterStat(float baseValue)
{
@Kryzarel
Kryzarel / StatModifier.cs
Last active November 30, 2017 19:27
Base StatModifier class
public class StatModifier
{
public readonly float Value;
public StatModifier(float value)
{
Value = value;
}
}
@Kryzarel
Kryzarel / CharacterStat.cs
Last active November 30, 2017 19:31
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);
@Kryzarel
Kryzarel / CharacterStat.cs
Last active November 30, 2017 22:50
Added isDirty
// 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;
public enum StatModType
{
Flat,
Percent,
}
@Kryzarel
Kryzarel / StatModifier.cs
Created November 30, 2017 20:28
Added Type
public class StatModifier
{
public readonly float Value;
public readonly StatModType Type;
public StatModifier(float value, StatModType type)
{
Value = value;
Type = type;
}
@Kryzarel
Kryzarel / CharacterStat.cs
Created November 30, 2017 20:31
Changed CalculateFinalValue() to deal with Flat and Percent modifiers
private float CalculateFinalValue()
{
float finalValue = BaseValue;
for (int i = 0; i < statModifiers.Count; i++)
{
StatModifier mod = statModifiers[i];
if (mod.Type == StatModType.Flat)
{
@Kryzarel
Kryzarel / StatModifier.cs
Last active November 30, 2017 22:49
Added Order
// 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;
}
@Kryzarel
Kryzarel / CharacterStat.cs
Created November 30, 2017 23:19
Added sorting to statModifiers
// 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)
public enum StatModType
{
Flat,
PercentAdd, // Add this new type.
PercentMult, // Change our old Percent type to this.
}