[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,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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user