Files
YachtDice/Assets/Scripts/Inventory/InventoryModel.cs
T
horooko bee20fd1f8 [Refactor] Add folder-based namespaces to all C# scripts
Wrap all 39 scripts and 6 test files in namespaces matching their folder
structure (e.g. Assets/Scripts/Dice/ → YachtDice.Dice). Add cross-namespace
using directives where types are referenced across modules. Set rootNamespace
in both .asmdef files (YachtDice, YachtDice.Tests).

Namespace mapping:
- YachtDice.Dice — Dice, DiceRoller
- YachtDice.Economy — CurrencyBank
- YachtDice.Game — GameManager, DiceManager, DebugGameInput
- YachtDice.Inventory — InventoryController/Model/SlotView/View
- YachtDice.Modifiers — ModifierData/Effect/Enums/Pipeline/Runtime/Target
- YachtDice.Persistence — SaveData, SaveSystem
- YachtDice.Scoring — CategoryScorer, ScoreResult, ScoringSystem, YachtCategory
- YachtDice.Shop — ShopCatalog/Controller/ItemView/Model/View
- YachtDice.UI — CategoryRowView, DicePanelView, GameController, GameInfoView, ScoreCardView
- YachtDice.Editor — ModifierAssetCreator
- YachtDice.Tests — all test files

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-28 19:06:57 +07:00

133 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using YachtDice.Modifiers;
namespace YachtDice.Inventory
{
public sealed class InventoryModel
{
private readonly List<ModifierRuntime> ownedModifiers = new();
private int maxActiveSlots;
public IReadOnlyList<ModifierRuntime> OwnedModifiers => ownedModifiers;
public int MaxActiveSlots => maxActiveSlots;
public event Action OnInventoryChanged;
public event Action<List<ModifierData>> OnActiveModifiersChanged;
public InventoryModel(int maxActiveSlots = 5)
{
this.maxActiveSlots = maxActiveSlots;
}
public int ActiveCount
{
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);
}
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);
}
}