Files
YachtDice/Assets/Scripts/Shop/ShopModel.cs
T
horooko fcceb0ce45 Rework shop to support multiple item types and add unified PlayerModel
Generalize the shop from selling only ModifierDefinition to any IShopItem
(modifiers + dice). Add purchase condition checks, hover tooltips, and
fixed prices. Introduce PlayerModel as a facade over CurrencyBank,
InventoryModel, and DiceCollection for centralized state and save/load.

New files:
- IShopItem interface for purchasable items
- ShopCatalog SO (unified catalog for all item types)
- ShopTooltipView (description on hover)
- PlayerModel (aggregates player state)
- DiceCollection (owned dice tracking)
- DiceCatalog SO (dice definition registry)
- DiceCollectionTests

Modified:
- ModifierDefinition/DieDefinitionSO implement IShopItem
- ShopModel/ShopView/ShopItemView/ShopController use IShopItem
- SaveData v3 with OwnedDiceIds
- GameController uses PlayerModel for save/load
- GameLifetimeScope registers new services

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-01 21:04:34 +07:00

96 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using YachtDice.Dice;
using YachtDice.Economy;
using YachtDice.Inventory;
using YachtDice.Modifiers.Definition;
using YachtDice.Player;
namespace YachtDice.Shop
{
public class ShopModel
{
private readonly CurrencyBank currencyBank;
private readonly InventoryModel inventoryModel;
private readonly DiceCollection diceCollection;
private readonly HashSet<string> purchasedPermanentIds = new();
public event Action<IShopItem> OnItemPurchased;
public ShopModel(CurrencyBank currencyBank, InventoryModel inventoryModel, DiceCollection diceCollection)
{
this.currencyBank = currencyBank;
this.inventoryModel = inventoryModel;
this.diceCollection = diceCollection;
}
public bool CanPurchase(IShopItem item)
{
if (item == null) return false;
if (!currencyBank.CanAfford(item.ShopPrice)) return false;
if (!item.IsRepurchasable && purchasedPermanentIds.Contains(item.Id))
return false;
return true;
}
public bool TryPurchase(IShopItem item)
{
if (!CanPurchase(item)) return false;
if (!currencyBank.Spend(item.ShopPrice)) return false;
if (!item.IsRepurchasable)
purchasedPermanentIds.Add(item.Id);
switch (item)
{
case ModifierDefinition modifier:
inventoryModel.AddModifier(modifier);
break;
case DieDefinitionSO die:
diceCollection.Add(die);
break;
}
OnItemPurchased?.Invoke(item);
return true;
}
public bool IsPermanentOwned(string itemId) => purchasedPermanentIds.Contains(itemId);
public ShopItemState GetItemState(IShopItem item)
{
if (item == null) return ShopItemState.TooExpensive;
if (!item.IsRepurchasable && purchasedPermanentIds.Contains(item.Id))
return ShopItemState.Owned;
if (!currencyBank.CanAfford(item.ShopPrice))
return ShopItemState.TooExpensive;
return item.IsRepurchasable
? ShopItemState.RepurchaseAvailable
: ShopItemState.Available;
}
public void LoadPurchasedPermanentIds(IEnumerable<string> ids)
{
purchasedPermanentIds.Clear();
if (ids != null)
foreach (var id in ids) purchasedPermanentIds.Add(id);
}
public HashSet<string> GetPurchasedPermanentIds() => new(purchasedPermanentIds);
}
public enum ShopItemState
{
Available,
TooExpensive,
Owned,
RepurchaseAvailable,
}
}