Files
YachtDice/Assets/Scripts/Shop/ShopView.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

98 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace YachtDice.Shop
{
public class ShopView : MonoBehaviour
{
[SerializeField] private Transform itemContainer;
[SerializeField] private ShopItemView itemPrefab;
[SerializeField] private TMP_Text currencyText;
[SerializeField] private Button closeButton;
[SerializeField] private ShopTooltipView tooltipView;
private readonly List<ShopItemView> spawnedItems = new();
public event Action<IShopItem> OnBuyClicked;
private void Awake()
{
if (closeButton != null)
closeButton.onClick.AddListener(Hide);
}
private void OnDestroy()
{
if (closeButton != null)
closeButton.onClick.RemoveListener(Hide);
}
public void Show() => gameObject.SetActive(true);
public void Hide() => gameObject.SetActive(false);
public bool IsVisible => gameObject.activeSelf;
public void Populate(IReadOnlyList<IShopItem> catalog, ShopModel model)
{
ClearItems();
for (int i = 0; i < catalog.Count; i++)
{
var def = catalog[i];
if (def == null) continue;
var item = Instantiate(itemPrefab, itemContainer);
var state = model.GetItemState(def);
item.Setup(def, state);
item.OnBuyClicked += HandleBuy;
item.OnHoverEnter += HandleHoverEnter;
item.OnHoverExit += HandleHoverExit;
spawnedItems.Add(item);
}
}
public void RefreshStates(IReadOnlyList<IShopItem> catalog, ShopModel model)
{
for (int i = 0; i < spawnedItems.Count && i < catalog.Count; i++)
{
var state = model.GetItemState(catalog[i]);
spawnedItems[i].SetState(state);
}
}
public void UpdateCurrencyDisplay(int currency)
{
if (currencyText != null)
currencyText.text = currency.ToString();
}
private void ClearItems()
{
for (int i = 0; i < spawnedItems.Count; i++)
{
spawnedItems[i].OnBuyClicked -= HandleBuy;
spawnedItems[i].OnHoverEnter -= HandleHoverEnter;
spawnedItems[i].OnHoverExit -= HandleHoverExit;
Destroy(spawnedItems[i].gameObject);
}
spawnedItems.Clear();
}
private void HandleBuy(IShopItem item) => OnBuyClicked?.Invoke(item);
private void HandleHoverEnter(IShopItem item, RectTransform anchor)
{
if (tooltipView != null)
tooltipView.Show(item, anchor);
}
private void HandleHoverExit()
{
if (tooltipView != null)
tooltipView.Hide();
}
}
}