[Refactor] Replace enum-driven modifier system with data-driven SO composition
Replace the entire static, enum-based modifier pipeline with a composition-based, data-driven architecture using ScriptableObject polymorphism. New modifiers can now be created by assembling SO building blocks (Conditions + Effects + Behaviors) — no core code edits needed. New architecture: - Core/: TriggerType, ModifierPhase, ModifierContext, ICondition, IEffect - Definition/: ModifierDefinitionSO, ModifierBehaviorSO, ConditionSO, EffectSO, ModifierCatalogSO - Conditions/: DieValueCondition, CategoryCondition, MinScoreCondition, DiceCountCondition - Effects/: AddFlat, AddPerDie, Multiply, MultiplyPerDie, PostMultiply, AddCurrency, ConsumeCharge - Runtime/: ModifierInstance, ModifierRegistry (non-static service) - Pipeline/: async ModifierPipeline with phase ordering, tracing, anti-recursion - Editor/: ModifierDefinitionValidator with menu items - Events/: GameEventBus (non-static typed dispatcher) - DI/: GameLifetimeScope (VContainer composition root) Deleted old system: ModifierData, ModifierEffect, ModifierEnums, ModifierPipeline (static), ModifierRuntime, ModifierTarget, ShopCatalog, ModifierAssetCreator. Updated: ScoringSystem (VContainer + async), InventoryModel (delegates to ModifierRegistry), ShopModel (uses ModifierDefinitionSO), GameController (VContainer injection), SaveData (uses Runtime.ModifierSaveEntry), all views/controllers, and all test files. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
using YachtDice.Scoring;
|
||||
|
||||
namespace YachtDice.Modifiers.Conditions
|
||||
{
|
||||
[CreateAssetMenu(fileName = "CategoryCondition", menuName = "YachtDice/Modifiers/Conditions/Category")]
|
||||
public class CategoryCondition : ConditionSO
|
||||
{
|
||||
[SerializeField] private YachtCategory requiredCategory;
|
||||
|
||||
public override bool Evaluate(ModifierContext context, ModifierInstance instance)
|
||||
{
|
||||
return context.Category == requiredCategory;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static CategoryCondition CreateForTest(YachtCategory category)
|
||||
{
|
||||
var so = CreateInstance<CategoryCondition>();
|
||||
so.requiredCategory = category;
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Conditions
|
||||
{
|
||||
[CreateAssetMenu(fileName = "DiceCountCondition", menuName = "YachtDice/Modifiers/Conditions/Dice Count")]
|
||||
public class DiceCountCondition : ConditionSO
|
||||
{
|
||||
[Tooltip("Die face value to count (1-6). 0 = any value.")]
|
||||
[SerializeField, Range(0, 6)] private int targetValue;
|
||||
|
||||
[Tooltip("Minimum number of dice that must match.")]
|
||||
[SerializeField] private int minCount = 1;
|
||||
|
||||
public override bool Evaluate(ModifierContext context, ModifierInstance instance)
|
||||
{
|
||||
if (context.DiceValues == null) return false;
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < context.DiceValues.Length; i++)
|
||||
{
|
||||
if (targetValue == 0 || context.DiceValues[i] == targetValue)
|
||||
count++;
|
||||
}
|
||||
return count >= minCount;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static DiceCountCondition CreateForTest(int targetValue, int minCount)
|
||||
{
|
||||
var so = CreateInstance<DiceCountCondition>();
|
||||
so.targetValue = targetValue;
|
||||
so.minCount = minCount;
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Conditions
|
||||
{
|
||||
[CreateAssetMenu(fileName = "DieValueCondition", menuName = "YachtDice/Modifiers/Conditions/Die Value")]
|
||||
public class DieValueCondition : ConditionSO
|
||||
{
|
||||
[SerializeField, Range(1, 6)] private int targetValue = 1;
|
||||
[SerializeField] private int minCount = 1;
|
||||
|
||||
public override bool Evaluate(ModifierContext context, ModifierInstance instance)
|
||||
{
|
||||
if (context.DiceValues == null) return false;
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < context.DiceValues.Length; i++)
|
||||
{
|
||||
if (context.DiceValues[i] == targetValue)
|
||||
count++;
|
||||
}
|
||||
return count >= minCount;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static DieValueCondition CreateForTest(int targetValue, int minCount = 1)
|
||||
{
|
||||
var so = CreateInstance<DieValueCondition>();
|
||||
so.targetValue = targetValue;
|
||||
so.minCount = minCount;
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Conditions
|
||||
{
|
||||
[CreateAssetMenu(fileName = "MinScoreCondition", menuName = "YachtDice/Modifiers/Conditions/Min Score")]
|
||||
public class MinScoreCondition : ConditionSO
|
||||
{
|
||||
[SerializeField] private int minimumBaseScore;
|
||||
|
||||
public override bool Evaluate(ModifierContext context, ModifierInstance instance)
|
||||
{
|
||||
return context.BaseScore >= minimumBaseScore;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static MinScoreCondition CreateForTest(int minScore)
|
||||
{
|
||||
var so = CreateInstance<MinScoreCondition>();
|
||||
so.minimumBaseScore = minScore;
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Core
|
||||
{
|
||||
public interface ICondition
|
||||
{
|
||||
bool Evaluate(ModifierContext context, ModifierInstance instance);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Core
|
||||
{
|
||||
public interface IEffect
|
||||
{
|
||||
ModifierPhase Phase { get; }
|
||||
int Priority { get; }
|
||||
UniTask Apply(ModifierContext context, ModifierInstance instance);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
using YachtDice.Scoring;
|
||||
|
||||
namespace YachtDice.Modifiers.Core
|
||||
{
|
||||
public class ModifierContext
|
||||
{
|
||||
// Scoring data
|
||||
public int BaseScore;
|
||||
public int FlatBonus;
|
||||
public float Multiplier = 1f;
|
||||
public float PostMultiplier = 1f;
|
||||
public int[] DiceValues;
|
||||
public YachtCategory Category;
|
||||
|
||||
// Game state (read-only snapshot)
|
||||
public int CurrentRoll;
|
||||
public int CurrentTurn;
|
||||
public int PlayerCurrency;
|
||||
public IReadOnlyList<ModifierInstance> AllActiveModifiers;
|
||||
|
||||
// Trigger info
|
||||
public TriggerType Trigger;
|
||||
|
||||
// Side-effect accumulators
|
||||
public int CurrencyDelta;
|
||||
|
||||
// Debug trace (populated when tracing is enabled)
|
||||
public List<string> DebugLog;
|
||||
|
||||
public int FinalScore => Mathf.FloorToInt((BaseScore + FlatBonus) * Multiplier * PostMultiplier);
|
||||
|
||||
public ScoreResult ToScoreResult()
|
||||
{
|
||||
return new ScoreResult
|
||||
{
|
||||
BaseScore = BaseScore,
|
||||
FlatBonus = FlatBonus,
|
||||
Multiplier = Multiplier * PostMultiplier,
|
||||
DiceValues = DiceValues,
|
||||
Category = Category,
|
||||
};
|
||||
}
|
||||
|
||||
public static ModifierContext CreateForScoring(
|
||||
int baseScore,
|
||||
int[] diceValues,
|
||||
YachtCategory category,
|
||||
int currentRoll,
|
||||
int currentTurn,
|
||||
int playerCurrency,
|
||||
IReadOnlyList<ModifierInstance> activeModifiers)
|
||||
{
|
||||
return new ModifierContext
|
||||
{
|
||||
BaseScore = baseScore,
|
||||
DiceValues = diceValues,
|
||||
Category = category,
|
||||
CurrentRoll = currentRoll,
|
||||
CurrentTurn = currentTurn,
|
||||
PlayerCurrency = playerCurrency,
|
||||
AllActiveModifiers = activeModifiers,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace YachtDice.Modifiers.Core
|
||||
{
|
||||
public enum ModifierPhase
|
||||
{
|
||||
Pre = 0,
|
||||
Additive = 100,
|
||||
Multiplicative = 200,
|
||||
PostMultiplicative = 300,
|
||||
Final = 400,
|
||||
SideEffect = 500,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace YachtDice.Modifiers.Core
|
||||
{
|
||||
public enum ModifierRarity
|
||||
{
|
||||
Common,
|
||||
Uncommon,
|
||||
Rare,
|
||||
Epic,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace YachtDice.Modifiers.Core
|
||||
{
|
||||
public enum TriggerType
|
||||
{
|
||||
OnCategoryScored,
|
||||
OnTurnStart,
|
||||
OnTurnEnd,
|
||||
OnRollComplete,
|
||||
OnDiceLocked,
|
||||
OnShopOpened,
|
||||
OnItemPurchased,
|
||||
OnGameStart,
|
||||
OnGameEnd,
|
||||
OnModifierActivated,
|
||||
OnModifierDeactivated,
|
||||
OnCurrencyChanged,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Definition
|
||||
{
|
||||
public abstract class ConditionSO : ScriptableObject, ICondition
|
||||
{
|
||||
public abstract bool Evaluate(ModifierContext context, ModifierInstance instance);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Definition
|
||||
{
|
||||
public abstract class EffectSO : ScriptableObject, IEffect
|
||||
{
|
||||
[SerializeField] private ModifierPhase phase = ModifierPhase.Additive;
|
||||
[SerializeField] private int priority;
|
||||
|
||||
public ModifierPhase Phase => phase;
|
||||
public int Priority => priority;
|
||||
|
||||
public abstract UniTask Apply(ModifierContext context, ModifierInstance instance);
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public void SetPhaseForTest(ModifierPhase p) => phase = p;
|
||||
public void SetPriorityForTest(int p) => priority = p;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Definition
|
||||
{
|
||||
[CreateAssetMenu(fileName = "NewBehavior", menuName = "YachtDice/Modifiers/Behavior")]
|
||||
public class ModifierBehaviorSO : ScriptableObject
|
||||
{
|
||||
[SerializeField] private TriggerType trigger;
|
||||
[SerializeField] private List<ConditionSO> conditions = new();
|
||||
[SerializeField] private List<EffectSO> effects = new();
|
||||
|
||||
public TriggerType Trigger => trigger;
|
||||
public IReadOnlyList<ConditionSO> Conditions => conditions;
|
||||
public IReadOnlyList<EffectSO> Effects => effects;
|
||||
|
||||
public bool EvaluateConditions(ModifierContext context, ModifierInstance instance)
|
||||
{
|
||||
for (int i = 0; i < conditions.Count; i++)
|
||||
{
|
||||
if (conditions[i] != null && !conditions[i].Evaluate(context, instance))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static ModifierBehaviorSO CreateForTest(
|
||||
TriggerType trigger,
|
||||
List<ConditionSO> conditions,
|
||||
List<EffectSO> effects)
|
||||
{
|
||||
var so = CreateInstance<ModifierBehaviorSO>();
|
||||
so.trigger = trigger;
|
||||
so.conditions = conditions ?? new List<ConditionSO>();
|
||||
so.effects = effects ?? new List<EffectSO>();
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YachtDice.Modifiers.Definition
|
||||
{
|
||||
[CreateAssetMenu(fileName = "ModifierCatalog", menuName = "YachtDice/Modifiers/Catalog")]
|
||||
public class ModifierCatalogSO : ScriptableObject
|
||||
{
|
||||
[SerializeField] private List<ModifierDefinitionSO> modifiers = new();
|
||||
|
||||
public IReadOnlyList<ModifierDefinitionSO> All => modifiers;
|
||||
|
||||
public ModifierDefinitionSO FindById(string id)
|
||||
{
|
||||
for (int i = 0; i < modifiers.Count; i++)
|
||||
{
|
||||
if (modifiers[i] != null && modifiers[i].Id == id)
|
||||
return modifiers[i];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
|
||||
namespace YachtDice.Modifiers.Definition
|
||||
{
|
||||
[CreateAssetMenu(fileName = "NewModifier", menuName = "YachtDice/Modifiers/Definition")]
|
||||
public class ModifierDefinitionSO : ScriptableObject
|
||||
{
|
||||
[Header("Identity")]
|
||||
[SerializeField] private string id;
|
||||
[SerializeField] private string displayName;
|
||||
[SerializeField, TextArea] private string description;
|
||||
[SerializeField] private Sprite icon;
|
||||
[SerializeField] private ModifierRarity rarity;
|
||||
|
||||
[Header("Economy")]
|
||||
[SerializeField] private int shopPrice;
|
||||
[SerializeField] private int sellPrice;
|
||||
|
||||
[Header("Durability")]
|
||||
[SerializeField] private bool hasLimitedUses;
|
||||
[SerializeField] private int maxUses;
|
||||
[SerializeField] private int maxStacks = 1;
|
||||
|
||||
[Header("Behaviors")]
|
||||
[SerializeField] private List<ModifierBehaviorSO> behaviors = new();
|
||||
|
||||
public string Id => id;
|
||||
public string DisplayName => displayName;
|
||||
public string Description => description;
|
||||
public Sprite Icon => icon;
|
||||
public ModifierRarity Rarity => rarity;
|
||||
public int ShopPrice => shopPrice;
|
||||
public int SellPrice => sellPrice;
|
||||
public bool HasLimitedUses => hasLimitedUses;
|
||||
public int MaxUses => maxUses;
|
||||
public int MaxStacks => maxStacks;
|
||||
public IReadOnlyList<ModifierBehaviorSO> Behaviors => behaviors;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static ModifierDefinitionSO CreateForTest(
|
||||
string id,
|
||||
List<ModifierBehaviorSO> behaviors,
|
||||
bool hasLimitedUses = false,
|
||||
int maxUses = 0,
|
||||
int shopPrice = 100,
|
||||
int sellPrice = 50,
|
||||
ModifierRarity rarity = ModifierRarity.Common)
|
||||
{
|
||||
var so = CreateInstance<ModifierDefinitionSO>();
|
||||
so.id = id;
|
||||
so.displayName = id;
|
||||
so.description = id;
|
||||
so.rarity = rarity;
|
||||
so.shopPrice = shopPrice;
|
||||
so.sellPrice = sellPrice;
|
||||
so.hasLimitedUses = hasLimitedUses;
|
||||
so.maxUses = maxUses;
|
||||
so.behaviors = behaviors ?? new List<ModifierBehaviorSO>();
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
#if UNITY_EDITOR
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
|
||||
namespace YachtDice.Modifiers.Editor
|
||||
{
|
||||
public static class ModifierDefinitionValidator
|
||||
{
|
||||
[MenuItem("YachtDice/Validate All Modifier Definitions")]
|
||||
public static void ValidateAll()
|
||||
{
|
||||
string[] guids = AssetDatabase.FindAssets("t:ModifierDefinitionSO");
|
||||
|
||||
if (guids.Length == 0)
|
||||
{
|
||||
Debug.Log("[ModifierValidator] No ModifierDefinitionSO assets found.");
|
||||
return;
|
||||
}
|
||||
|
||||
int errorCount = 0;
|
||||
int warnCount = 0;
|
||||
var usedIds = new Dictionary<string, string>(); // id -> asset path
|
||||
|
||||
for (int i = 0; i < guids.Length; i++)
|
||||
{
|
||||
string path = AssetDatabase.GUIDToAssetPath(guids[i]);
|
||||
var def = AssetDatabase.LoadAssetAtPath<ModifierDefinitionSO>(path);
|
||||
|
||||
if (def == null)
|
||||
{
|
||||
Debug.LogError($"[ModifierValidator] Failed to load asset at {path}");
|
||||
errorCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// ── Id checks ────────────────────────────────────────
|
||||
|
||||
if (string.IsNullOrWhiteSpace(def.Id))
|
||||
{
|
||||
Debug.LogError($"[ModifierValidator] {path}: Id is empty.", def);
|
||||
errorCount++;
|
||||
}
|
||||
else if (usedIds.TryGetValue(def.Id, out string existingPath))
|
||||
{
|
||||
Debug.LogError($"[ModifierValidator] {path}: Duplicate Id '{def.Id}' (also used by {existingPath}).", def);
|
||||
errorCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
usedIds[def.Id] = path;
|
||||
}
|
||||
|
||||
// ── Economy checks ───────────────────────────────────
|
||||
|
||||
if (def.ShopPrice < 0)
|
||||
{
|
||||
Debug.LogError($"[ModifierValidator] {path}: ShopPrice is negative ({def.ShopPrice}).", def);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
if (def.SellPrice < 0)
|
||||
{
|
||||
Debug.LogError($"[ModifierValidator] {path}: SellPrice is negative ({def.SellPrice}).", def);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
if (def.SellPrice > def.ShopPrice && def.ShopPrice > 0)
|
||||
{
|
||||
Debug.LogWarning($"[ModifierValidator] {path}: SellPrice ({def.SellPrice}) > ShopPrice ({def.ShopPrice}). Infinite money exploit?", def);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
// ── Durability checks ────────────────────────────────
|
||||
|
||||
if (def.HasLimitedUses && def.MaxUses <= 0)
|
||||
{
|
||||
Debug.LogError($"[ModifierValidator] {path}: HasLimitedUses is true but MaxUses is {def.MaxUses}.", def);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
if (!def.HasLimitedUses && def.MaxUses > 0)
|
||||
{
|
||||
Debug.LogWarning($"[ModifierValidator] {path}: HasLimitedUses is false but MaxUses is {def.MaxUses}. Ignored at runtime.", def);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
if (def.MaxStacks < 1)
|
||||
{
|
||||
Debug.LogError($"[ModifierValidator] {path}: MaxStacks must be >= 1 (is {def.MaxStacks}).", def);
|
||||
errorCount++;
|
||||
}
|
||||
|
||||
// ── Behavior checks ──────────────────────────────────
|
||||
|
||||
if (def.Behaviors == null || def.Behaviors.Count == 0)
|
||||
{
|
||||
Debug.LogWarning($"[ModifierValidator] {path}: No behaviors assigned. Modifier will do nothing.", def);
|
||||
warnCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int b = 0; b < def.Behaviors.Count; b++)
|
||||
{
|
||||
var behavior = def.Behaviors[b];
|
||||
|
||||
if (behavior == null)
|
||||
{
|
||||
Debug.LogError($"[ModifierValidator] {path}: Behavior slot [{b}] is null.", def);
|
||||
errorCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for null conditions
|
||||
if (behavior.Conditions != null)
|
||||
{
|
||||
for (int c = 0; c < behavior.Conditions.Count; c++)
|
||||
{
|
||||
if (behavior.Conditions[c] == null)
|
||||
{
|
||||
Debug.LogWarning($"[ModifierValidator] {path}: Behavior '{behavior.name}' has null condition at slot [{c}].", behavior);
|
||||
warnCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for null or empty effects
|
||||
if (behavior.Effects == null || behavior.Effects.Count == 0)
|
||||
{
|
||||
Debug.LogWarning($"[ModifierValidator] {path}: Behavior '{behavior.name}' has no effects.", behavior);
|
||||
warnCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int e = 0; e < behavior.Effects.Count; e++)
|
||||
{
|
||||
if (behavior.Effects[e] == null)
|
||||
{
|
||||
Debug.LogError($"[ModifierValidator] {path}: Behavior '{behavior.name}' has null effect at slot [{e}].", behavior);
|
||||
errorCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Display checks ───────────────────────────────────
|
||||
|
||||
if (string.IsNullOrWhiteSpace(def.DisplayName))
|
||||
{
|
||||
Debug.LogWarning($"[ModifierValidator] {path}: DisplayName is empty.", def);
|
||||
warnCount++;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(def.Description))
|
||||
{
|
||||
Debug.LogWarning($"[ModifierValidator] {path}: Description is empty.", def);
|
||||
warnCount++;
|
||||
}
|
||||
}
|
||||
|
||||
string summary = $"[ModifierValidator] Validated {guids.Length} modifier(s): {errorCount} error(s), {warnCount} warning(s).";
|
||||
|
||||
if (errorCount > 0)
|
||||
Debug.LogError(summary);
|
||||
else if (warnCount > 0)
|
||||
Debug.LogWarning(summary);
|
||||
else
|
||||
Debug.Log(summary);
|
||||
}
|
||||
|
||||
[MenuItem("YachtDice/Validate Modifier Catalog")]
|
||||
public static void ValidateCatalog()
|
||||
{
|
||||
string[] guids = AssetDatabase.FindAssets("t:ModifierCatalogSO");
|
||||
|
||||
if (guids.Length == 0)
|
||||
{
|
||||
Debug.LogWarning("[ModifierValidator] No ModifierCatalogSO asset found. Create one via Create > YachtDice/Modifiers/Catalog.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < guids.Length; i++)
|
||||
{
|
||||
string path = AssetDatabase.GUIDToAssetPath(guids[i]);
|
||||
var catalog = AssetDatabase.LoadAssetAtPath<ModifierCatalogSO>(path);
|
||||
|
||||
if (catalog == null)
|
||||
{
|
||||
Debug.LogError($"[ModifierValidator] Failed to load catalog at {path}");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (catalog.All == null || catalog.All.Count == 0)
|
||||
{
|
||||
Debug.LogWarning($"[ModifierValidator] Catalog at {path} is empty.");
|
||||
continue;
|
||||
}
|
||||
|
||||
int nullCount = 0;
|
||||
var catalogIds = new HashSet<string>();
|
||||
|
||||
for (int j = 0; j < catalog.All.Count; j++)
|
||||
{
|
||||
if (catalog.All[j] == null)
|
||||
{
|
||||
nullCount++;
|
||||
Debug.LogError($"[ModifierValidator] Catalog {path}: Null entry at index [{j}].", catalog);
|
||||
continue;
|
||||
}
|
||||
|
||||
string id = catalog.All[j].Id;
|
||||
if (!catalogIds.Add(id))
|
||||
{
|
||||
Debug.LogError($"[ModifierValidator] Catalog {path}: Duplicate modifier Id '{id}' in catalog.", catalog);
|
||||
}
|
||||
}
|
||||
|
||||
Debug.Log($"[ModifierValidator] Catalog {path}: {catalog.All.Count} entries, {nullCount} null.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,32 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Effects
|
||||
{
|
||||
[CreateAssetMenu(fileName = "AddCurrencyEffect", menuName = "YachtDice/Modifiers/Effects/Add Currency")]
|
||||
public class AddCurrencyEffect : EffectSO
|
||||
{
|
||||
[SerializeField] private int amount;
|
||||
|
||||
public override UniTask Apply(ModifierContext context, ModifierInstance instance)
|
||||
{
|
||||
context.CurrencyDelta += amount * instance.Stacks;
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static AddCurrencyEffect CreateForTest(int amount,
|
||||
ModifierPhase phase = ModifierPhase.SideEffect, int priority = 0)
|
||||
{
|
||||
var so = CreateInstance<AddCurrencyEffect>();
|
||||
so.amount = amount;
|
||||
so.SetPhaseForTest(phase);
|
||||
so.SetPriorityForTest(priority);
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Effects
|
||||
{
|
||||
[CreateAssetMenu(fileName = "AddFlatScoreEffect", menuName = "YachtDice/Modifiers/Effects/Add Flat Score")]
|
||||
public class AddFlatScoreEffect : EffectSO
|
||||
{
|
||||
[SerializeField] private int value;
|
||||
|
||||
public override UniTask Apply(ModifierContext context, ModifierInstance instance)
|
||||
{
|
||||
context.FlatBonus += value * instance.Stacks;
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static AddFlatScoreEffect CreateForTest(int value, ModifierPhase phase = ModifierPhase.Additive, int priority = 0)
|
||||
{
|
||||
var so = CreateInstance<AddFlatScoreEffect>();
|
||||
so.value = value;
|
||||
so.SetPhaseForTest(phase);
|
||||
so.SetPriorityForTest(priority);
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Effects
|
||||
{
|
||||
[CreateAssetMenu(fileName = "AddPerDieEffect", menuName = "YachtDice/Modifiers/Effects/Add Per Die")]
|
||||
public class AddPerDieEffect : EffectSO
|
||||
{
|
||||
[Tooltip("Points to add per matching die.")]
|
||||
[SerializeField] private int valuePerDie;
|
||||
|
||||
[Tooltip("Die face value to match (1-6). 0 = any/all dice.")]
|
||||
[SerializeField, Range(0, 6)] private int targetDieValue;
|
||||
|
||||
public override UniTask Apply(ModifierContext context, ModifierInstance instance)
|
||||
{
|
||||
if (context.DiceValues == null) return UniTask.CompletedTask;
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < context.DiceValues.Length; i++)
|
||||
{
|
||||
if (targetDieValue == 0 || context.DiceValues[i] == targetDieValue)
|
||||
count++;
|
||||
}
|
||||
|
||||
context.FlatBonus += valuePerDie * count * instance.Stacks;
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static AddPerDieEffect CreateForTest(int valuePerDie, int targetDieValue = 0,
|
||||
ModifierPhase phase = ModifierPhase.Additive, int priority = 0)
|
||||
{
|
||||
var so = CreateInstance<AddPerDieEffect>();
|
||||
so.valuePerDie = valuePerDie;
|
||||
so.targetDieValue = targetDieValue;
|
||||
so.SetPhaseForTest(phase);
|
||||
so.SetPriorityForTest(priority);
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Effects
|
||||
{
|
||||
[CreateAssetMenu(fileName = "ConsumeChargeEffect", menuName = "YachtDice/Modifiers/Effects/Consume Charge")]
|
||||
public class ConsumeChargeEffect : EffectSO
|
||||
{
|
||||
[SerializeField] private int charges = 1;
|
||||
|
||||
public override UniTask Apply(ModifierContext context, ModifierInstance instance)
|
||||
{
|
||||
instance.ConsumeCharge(charges);
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static ConsumeChargeEffect CreateForTest(int charges = 1,
|
||||
ModifierPhase phase = ModifierPhase.SideEffect, int priority = 100)
|
||||
{
|
||||
var so = CreateInstance<ConsumeChargeEffect>();
|
||||
so.charges = charges;
|
||||
so.SetPhaseForTest(phase);
|
||||
so.SetPriorityForTest(priority);
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Effects
|
||||
{
|
||||
[CreateAssetMenu(fileName = "MultiplyPerDieEffect", menuName = "YachtDice/Modifiers/Effects/Multiply Per Die")]
|
||||
public class MultiplyPerDieEffect : EffectSO
|
||||
{
|
||||
[Tooltip("Multiplier to apply per matching die.")]
|
||||
[SerializeField] private float multiplierPerDie = 1f;
|
||||
|
||||
[Tooltip("Die face value to match (1-6). 0 = any/all dice.")]
|
||||
[SerializeField, Range(0, 6)] private int targetDieValue;
|
||||
|
||||
public override UniTask Apply(ModifierContext context, ModifierInstance instance)
|
||||
{
|
||||
if (context.DiceValues == null) return UniTask.CompletedTask;
|
||||
|
||||
for (int i = 0; i < context.DiceValues.Length; i++)
|
||||
{
|
||||
if (targetDieValue == 0 || context.DiceValues[i] == targetDieValue)
|
||||
context.Multiplier *= multiplierPerDie;
|
||||
}
|
||||
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static MultiplyPerDieEffect CreateForTest(float multiplierPerDie, int targetDieValue = 0,
|
||||
ModifierPhase phase = ModifierPhase.Multiplicative, int priority = 0)
|
||||
{
|
||||
var so = CreateInstance<MultiplyPerDieEffect>();
|
||||
so.multiplierPerDie = multiplierPerDie;
|
||||
so.targetDieValue = targetDieValue;
|
||||
so.SetPhaseForTest(phase);
|
||||
so.SetPriorityForTest(priority);
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Effects
|
||||
{
|
||||
[CreateAssetMenu(fileName = "MultiplyScoreEffect", menuName = "YachtDice/Modifiers/Effects/Multiply Score")]
|
||||
public class MultiplyScoreEffect : EffectSO
|
||||
{
|
||||
[SerializeField] private float multiplier = 1f;
|
||||
|
||||
public override UniTask Apply(ModifierContext context, ModifierInstance instance)
|
||||
{
|
||||
context.Multiplier *= Mathf.Pow(multiplier, instance.Stacks);
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static MultiplyScoreEffect CreateForTest(float multiplier,
|
||||
ModifierPhase phase = ModifierPhase.Multiplicative, int priority = 0)
|
||||
{
|
||||
var so = CreateInstance<MultiplyScoreEffect>();
|
||||
so.multiplier = multiplier;
|
||||
so.SetPhaseForTest(phase);
|
||||
so.SetPriorityForTest(priority);
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Effects
|
||||
{
|
||||
[CreateAssetMenu(fileName = "PostMultiplyEffect", menuName = "YachtDice/Modifiers/Effects/Post Multiply")]
|
||||
public class PostMultiplyEffect : EffectSO
|
||||
{
|
||||
[SerializeField] private float postMultiplier = 1f;
|
||||
|
||||
public override UniTask Apply(ModifierContext context, ModifierInstance instance)
|
||||
{
|
||||
context.PostMultiplier *= Mathf.Pow(postMultiplier, instance.Stacks);
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public static PostMultiplyEffect CreateForTest(float postMultiplier,
|
||||
ModifierPhase phase = ModifierPhase.PostMultiplicative, int priority = 0)
|
||||
{
|
||||
var so = CreateInstance<PostMultiplyEffect>();
|
||||
so.postMultiplier = postMultiplier;
|
||||
so.SetPhaseForTest(phase);
|
||||
so.SetPriorityForTest(priority);
|
||||
return so;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
using UnityEngine;
|
||||
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)
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
namespace YachtDice.Modifiers
|
||||
{
|
||||
public enum ModifierScope
|
||||
{
|
||||
SelectedCategory,
|
||||
AnyCategoryClosed
|
||||
}
|
||||
|
||||
public enum ModifierEffectType
|
||||
{
|
||||
AddPerDieValue,
|
||||
AddFlatToFinalScore,
|
||||
MultiplyPerDieValue,
|
||||
MultiplyFinalScore
|
||||
}
|
||||
|
||||
public enum ModifierDurability
|
||||
{
|
||||
Permanent,
|
||||
LimitedUses
|
||||
}
|
||||
|
||||
public enum ModifierRarity
|
||||
{
|
||||
Common,
|
||||
Uncommon,
|
||||
Rare,
|
||||
Epic
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace YachtDice.Modifiers
|
||||
{
|
||||
[Serializable]
|
||||
public 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()
|
||||
{
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
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;
|
||||
|
||||
[Tooltip("Category this modifier targets.")]
|
||||
public YachtCategory TargetCategory;
|
||||
|
||||
[Tooltip("If true, TargetCategory is used as a filter.")]
|
||||
public bool HasCategoryFilter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Pipeline
|
||||
{
|
||||
public struct EffectEntry
|
||||
{
|
||||
public EffectSO Effect;
|
||||
public ModifierInstance Instance;
|
||||
}
|
||||
|
||||
public class ModifierComparer : IComparer<EffectEntry>
|
||||
{
|
||||
public static readonly ModifierComparer Default = new();
|
||||
|
||||
public int Compare(EffectEntry a, EffectEntry b)
|
||||
{
|
||||
int cmp = a.Effect.Phase.CompareTo(b.Effect.Phase);
|
||||
if (cmp != 0) return cmp;
|
||||
|
||||
cmp = a.Effect.Priority.CompareTo(b.Effect.Priority);
|
||||
if (cmp != 0) return cmp;
|
||||
|
||||
return string.Compare(a.Instance.Definition.Id, b.Instance.Definition.Id, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Modifiers.Pipeline
|
||||
{
|
||||
public class ModifierPipeline
|
||||
{
|
||||
private readonly ModifierRegistry registry;
|
||||
private readonly List<EffectEntry> effectBuffer = new();
|
||||
private bool isExecuting;
|
||||
private readonly Queue<(TriggerType trigger, ModifierContext context)> deferredQueue = new();
|
||||
|
||||
private const int MaxRecursionDepth = 1;
|
||||
private int currentDepth;
|
||||
|
||||
public bool TracingEnabled { get; set; }
|
||||
#if UNITY_EDITOR || DEVELOPMENT_BUILD
|
||||
= true;
|
||||
#endif
|
||||
|
||||
public ModifierPipeline(ModifierRegistry registry)
|
||||
{
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
public async UniTask<ModifierContext> Execute(TriggerType trigger, ModifierContext context)
|
||||
{
|
||||
if (isExecuting)
|
||||
{
|
||||
if (currentDepth >= MaxRecursionDepth)
|
||||
{
|
||||
Debug.LogWarning($"[ModifierPipeline] Max recursion depth reached for trigger {trigger}. Dropping.");
|
||||
return context;
|
||||
}
|
||||
|
||||
currentDepth++;
|
||||
}
|
||||
|
||||
isExecuting = true;
|
||||
context.Trigger = trigger;
|
||||
|
||||
PipelineTrace trace = null;
|
||||
if (TracingEnabled)
|
||||
{
|
||||
trace = new PipelineTrace { Trigger = trigger };
|
||||
context.DebugLog ??= new List<string>();
|
||||
}
|
||||
|
||||
effectBuffer.Clear();
|
||||
|
||||
// Snapshot active modifiers to avoid modification during iteration
|
||||
var activeSnapshot = registry.Active;
|
||||
|
||||
// Gather eligible effects
|
||||
for (int i = 0; i < activeSnapshot.Count; i++)
|
||||
{
|
||||
var inst = activeSnapshot[i];
|
||||
var behaviors = inst.Definition.Behaviors;
|
||||
|
||||
for (int b = 0; b < behaviors.Count; b++)
|
||||
{
|
||||
var behavior = behaviors[b];
|
||||
if (behavior == null) continue;
|
||||
if (behavior.Trigger != trigger) continue;
|
||||
|
||||
// Evaluate conditions with tracing
|
||||
bool passed = true;
|
||||
string failedCondition = null;
|
||||
|
||||
var conditions = behavior.Conditions;
|
||||
for (int c = 0; c < conditions.Count; c++)
|
||||
{
|
||||
if (conditions[c] == null) continue;
|
||||
if (!conditions[c].Evaluate(context, inst))
|
||||
{
|
||||
passed = false;
|
||||
failedCondition = conditions[c].name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (trace != null)
|
||||
trace.AddConditionResult(inst.Definition.Id, behavior.name, passed, failedCondition);
|
||||
|
||||
if (!passed) continue;
|
||||
|
||||
// Collect effects
|
||||
var effects = behavior.Effects;
|
||||
for (int e = 0; e < effects.Count; e++)
|
||||
{
|
||||
if (effects[e] == null) continue;
|
||||
effectBuffer.Add(new EffectEntry
|
||||
{
|
||||
Effect = effects[e],
|
||||
Instance = inst,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by Phase -> Priority -> Id
|
||||
effectBuffer.Sort(ModifierComparer.Default);
|
||||
|
||||
// Execute sequentially
|
||||
for (int i = 0; i < effectBuffer.Count; i++)
|
||||
{
|
||||
var entry = effectBuffer[i];
|
||||
await entry.Effect.Apply(context, entry.Instance);
|
||||
|
||||
if (trace != null)
|
||||
trace.AddEffectApplied(entry.Instance.Definition.Id, entry.Effect.name, entry.Effect.Phase);
|
||||
}
|
||||
|
||||
if (trace != null)
|
||||
{
|
||||
string traceStr = trace.ToString();
|
||||
context.DebugLog.Add(traceStr);
|
||||
Debug.Log(traceStr);
|
||||
}
|
||||
|
||||
isExecuting = false;
|
||||
currentDepth = 0;
|
||||
|
||||
return context;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using YachtDice.Modifiers.Core;
|
||||
|
||||
namespace YachtDice.Modifiers.Pipeline
|
||||
{
|
||||
public class PipelineTrace
|
||||
{
|
||||
public TriggerType Trigger;
|
||||
public readonly List<TraceEntry> Entries = new();
|
||||
|
||||
public struct TraceEntry
|
||||
{
|
||||
public string ModifierId;
|
||||
public string BehaviorName;
|
||||
public bool ConditionsPassed;
|
||||
public string FailedCondition;
|
||||
public string EffectApplied;
|
||||
public ModifierPhase Phase;
|
||||
}
|
||||
|
||||
public void AddConditionResult(string modifierId, string behaviorName, bool passed, string failedCondition = null)
|
||||
{
|
||||
Entries.Add(new TraceEntry
|
||||
{
|
||||
ModifierId = modifierId,
|
||||
BehaviorName = behaviorName,
|
||||
ConditionsPassed = passed,
|
||||
FailedCondition = failedCondition,
|
||||
});
|
||||
}
|
||||
|
||||
public void AddEffectApplied(string modifierId, string effectName, ModifierPhase phase)
|
||||
{
|
||||
Entries.Add(new TraceEntry
|
||||
{
|
||||
ModifierId = modifierId,
|
||||
EffectApplied = effectName,
|
||||
Phase = phase,
|
||||
ConditionsPassed = true,
|
||||
});
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine($"[ModifierPipeline] Trigger: {Trigger}");
|
||||
for (int i = 0; i < Entries.Count; i++)
|
||||
{
|
||||
var e = Entries[i];
|
||||
if (e.EffectApplied != null)
|
||||
{
|
||||
sb.AppendLine($" EFFECT [{e.Phase}] {e.ModifierId} -> {e.EffectApplied}");
|
||||
}
|
||||
else if (e.ConditionsPassed)
|
||||
{
|
||||
sb.AppendLine($" PASS {e.ModifierId} / {e.BehaviorName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.AppendLine($" FAIL {e.ModifierId} / {e.BehaviorName} (failed: {e.FailedCondition})");
|
||||
}
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System.Collections.Generic;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
|
||||
namespace YachtDice.Modifiers.Runtime
|
||||
{
|
||||
public class ModifierInstance
|
||||
{
|
||||
public ModifierDefinitionSO Definition { get; }
|
||||
public bool IsActive { get; set; }
|
||||
public int RemainingUses { get; set; }
|
||||
public int Stacks { get; set; } = 1;
|
||||
public Dictionary<string, float> CustomState { get; } = new();
|
||||
|
||||
public bool IsExpired => Definition.HasLimitedUses && RemainingUses <= 0;
|
||||
|
||||
public ModifierInstance(ModifierDefinitionSO definition)
|
||||
{
|
||||
Definition = definition;
|
||||
RemainingUses = definition.HasLimitedUses ? definition.MaxUses : -1;
|
||||
}
|
||||
|
||||
public void ConsumeCharge(int amount = 1)
|
||||
{
|
||||
if (!Definition.HasLimitedUses) return;
|
||||
RemainingUses -= amount;
|
||||
if (RemainingUses < 0) RemainingUses = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
|
||||
namespace YachtDice.Modifiers.Runtime
|
||||
{
|
||||
public class ModifierRegistry
|
||||
{
|
||||
private readonly List<ModifierInstance> instances = new();
|
||||
private readonly List<ModifierInstance> activeCache = new();
|
||||
private int maxActiveSlots;
|
||||
private bool activeCacheDirty = true;
|
||||
|
||||
public event Action OnChanged;
|
||||
public event Action<IReadOnlyList<ModifierInstance>> OnActiveModifiersChanged;
|
||||
|
||||
public IReadOnlyList<ModifierInstance> All => instances;
|
||||
public int MaxActiveSlots => maxActiveSlots;
|
||||
|
||||
public ModifierRegistry(int maxActiveSlots = 5)
|
||||
{
|
||||
this.maxActiveSlots = maxActiveSlots;
|
||||
}
|
||||
|
||||
public IReadOnlyList<ModifierInstance> Active
|
||||
{
|
||||
get
|
||||
{
|
||||
if (activeCacheDirty)
|
||||
RebuildActiveCache();
|
||||
return activeCache;
|
||||
}
|
||||
}
|
||||
|
||||
public int ActiveCount
|
||||
{
|
||||
get
|
||||
{
|
||||
int count = 0;
|
||||
for (int i = 0; i < instances.Count; i++)
|
||||
if (instances[i].IsActive) count++;
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetMaxActiveSlots(int slots)
|
||||
{
|
||||
maxActiveSlots = slots;
|
||||
}
|
||||
|
||||
public ModifierInstance Add(ModifierDefinitionSO definition)
|
||||
{
|
||||
var instance = new ModifierInstance(definition);
|
||||
instances.Add(instance);
|
||||
activeCacheDirty = true;
|
||||
OnChanged?.Invoke();
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void Remove(ModifierInstance instance)
|
||||
{
|
||||
if (!instances.Contains(instance)) return;
|
||||
|
||||
bool wasActive = instance.IsActive;
|
||||
instance.IsActive = false;
|
||||
instances.Remove(instance);
|
||||
activeCacheDirty = true;
|
||||
|
||||
if (wasActive)
|
||||
OnActiveModifiersChanged?.Invoke(Active);
|
||||
|
||||
OnChanged?.Invoke();
|
||||
}
|
||||
|
||||
public bool TryActivate(ModifierInstance instance)
|
||||
{
|
||||
if (instance.IsActive) return false;
|
||||
if (!instances.Contains(instance)) return false;
|
||||
if (ActiveCount >= maxActiveSlots) return false;
|
||||
|
||||
instance.IsActive = true;
|
||||
activeCacheDirty = true;
|
||||
OnActiveModifiersChanged?.Invoke(Active);
|
||||
OnChanged?.Invoke();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Deactivate(ModifierInstance instance)
|
||||
{
|
||||
if (!instance.IsActive) return;
|
||||
|
||||
instance.IsActive = false;
|
||||
activeCacheDirty = true;
|
||||
OnActiveModifiersChanged?.Invoke(Active);
|
||||
OnChanged?.Invoke();
|
||||
}
|
||||
|
||||
public void ConsumeChargesOnActive()
|
||||
{
|
||||
bool changed = false;
|
||||
|
||||
for (int i = instances.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var inst = instances[i];
|
||||
if (!inst.IsActive) continue;
|
||||
if (!inst.Definition.HasLimitedUses) continue;
|
||||
|
||||
inst.ConsumeCharge();
|
||||
|
||||
if (inst.IsExpired)
|
||||
{
|
||||
instances.RemoveAt(i);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (changed)
|
||||
{
|
||||
activeCacheDirty = true;
|
||||
OnActiveModifiersChanged?.Invoke(Active);
|
||||
OnChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public List<ModifierSaveEntry> GetSaveData()
|
||||
{
|
||||
var entries = new List<ModifierSaveEntry>();
|
||||
for (int i = 0; i < instances.Count; i++)
|
||||
{
|
||||
var inst = instances[i];
|
||||
var entry = new ModifierSaveEntry
|
||||
{
|
||||
ModifierId = inst.Definition.Id,
|
||||
IsActive = inst.IsActive,
|
||||
RemainingUses = inst.RemainingUses,
|
||||
Stacks = inst.Stacks,
|
||||
};
|
||||
|
||||
foreach (var kvp in inst.CustomState)
|
||||
{
|
||||
entry.CustomState.Add(new CustomStateEntry
|
||||
{
|
||||
Key = kvp.Key,
|
||||
Value = kvp.Value,
|
||||
});
|
||||
}
|
||||
|
||||
entries.Add(entry);
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
public void LoadSaveData(List<ModifierSaveEntry> entries, ModifierCatalogSO catalog)
|
||||
{
|
||||
instances.Clear();
|
||||
activeCacheDirty = true;
|
||||
|
||||
if (entries == null) return;
|
||||
|
||||
for (int i = 0; i < entries.Count; i++)
|
||||
{
|
||||
var entry = entries[i];
|
||||
var definition = catalog.FindById(entry.ModifierId);
|
||||
|
||||
if (definition == null)
|
||||
{
|
||||
Debug.LogWarning($"Modifier '{entry.ModifierId}' not found in catalog, skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
var instance = new ModifierInstance(definition)
|
||||
{
|
||||
IsActive = entry.IsActive,
|
||||
RemainingUses = entry.RemainingUses,
|
||||
Stacks = entry.Stacks,
|
||||
};
|
||||
|
||||
if (entry.CustomState != null)
|
||||
{
|
||||
foreach (var cs in entry.CustomState)
|
||||
instance.CustomState[cs.Key] = cs.Value;
|
||||
}
|
||||
|
||||
instances.Add(instance);
|
||||
}
|
||||
|
||||
OnActiveModifiersChanged?.Invoke(Active);
|
||||
OnChanged?.Invoke();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
instances.Clear();
|
||||
activeCacheDirty = true;
|
||||
OnActiveModifiersChanged?.Invoke(Active);
|
||||
OnChanged?.Invoke();
|
||||
}
|
||||
|
||||
private void RebuildActiveCache()
|
||||
{
|
||||
activeCache.Clear();
|
||||
for (int i = 0; i < instances.Count; i++)
|
||||
{
|
||||
if (instances[i].IsActive)
|
||||
activeCache.Add(instances[i]);
|
||||
}
|
||||
activeCacheDirty = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace YachtDice.Modifiers.Runtime
|
||||
{
|
||||
[Serializable]
|
||||
public class ModifierSaveEntry
|
||||
{
|
||||
public string ModifierId;
|
||||
public bool IsActive;
|
||||
public int RemainingUses;
|
||||
public int Stacks;
|
||||
public List<CustomStateEntry> CustomState = new();
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class CustomStateEntry
|
||||
{
|
||||
public string Key;
|
||||
public float Value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user