Skip to content

Instantly share code, notes, and snippets.

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;
@Kryzarel
Kryzarel / Item.cs
Created December 3, 2017 20:40
Example Item for CharacterStat tutorial
// 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);
@Kryzarel
Kryzarel / StatModifier.cs
Last active December 3, 2017 20:27
Updated with Source variable, for tutorial part 2.
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;
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)
public enum StatModType
{
Flat,
PercentAdd, // Add this new type.
PercentMult, // Change our old Percent type to this.
}
@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)
@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 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
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;
}
public enum StatModType
{
Flat,
Percent,
}