using UnityEngine; using VContainer; using YachtDice.Economy; namespace YachtDice.Shop { public class ShopController : MonoBehaviour { [SerializeField] private ShopView shopView; private ShopCatalog _catalog; private CurrencyBank _currencyBank; private ShopModel _model; public ShopCatalog Catalog => _catalog; [Inject] public void Construct(ShopCatalog 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(IShopItem item) { _model.TryPurchase(item); } private void HandleCurrencyChanged(int newBalance) { shopView.UpdateCurrencyDisplay(newBalance); shopView.RefreshStates(_catalog.All, _model); } private void HandleItemPurchased(IShopItem item) { shopView.RefreshStates(_catalog.All, _model); } } }