[Fix] Code visual
This commit is contained in:
@@ -3,91 +3,90 @@ using YachtDice.Scoring;
|
||||
|
||||
namespace YachtDice.Modifiers
|
||||
{
|
||||
|
||||
[CreateAssetMenu(fileName = "NewModifier", menuName = "YachtDice/Modifier Data")]
|
||||
public sealed class ModifierData : ScriptableObject
|
||||
{
|
||||
[SerializeField] private string id;
|
||||
[SerializeField] private string displayName;
|
||||
[SerializeField] [TextArea] private string description;
|
||||
[SerializeField] private ModifierRarity rarity;
|
||||
[SerializeField] private int shopPrice;
|
||||
[SerializeField] private int sellPrice;
|
||||
[SerializeField] private Sprite icon;
|
||||
|
||||
[Header("Effect")]
|
||||
[SerializeField] private ModifierScope scope;
|
||||
[SerializeField] private ModifierEffectType effectType;
|
||||
[SerializeField] private ModifierTarget target;
|
||||
[SerializeField] private float effectValue;
|
||||
|
||||
[Header("Durability")]
|
||||
[SerializeField] private ModifierDurability durability;
|
||||
[SerializeField] private int maxUses;
|
||||
|
||||
public string Id => id;
|
||||
public string DisplayName => displayName;
|
||||
public string Description => description;
|
||||
public ModifierRarity Rarity => rarity;
|
||||
public int ShopPrice => shopPrice;
|
||||
public int SellPrice => sellPrice;
|
||||
public Sprite Icon => icon;
|
||||
public ModifierScope Scope => scope;
|
||||
public ModifierEffectType EffectType => effectType;
|
||||
public ModifierTarget Target => target;
|
||||
public float EffectValue => effectValue;
|
||||
public ModifierDurability Durability => durability;
|
||||
public int MaxUses => maxUses;
|
||||
|
||||
public bool IsAdditive =>
|
||||
effectType == ModifierEffectType.AddPerDieValue ||
|
||||
effectType == ModifierEffectType.AddFlatToFinalScore;
|
||||
|
||||
public bool IsMultiplicative =>
|
||||
effectType == ModifierEffectType.MultiplyPerDieValue ||
|
||||
effectType == ModifierEffectType.MultiplyFinalScore;
|
||||
|
||||
public bool IsCategoryLevel =>
|
||||
effectType == ModifierEffectType.AddPerDieValue ||
|
||||
effectType == ModifierEffectType.MultiplyPerDieValue;
|
||||
|
||||
public bool IsFinalScoreLevel =>
|
||||
effectType == ModifierEffectType.AddFlatToFinalScore ||
|
||||
effectType == ModifierEffectType.MultiplyFinalScore;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static ModifierData CreateForTest(
|
||||
string id,
|
||||
ModifierScope scope,
|
||||
ModifierEffectType effectType,
|
||||
float effectValue,
|
||||
int dieValue = 0,
|
||||
YachtCategory targetCategory = YachtCategory.Ones,
|
||||
bool hasCategoryFilter = false,
|
||||
ModifierDurability durability = ModifierDurability.Permanent,
|
||||
int maxUses = 0,
|
||||
int shopPrice = 100,
|
||||
int sellPrice = 50)
|
||||
[CreateAssetMenu(fileName = "NewModifier", menuName = "YachtDice/Modifier Data")]
|
||||
public sealed class ModifierData : ScriptableObject
|
||||
{
|
||||
var data = CreateInstance<ModifierData>();
|
||||
data.id = id;
|
||||
data.displayName = id;
|
||||
data.description = id;
|
||||
data.scope = scope;
|
||||
data.effectType = effectType;
|
||||
data.effectValue = effectValue;
|
||||
data.target = new ModifierTarget
|
||||
[SerializeField] private string id;
|
||||
[SerializeField] private string displayName;
|
||||
[SerializeField] [TextArea] private string description;
|
||||
[SerializeField] private ModifierRarity rarity;
|
||||
[SerializeField] private int shopPrice;
|
||||
[SerializeField] private int sellPrice;
|
||||
[SerializeField] private Sprite icon;
|
||||
|
||||
[Header("Effect")]
|
||||
[SerializeField] private ModifierScope scope;
|
||||
[SerializeField] private ModifierEffectType effectType;
|
||||
[SerializeField] private ModifierTarget target;
|
||||
[SerializeField] private float effectValue;
|
||||
|
||||
[Header("Durability")]
|
||||
[SerializeField] private ModifierDurability durability;
|
||||
[SerializeField] private int maxUses;
|
||||
|
||||
public string Id => id;
|
||||
public string DisplayName => displayName;
|
||||
public string Description => description;
|
||||
public ModifierRarity Rarity => rarity;
|
||||
public int ShopPrice => shopPrice;
|
||||
public int SellPrice => sellPrice;
|
||||
public Sprite Icon => icon;
|
||||
public ModifierScope Scope => scope;
|
||||
public ModifierEffectType EffectType => effectType;
|
||||
public ModifierTarget Target => target;
|
||||
public float EffectValue => effectValue;
|
||||
public ModifierDurability Durability => durability;
|
||||
public int MaxUses => maxUses;
|
||||
|
||||
public bool IsAdditive =>
|
||||
effectType == ModifierEffectType.AddPerDieValue ||
|
||||
effectType == ModifierEffectType.AddFlatToFinalScore;
|
||||
|
||||
public bool IsMultiplicative =>
|
||||
effectType == ModifierEffectType.MultiplyPerDieValue ||
|
||||
effectType == ModifierEffectType.MultiplyFinalScore;
|
||||
|
||||
public bool IsCategoryLevel =>
|
||||
effectType == ModifierEffectType.AddPerDieValue ||
|
||||
effectType == ModifierEffectType.MultiplyPerDieValue;
|
||||
|
||||
public bool IsFinalScoreLevel =>
|
||||
effectType == ModifierEffectType.AddFlatToFinalScore ||
|
||||
effectType == ModifierEffectType.MultiplyFinalScore;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static ModifierData CreateForTest(
|
||||
string id,
|
||||
ModifierScope scope,
|
||||
ModifierEffectType effectType,
|
||||
float effectValue,
|
||||
int dieValue = 0,
|
||||
YachtCategory targetCategory = YachtCategory.Ones,
|
||||
bool hasCategoryFilter = false,
|
||||
ModifierDurability durability = ModifierDurability.Permanent,
|
||||
int maxUses = 0,
|
||||
int shopPrice = 100,
|
||||
int sellPrice = 50)
|
||||
{
|
||||
DieValue = dieValue,
|
||||
TargetCategory = targetCategory,
|
||||
HasCategoryFilter = hasCategoryFilter
|
||||
};
|
||||
data.durability = durability;
|
||||
data.maxUses = maxUses;
|
||||
data.shopPrice = shopPrice;
|
||||
data.sellPrice = sellPrice;
|
||||
return data;
|
||||
var data = CreateInstance<ModifierData>();
|
||||
data.id = id;
|
||||
data.displayName = id;
|
||||
data.description = id;
|
||||
data.scope = scope;
|
||||
data.effectType = effectType;
|
||||
data.effectValue = effectValue;
|
||||
data.target = new ModifierTarget
|
||||
{
|
||||
DieValue = dieValue,
|
||||
TargetCategory = targetCategory,
|
||||
HasCategoryFilter = hasCategoryFilter
|
||||
};
|
||||
data.durability = durability;
|
||||
data.maxUses = maxUses;
|
||||
data.shopPrice = shopPrice;
|
||||
data.sellPrice = sellPrice;
|
||||
return data;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,58 +3,57 @@ using YachtDice.Scoring;
|
||||
|
||||
namespace YachtDice.Modifiers
|
||||
{
|
||||
public delegate void ModifierHandler(ModifierData data, ref ScoreResult result);
|
||||
|
||||
public delegate void ModifierHandler(ModifierData data, ref ScoreResult result);
|
||||
|
||||
public static class ModifierEffect
|
||||
{
|
||||
private static readonly Dictionary<ModifierEffectType, ModifierHandler> Handlers = new()
|
||||
public static class ModifierEffect
|
||||
{
|
||||
{ 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++)
|
||||
private static readonly Dictionary<ModifierEffectType, ModifierHandler> Handlers = new()
|
||||
{
|
||||
if (targetValue == 0 || result.DiceValues[i] == targetValue)
|
||||
count++;
|
||||
{ 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);
|
||||
}
|
||||
|
||||
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++)
|
||||
private static void ApplyAddPerDieValue(ModifierData data, ref ScoreResult result)
|
||||
{
|
||||
if (targetValue == 0 || result.DiceValues[i] == targetValue)
|
||||
result.Multiplier *= data.EffectValue;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyMultiplyFinal(ModifierData data, ref ScoreResult result)
|
||||
{
|
||||
result.Multiplier *= data.EffectValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
namespace YachtDice.Modifiers
|
||||
{
|
||||
public enum ModifierScope
|
||||
{
|
||||
SelectedCategory,
|
||||
AnyCategoryClosed
|
||||
}
|
||||
public enum ModifierScope
|
||||
{
|
||||
SelectedCategory,
|
||||
AnyCategoryClosed
|
||||
}
|
||||
|
||||
public enum ModifierEffectType
|
||||
{
|
||||
AddPerDieValue,
|
||||
AddFlatToFinalScore,
|
||||
MultiplyPerDieValue,
|
||||
MultiplyFinalScore
|
||||
}
|
||||
public enum ModifierEffectType
|
||||
{
|
||||
AddPerDieValue,
|
||||
AddFlatToFinalScore,
|
||||
MultiplyPerDieValue,
|
||||
MultiplyFinalScore
|
||||
}
|
||||
|
||||
public enum ModifierDurability
|
||||
{
|
||||
Permanent,
|
||||
LimitedUses
|
||||
}
|
||||
public enum ModifierDurability
|
||||
{
|
||||
Permanent,
|
||||
LimitedUses
|
||||
}
|
||||
|
||||
public enum ModifierRarity
|
||||
{
|
||||
Common,
|
||||
Uncommon,
|
||||
Rare,
|
||||
Epic
|
||||
}
|
||||
public enum ModifierRarity
|
||||
{
|
||||
Common,
|
||||
Uncommon,
|
||||
Rare,
|
||||
Epic
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,65 +3,64 @@ using YachtDice.Scoring;
|
||||
|
||||
namespace YachtDice.Modifiers
|
||||
{
|
||||
|
||||
public static class ModifierPipeline
|
||||
{
|
||||
// Application order (explicit):
|
||||
// 1. Category-level additive (AddPerDieValue)
|
||||
// 2. Category-level multiplicative (MultiplyPerDieValue)
|
||||
// 3. Final-score additive (AddFlatToFinalScore)
|
||||
// 4. Final-score multiplicative (MultiplyFinalScore)
|
||||
|
||||
public static void Apply(
|
||||
IReadOnlyList<ModifierData> activeModifiers,
|
||||
ref ScoreResult result,
|
||||
ModifierScope currentScope)
|
||||
public static class ModifierPipeline
|
||||
{
|
||||
if (activeModifiers == null) return;
|
||||
// Application order (explicit):
|
||||
// 1. Category-level additive (AddPerDieValue)
|
||||
// 2. Category-level multiplicative (MultiplyPerDieValue)
|
||||
// 3. Final-score additive (AddFlatToFinalScore)
|
||||
// 4. Final-score multiplicative (MultiplyFinalScore)
|
||||
|
||||
// Pass 1: Category-level additive
|
||||
for (int i = 0; i < activeModifiers.Count; i++)
|
||||
public static void Apply(
|
||||
IReadOnlyList<ModifierData> activeModifiers,
|
||||
ref ScoreResult result,
|
||||
ModifierScope currentScope)
|
||||
{
|
||||
var mod = activeModifiers[i];
|
||||
if (!ShouldApply(mod, ref result, currentScope)) continue;
|
||||
if (mod.IsCategoryLevel && mod.IsAdditive)
|
||||
ModifierEffect.Apply(mod, ref result);
|
||||
if (activeModifiers == null) return;
|
||||
|
||||
// Pass 1: Category-level additive
|
||||
for (int i = 0; i < activeModifiers.Count; i++)
|
||||
{
|
||||
var mod = activeModifiers[i];
|
||||
if (!ShouldApply(mod, ref result, currentScope)) continue;
|
||||
if (mod.IsCategoryLevel && mod.IsAdditive)
|
||||
ModifierEffect.Apply(mod, ref result);
|
||||
}
|
||||
|
||||
// Pass 2: Category-level multiplicative
|
||||
for (int i = 0; i < activeModifiers.Count; i++)
|
||||
{
|
||||
var mod = activeModifiers[i];
|
||||
if (!ShouldApply(mod, ref result, currentScope)) continue;
|
||||
if (mod.IsCategoryLevel && mod.IsMultiplicative)
|
||||
ModifierEffect.Apply(mod, ref result);
|
||||
}
|
||||
|
||||
// Pass 3: Final-score additive
|
||||
for (int i = 0; i < activeModifiers.Count; i++)
|
||||
{
|
||||
var mod = activeModifiers[i];
|
||||
if (!ShouldApply(mod, ref result, currentScope)) continue;
|
||||
if (mod.IsFinalScoreLevel && mod.IsAdditive)
|
||||
ModifierEffect.Apply(mod, ref result);
|
||||
}
|
||||
|
||||
// Pass 4: Final-score multiplicative
|
||||
for (int i = 0; i < activeModifiers.Count; i++)
|
||||
{
|
||||
var mod = activeModifiers[i];
|
||||
if (!ShouldApply(mod, ref result, currentScope)) continue;
|
||||
if (mod.IsFinalScoreLevel && mod.IsMultiplicative)
|
||||
ModifierEffect.Apply(mod, ref result);
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 2: Category-level multiplicative
|
||||
for (int i = 0; i < activeModifiers.Count; i++)
|
||||
private static bool ShouldApply(ModifierData mod, ref ScoreResult result, ModifierScope currentScope)
|
||||
{
|
||||
var mod = activeModifiers[i];
|
||||
if (!ShouldApply(mod, ref result, currentScope)) continue;
|
||||
if (mod.IsCategoryLevel && mod.IsMultiplicative)
|
||||
ModifierEffect.Apply(mod, ref result);
|
||||
}
|
||||
|
||||
// Pass 3: Final-score additive
|
||||
for (int i = 0; i < activeModifiers.Count; i++)
|
||||
{
|
||||
var mod = activeModifiers[i];
|
||||
if (!ShouldApply(mod, ref result, currentScope)) continue;
|
||||
if (mod.IsFinalScoreLevel && mod.IsAdditive)
|
||||
ModifierEffect.Apply(mod, ref result);
|
||||
}
|
||||
|
||||
// Pass 4: Final-score multiplicative
|
||||
for (int i = 0; i < activeModifiers.Count; i++)
|
||||
{
|
||||
var mod = activeModifiers[i];
|
||||
if (!ShouldApply(mod, ref result, currentScope)) continue;
|
||||
if (mod.IsFinalScoreLevel && mod.IsMultiplicative)
|
||||
ModifierEffect.Apply(mod, ref result);
|
||||
if (mod == null) return false;
|
||||
if (mod.Scope != currentScope) return false;
|
||||
if (mod.Target.HasCategoryFilter && mod.Target.TargetCategory != result.Category) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ShouldApply(ModifierData mod, ref ScoreResult result, ModifierScope currentScope)
|
||||
{
|
||||
if (mod == null) return false;
|
||||
if (mod.Scope != currentScope) return false;
|
||||
if (mod.Target.HasCategoryFilter && mod.Target.TargetCategory != result.Category) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,37 +2,36 @@ using System;
|
||||
|
||||
namespace YachtDice.Modifiers
|
||||
{
|
||||
|
||||
[Serializable]
|
||||
public sealed class ModifierRuntime
|
||||
{
|
||||
public string ModifierId;
|
||||
public bool IsActive;
|
||||
public int RemainingUses;
|
||||
|
||||
[NonSerialized] public ModifierData Data;
|
||||
|
||||
public bool IsExpired => Data != null &&
|
||||
Data.Durability == ModifierDurability.LimitedUses &&
|
||||
RemainingUses <= 0;
|
||||
|
||||
public void ConsumeUse()
|
||||
[Serializable]
|
||||
public class ModifierRuntime
|
||||
{
|
||||
if (Data == null) return;
|
||||
if (Data.Durability != ModifierDurability.LimitedUses) return;
|
||||
public string ModifierId;
|
||||
public bool IsActive;
|
||||
public int RemainingUses;
|
||||
|
||||
RemainingUses--;
|
||||
}
|
||||
[NonSerialized] public ModifierData Data;
|
||||
|
||||
public static ModifierRuntime Create(ModifierData data)
|
||||
{
|
||||
return new ModifierRuntime
|
||||
public bool IsExpired => Data != null &&
|
||||
Data.Durability == ModifierDurability.LimitedUses &&
|
||||
RemainingUses <= 0;
|
||||
|
||||
public void ConsumeUse()
|
||||
{
|
||||
ModifierId = data.Id,
|
||||
IsActive = false,
|
||||
RemainingUses = data.Durability == ModifierDurability.LimitedUses ? data.MaxUses : -1,
|
||||
Data = data
|
||||
};
|
||||
if (Data == null) return;
|
||||
if (Data.Durability != ModifierDurability.LimitedUses) return;
|
||||
|
||||
RemainingUses--;
|
||||
}
|
||||
|
||||
public static ModifierRuntime Create(ModifierData data)
|
||||
{
|
||||
return new ModifierRuntime
|
||||
{
|
||||
ModifierId = data.Id,
|
||||
IsActive = false,
|
||||
RemainingUses = data.Durability == ModifierDurability.LimitedUses ? data.MaxUses : -1,
|
||||
Data = data
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,18 +4,17 @@ using YachtDice.Scoring;
|
||||
|
||||
namespace YachtDice.Modifiers
|
||||
{
|
||||
[Serializable]
|
||||
public struct ModifierTarget
|
||||
{
|
||||
[Tooltip("Die face value (1-6). 0 = any/all dice.")]
|
||||
[Range(0, 6)]
|
||||
public int DieValue;
|
||||
|
||||
[Serializable]
|
||||
public struct ModifierTarget
|
||||
{
|
||||
[Tooltip("Die face value (1-6). 0 = any/all dice.")]
|
||||
[Range(0, 6)]
|
||||
public int DieValue;
|
||||
[Tooltip("Category this modifier targets.")]
|
||||
public YachtCategory TargetCategory;
|
||||
|
||||
[Tooltip("Category this modifier targets.")]
|
||||
public YachtCategory TargetCategory;
|
||||
|
||||
[Tooltip("If true, TargetCategory is used as a filter.")]
|
||||
public bool HasCategoryFilter;
|
||||
}
|
||||
[Tooltip("If true, TargetCategory is used as a filter.")]
|
||||
public bool HasCategoryFilter;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user