60 lines
2.0 KiB
C#
60 lines
2.0 KiB
C#
using System.Collections.Generic;
|
|
using YachtDice.Scoring;
|
|
|
|
namespace YachtDice.Modifiers
|
|
{
|
|
public delegate void ModifierHandler(ModifierData data, ref ScoreResult result);
|
|
|
|
public static class ModifierEffect
|
|
{
|
|
private static readonly Dictionary<ModifierEffectType, ModifierHandler> Handlers = new()
|
|
{
|
|
{ ModifierEffectType.AddPerDieValue, ApplyAddPerDieValue },
|
|
{ ModifierEffectType.AddFlatToFinalScore, ApplyAddFlat },
|
|
{ ModifierEffectType.MultiplyPerDieValue, ApplyMultiplyPerDieValue },
|
|
{ ModifierEffectType.MultiplyFinalScore, ApplyMultiplyFinal }
|
|
};
|
|
|
|
public static void Apply(ModifierData data, ref ScoreResult result)
|
|
{
|
|
if (Handlers.TryGetValue(data.EffectType, out var handler))
|
|
handler(data, ref result);
|
|
}
|
|
|
|
private static void ApplyAddPerDieValue(ModifierData data, ref ScoreResult result)
|
|
{
|
|
int targetValue = data.Target.DieValue;
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < result.DiceValues.Length; i++)
|
|
{
|
|
if (targetValue == 0 || result.DiceValues[i] == targetValue)
|
|
count++;
|
|
}
|
|
|
|
result.FlatBonus += (int)(data.EffectValue * count);
|
|
}
|
|
|
|
private static void ApplyAddFlat(ModifierData data, ref ScoreResult result)
|
|
{
|
|
result.FlatBonus += (int)data.EffectValue;
|
|
}
|
|
|
|
private static void ApplyMultiplyPerDieValue(ModifierData data, ref ScoreResult result)
|
|
{
|
|
int targetValue = data.Target.DieValue;
|
|
|
|
for (int i = 0; i < result.DiceValues.Length; i++)
|
|
{
|
|
if (targetValue == 0 || result.DiceValues[i] == targetValue)
|
|
result.Multiplier *= data.EffectValue;
|
|
}
|
|
}
|
|
|
|
private static void ApplyMultiplyFinal(ModifierData data, ref ScoreResult result)
|
|
{
|
|
result.Multiplier *= data.EffectValue;
|
|
}
|
|
}
|
|
}
|