[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:
@@ -1,131 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using YachtDice.Modifiers;
|
||||
using YachtDice.Modifiers.Definition;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Inventory
|
||||
{
|
||||
public class InventoryModel
|
||||
{
|
||||
private readonly List<ModifierRuntime> ownedModifiers = new();
|
||||
private int maxActiveSlots;
|
||||
|
||||
public IReadOnlyList<ModifierRuntime> OwnedModifiers => ownedModifiers;
|
||||
public int MaxActiveSlots => maxActiveSlots;
|
||||
private readonly ModifierRegistry registry;
|
||||
|
||||
public event Action OnInventoryChanged;
|
||||
public event Action<List<ModifierData>> OnActiveModifiersChanged;
|
||||
public event Action<IReadOnlyList<ModifierInstance>> OnActiveModifiersChanged;
|
||||
|
||||
public InventoryModel(int maxActiveSlots = 5)
|
||||
public InventoryModel(ModifierRegistry registry)
|
||||
{
|
||||
this.maxActiveSlots = maxActiveSlots;
|
||||
this.registry = registry;
|
||||
|
||||
registry.OnChanged += () => OnInventoryChanged?.Invoke();
|
||||
registry.OnActiveModifiersChanged += list => OnActiveModifiersChanged?.Invoke(list);
|
||||
}
|
||||
|
||||
public int ActiveCount
|
||||
public IReadOnlyList<ModifierInstance> OwnedModifiers => registry.All;
|
||||
public int MaxActiveSlots => registry.MaxActiveSlots;
|
||||
public int ActiveCount => registry.ActiveCount;
|
||||
|
||||
public void SetMaxActiveSlots(int slots) => registry.SetMaxActiveSlots(slots);
|
||||
|
||||
public void AddModifier(ModifierDefinitionSO definition) => registry.Add(definition);
|
||||
|
||||
public void RemoveModifier(ModifierInstance instance) => registry.Remove(instance);
|
||||
|
||||
public bool TryActivate(ModifierInstance instance) => registry.TryActivate(instance);
|
||||
|
||||
public void Deactivate(ModifierInstance instance) => registry.Deactivate(instance);
|
||||
|
||||
public void ConsumeUseOnActive() => registry.ConsumeChargesOnActive();
|
||||
|
||||
public List<ModifierDefinitionSO> GetActiveModifierDefinitions()
|
||||
{
|
||||
get
|
||||
{
|
||||
int count = 0;
|
||||
for (int i = 0; i < ownedModifiers.Count; i++)
|
||||
if (ownedModifiers[i].IsActive) count++;
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetMaxActiveSlots(int slots)
|
||||
{
|
||||
maxActiveSlots = slots;
|
||||
}
|
||||
|
||||
public void AddModifier(ModifierData data)
|
||||
{
|
||||
var runtime = ModifierRuntime.Create(data);
|
||||
ownedModifiers.Add(runtime);
|
||||
OnInventoryChanged?.Invoke();
|
||||
}
|
||||
|
||||
public void RemoveModifier(ModifierRuntime modifier)
|
||||
{
|
||||
if (!ownedModifiers.Contains(modifier)) return;
|
||||
|
||||
if (modifier.IsActive)
|
||||
{
|
||||
modifier.IsActive = false;
|
||||
OnActiveModifiersChanged?.Invoke(GetActiveModifierData());
|
||||
}
|
||||
|
||||
ownedModifiers.Remove(modifier);
|
||||
OnInventoryChanged?.Invoke();
|
||||
}
|
||||
|
||||
public bool TryActivate(ModifierRuntime modifier)
|
||||
{
|
||||
if (modifier.IsActive) return false;
|
||||
if (!ownedModifiers.Contains(modifier)) return false;
|
||||
if (ActiveCount >= maxActiveSlots) return false;
|
||||
|
||||
modifier.IsActive = true;
|
||||
OnActiveModifiersChanged?.Invoke(GetActiveModifierData());
|
||||
OnInventoryChanged?.Invoke();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Deactivate(ModifierRuntime modifier)
|
||||
{
|
||||
if (!modifier.IsActive) return;
|
||||
|
||||
modifier.IsActive = false;
|
||||
OnActiveModifiersChanged?.Invoke(GetActiveModifierData());
|
||||
OnInventoryChanged?.Invoke();
|
||||
}
|
||||
|
||||
public void ConsumeUseOnActive()
|
||||
{
|
||||
bool changed = false;
|
||||
|
||||
for (int i = ownedModifiers.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var mod = ownedModifiers[i];
|
||||
if (!mod.IsActive) continue;
|
||||
if (mod.Data == null) continue;
|
||||
if (mod.Data.Durability != ModifierDurability.LimitedUses) continue;
|
||||
|
||||
mod.ConsumeUse();
|
||||
|
||||
if (mod.IsExpired)
|
||||
{
|
||||
ownedModifiers.RemoveAt(i);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (changed)
|
||||
{
|
||||
OnActiveModifiersChanged?.Invoke(GetActiveModifierData());
|
||||
OnInventoryChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public List<ModifierData> GetActiveModifierData()
|
||||
{
|
||||
var result = new List<ModifierData>();
|
||||
for (int i = 0; i < ownedModifiers.Count; i++)
|
||||
{
|
||||
if (ownedModifiers[i].IsActive && ownedModifiers[i].Data != null)
|
||||
result.Add(ownedModifiers[i].Data);
|
||||
}
|
||||
var result = new List<ModifierDefinitionSO>();
|
||||
var active = registry.Active;
|
||||
for (int i = 0; i < active.Count; i++)
|
||||
result.Add(active[i].Definition);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void LoadState(List<ModifierRuntime> loaded)
|
||||
{
|
||||
ownedModifiers.Clear();
|
||||
if (loaded != null)
|
||||
ownedModifiers.AddRange(loaded);
|
||||
|
||||
OnActiveModifiersChanged?.Invoke(GetActiveModifierData());
|
||||
OnInventoryChanged?.Invoke();
|
||||
}
|
||||
|
||||
public List<ModifierRuntime> GetAllForSave() => new(ownedModifiers);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user