3c50415111
- Register InventoryModel, ShopModel as container-managed singletons - Register GameController, ShopController, InventoryController via RegisterComponent - Replace [SerializeField] with [Inject] for service dependencies in controllers - Move maxActiveModifierSlots config to GameLifetimeScope (composition root) - Remove manual model creation and Initialize() calls from GameController - Add ToggleVisibility() to ShopController/InventoryController, removing GetComponentInChildren - Move event subscriptions from Awake to Start for safe VContainer injection order - Transfer game startup orchestration to GameController.Start() Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
75 lines
2.0 KiB
C#
75 lines
2.0 KiB
C#
using UnityEngine;
|
|
using VContainer;
|
|
using YachtDice.Economy;
|
|
using YachtDice.Modifiers.Definition;
|
|
|
|
namespace YachtDice.Shop
|
|
{
|
|
public class ShopController : MonoBehaviour
|
|
{
|
|
[SerializeField] private ShopView shopView;
|
|
|
|
private ModifierCatalogSO catalog;
|
|
private CurrencyBank currencyBank;
|
|
private ShopModel model;
|
|
|
|
public ModifierCatalogSO Catalog => catalog;
|
|
|
|
[Inject]
|
|
public void Construct(ModifierCatalogSO catalog, CurrencyBank currencyBank, ShopModel model)
|
|
{
|
|
this.catalog = catalog;
|
|
this.currencyBank = currencyBank;
|
|
this.model = model;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
shopView.OnBuyClicked += HandleBuyClicked;
|
|
currencyBank.OnBalanceChanged += HandleCurrencyChanged;
|
|
model.OnItemPurchased += HandleItemPurchased;
|
|
|
|
shopView.Populate(catalog.All, model);
|
|
shopView.UpdateCurrencyDisplay(currencyBank.Balance);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (shopView != null)
|
|
shopView.OnBuyClicked -= HandleBuyClicked;
|
|
|
|
if (currencyBank != null)
|
|
currencyBank.OnBalanceChanged -= HandleCurrencyChanged;
|
|
|
|
if (model != null)
|
|
model.OnItemPurchased -= HandleItemPurchased;
|
|
}
|
|
|
|
public void ToggleVisibility()
|
|
{
|
|
if (shopView == null) return;
|
|
|
|
if (shopView.IsVisible)
|
|
shopView.Hide();
|
|
else
|
|
shopView.Show();
|
|
}
|
|
|
|
private void HandleBuyClicked(ModifierDefinitionSO def)
|
|
{
|
|
model.TryPurchase(def);
|
|
}
|
|
|
|
private void HandleCurrencyChanged(int newBalance)
|
|
{
|
|
shopView.UpdateCurrencyDisplay(newBalance);
|
|
shopView.RefreshStates(catalog.All, model);
|
|
}
|
|
|
|
private void HandleItemPurchased(ModifierDefinitionSO def)
|
|
{
|
|
shopView.RefreshStates(catalog.All, model);
|
|
}
|
|
}
|
|
}
|