[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,6 +1,6 @@
|
||||
using UnityEngine;
|
||||
using YachtDice.Economy;
|
||||
using YachtDice.Modifiers;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
using YachtDice.Scoring;
|
||||
|
||||
namespace YachtDice.Inventory
|
||||
@@ -23,7 +23,6 @@ namespace YachtDice.Inventory
|
||||
inventoryView.OnDeactivateClicked += HandleDeactivate;
|
||||
inventoryView.OnSellClicked += HandleSell;
|
||||
|
||||
model.OnActiveModifiersChanged += HandleActiveModifiersChanged;
|
||||
model.OnInventoryChanged += HandleInventoryChanged;
|
||||
|
||||
if (scoringSystem != null)
|
||||
@@ -42,42 +41,33 @@ namespace YachtDice.Inventory
|
||||
}
|
||||
|
||||
if (model != null)
|
||||
{
|
||||
model.OnActiveModifiersChanged -= HandleActiveModifiersChanged;
|
||||
model.OnInventoryChanged -= HandleInventoryChanged;
|
||||
}
|
||||
|
||||
if (scoringSystem != null)
|
||||
scoringSystem.OnCategoryConfirmed -= HandleCategoryConfirmed;
|
||||
}
|
||||
|
||||
private void HandleActivate(ModifierRuntime runtime)
|
||||
private void HandleActivate(ModifierInstance instance)
|
||||
{
|
||||
model.TryActivate(runtime);
|
||||
model.TryActivate(instance);
|
||||
}
|
||||
|
||||
private void HandleDeactivate(ModifierRuntime runtime)
|
||||
private void HandleDeactivate(ModifierInstance instance)
|
||||
{
|
||||
model.Deactivate(runtime);
|
||||
model.Deactivate(instance);
|
||||
}
|
||||
|
||||
private void HandleSell(ModifierRuntime runtime)
|
||||
private void HandleSell(ModifierInstance instance)
|
||||
{
|
||||
if (runtime.Data == null) return;
|
||||
if (instance.Definition == null) return;
|
||||
|
||||
int sellPrice = runtime.Data.SellPrice;
|
||||
model.RemoveModifier(runtime);
|
||||
int sellPrice = instance.Definition.SellPrice;
|
||||
model.RemoveModifier(instance);
|
||||
|
||||
if (currencyBank != null)
|
||||
currencyBank.Add(sellPrice);
|
||||
}
|
||||
|
||||
private void HandleActiveModifiersChanged(System.Collections.Generic.List<ModifierData> activeModifiers)
|
||||
{
|
||||
if (scoringSystem != null)
|
||||
scoringSystem.SetActiveModifiers(activeModifiers);
|
||||
}
|
||||
|
||||
private void HandleInventoryChanged()
|
||||
{
|
||||
RefreshView();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ using System;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using YachtDice.Modifiers;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Inventory
|
||||
{
|
||||
@@ -22,39 +22,39 @@ namespace YachtDice.Inventory
|
||||
[SerializeField] private Color activeColor = new(0.7f, 1f, 0.7f);
|
||||
[SerializeField] private Color inactiveColor = Color.white;
|
||||
|
||||
private ModifierRuntime runtime;
|
||||
private ModifierInstance instance;
|
||||
|
||||
public event Action<ModifierRuntime> OnActivateClicked;
|
||||
public event Action<ModifierRuntime> OnDeactivateClicked;
|
||||
public event Action<ModifierRuntime> OnSellClicked;
|
||||
public event Action<ModifierInstance> OnActivateClicked;
|
||||
public event Action<ModifierInstance> OnDeactivateClicked;
|
||||
public event Action<ModifierInstance> OnSellClicked;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (activateButton != null)
|
||||
activateButton.onClick.AddListener(() => OnActivateClicked?.Invoke(runtime));
|
||||
activateButton.onClick.AddListener(() => OnActivateClicked?.Invoke(instance));
|
||||
if (deactivateButton != null)
|
||||
deactivateButton.onClick.AddListener(() => OnDeactivateClicked?.Invoke(runtime));
|
||||
deactivateButton.onClick.AddListener(() => OnDeactivateClicked?.Invoke(instance));
|
||||
if (sellButton != null)
|
||||
sellButton.onClick.AddListener(() => OnSellClicked?.Invoke(runtime));
|
||||
sellButton.onClick.AddListener(() => OnSellClicked?.Invoke(instance));
|
||||
}
|
||||
|
||||
public void Setup(ModifierRuntime modifierRuntime, bool canActivateMore)
|
||||
public void Setup(ModifierInstance modifierInstance, bool canActivateMore)
|
||||
{
|
||||
runtime = modifierRuntime;
|
||||
var data = runtime.Data;
|
||||
instance = modifierInstance;
|
||||
var def = instance.Definition;
|
||||
|
||||
if (data == null) return;
|
||||
if (def == null) return;
|
||||
|
||||
if (nameText != null) nameText.text = data.DisplayName;
|
||||
if (descriptionText != null) descriptionText.text = data.Description;
|
||||
if (iconImage != null && data.Icon != null) iconImage.sprite = data.Icon;
|
||||
if (nameText != null) nameText.text = def.DisplayName;
|
||||
if (descriptionText != null) descriptionText.text = def.Description;
|
||||
if (iconImage != null && def.Icon != null) iconImage.sprite = def.Icon;
|
||||
|
||||
if (usesText != null)
|
||||
{
|
||||
if (data.Durability == ModifierDurability.LimitedUses)
|
||||
if (def.HasLimitedUses)
|
||||
{
|
||||
usesText.gameObject.SetActive(true);
|
||||
usesText.text = $"{runtime.RemainingUses}/{data.MaxUses}";
|
||||
usesText.text = $"{instance.RemainingUses}/{def.MaxUses}";
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -62,9 +62,9 @@ namespace YachtDice.Inventory
|
||||
}
|
||||
}
|
||||
|
||||
if (sellPriceText != null) sellPriceText.text = data.SellPrice.ToString();
|
||||
if (sellPriceText != null) sellPriceText.text = def.SellPrice.ToString();
|
||||
|
||||
bool isActive = runtime.IsActive;
|
||||
bool isActive = instance.IsActive;
|
||||
|
||||
if (activateButton != null)
|
||||
{
|
||||
|
||||
@@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using YachtDice.Modifiers;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
|
||||
namespace YachtDice.Inventory
|
||||
{
|
||||
@@ -16,9 +16,9 @@ namespace YachtDice.Inventory
|
||||
|
||||
private readonly List<InventorySlotView> spawnedSlots = new();
|
||||
|
||||
public event Action<ModifierRuntime> OnActivateClicked;
|
||||
public event Action<ModifierRuntime> OnDeactivateClicked;
|
||||
public event Action<ModifierRuntime> OnSellClicked;
|
||||
public event Action<ModifierInstance> OnActivateClicked;
|
||||
public event Action<ModifierInstance> OnDeactivateClicked;
|
||||
public event Action<ModifierInstance> OnSellClicked;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -36,7 +36,7 @@ namespace YachtDice.Inventory
|
||||
public void Hide() => gameObject.SetActive(false);
|
||||
public bool IsVisible => gameObject.activeSelf;
|
||||
|
||||
public void Refresh(IReadOnlyList<ModifierRuntime> owned, int maxSlots)
|
||||
public void Refresh(IReadOnlyList<ModifierInstance> owned, int maxSlots)
|
||||
{
|
||||
ClearSlots();
|
||||
|
||||
@@ -44,11 +44,11 @@ namespace YachtDice.Inventory
|
||||
|
||||
for (int i = 0; i < owned.Count; i++)
|
||||
{
|
||||
var runtime = owned[i];
|
||||
if (runtime.IsActive) activeCount++;
|
||||
var inst = owned[i];
|
||||
if (inst.IsActive) activeCount++;
|
||||
|
||||
var slot = Instantiate(slotPrefab, slotContainer);
|
||||
slot.Setup(runtime, activeCount <= maxSlots);
|
||||
slot.Setup(inst, activeCount <= maxSlots);
|
||||
slot.OnActivateClicked += HandleActivate;
|
||||
slot.OnDeactivateClicked += HandleDeactivate;
|
||||
slot.OnSellClicked += HandleSell;
|
||||
@@ -71,8 +71,8 @@ namespace YachtDice.Inventory
|
||||
spawnedSlots.Clear();
|
||||
}
|
||||
|
||||
private void HandleActivate(ModifierRuntime runtime) => OnActivateClicked?.Invoke(runtime);
|
||||
private void HandleDeactivate(ModifierRuntime runtime) => OnDeactivateClicked?.Invoke(runtime);
|
||||
private void HandleSell(ModifierRuntime runtime) => OnSellClicked?.Invoke(runtime);
|
||||
private void HandleActivate(ModifierInstance inst) => OnActivateClicked?.Invoke(inst);
|
||||
private void HandleDeactivate(ModifierInstance inst) => OnDeactivateClicked?.Invoke(inst);
|
||||
private void HandleSell(ModifierInstance inst) => OnSellClicked?.Invoke(inst);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user