示例#1
0
 public void AddModifier(FloatModifier mod)
 {
     isDirty = true;
     floatModifiers.Add(mod);
     floatModifiers.Sort(CompareModifierOrder);
     //CalculateFinalValue();
 }
示例#2
0
 public bool RemoveModifier(FloatModifier mod)
 {
     if (floatModifiers.Remove(mod))
     {
         isDirty = true;
         //CalculateFinalValue();
         return(true);
     }
     return(false);
 }
示例#3
0
 private int CompareModifierOrder(FloatModifier a, FloatModifier b)
 {
     if (a.Order < b.Order)
     {
         return(-1);
     }
     if (a.Order > b.Order)
     {
         return(1);
     }
     return(0); // if (a.Order == b.Order)
 }
示例#4
0
        private void CalculateFinalValue()
        {
            lastBaseValue = BaseValue;

            float finalValue    = BaseValue;
            float sumPercentAdd = 0; // This will hold the sum of our "PercentAdd" modifiers

            for (int i = 0; i < floatModifiers.Count; i++)
            {
                FloatModifier mod = floatModifiers[i];

                if (mod.Type == StatModType.Flat)
                {
                    finalValue += mod.Value;
                }
                else if (mod.Type == StatModType.PercentAdd) // When we encounter a "PercentAdd" modifier
                {
                    sumPercentAdd += mod.Value;              // Start adding together all modifiers of this type

                    // If we're at the end of the list OR the next modifer isn't of this type
                    if (i + 1 >= floatModifiers.Count || floatModifiers[i + 1].Type != StatModType.PercentAdd)
                    {
                        finalValue   *= 1 + sumPercentAdd; // Multiply the sum with the "finalValue", like we do for "PercentMult" modifiers
                        sumPercentAdd = 0;                 // Reset the sum back to 0
                    }
                }
                else if (mod.Type == StatModType.PercentMult) // Percent renamed to PercentMult
                {
                    finalValue *= 1 + mod.Value;
                }
            }

            // Rounding gets around dumb float calculation errors (like getting 12.0001f, instead of 12f)
            // 4 significant digits is usually precise enough, but feel free to change this to fit your needs
            value = (float)Math.Round(finalValue, 4);

            isDirty = false;
        }