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