98 lines
2.6 KiB
C#
98 lines
2.6 KiB
C#
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;
|
|
public event System.Action OnCloseRequested;
|
|
|
|
[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;
|
|
shopView.OnCloseRequested += HandleCloseRequested;
|
|
_currencyBank.OnBalanceChanged += HandleCurrencyChanged;
|
|
_model.OnItemPurchased += HandleItemPurchased;
|
|
|
|
shopView.Populate(_catalog.All, _model);
|
|
shopView.UpdateCurrencyDisplay(_currencyBank.Balance);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (shopView != null)
|
|
{
|
|
shopView.OnBuyClicked -= HandleBuyClicked;
|
|
shopView.OnCloseRequested -= HandleCloseRequested;
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
public void Open()
|
|
{
|
|
if (shopView == null) return;
|
|
shopView.Show();
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
if (shopView == null) return;
|
|
shopView.Hide();
|
|
}
|
|
|
|
public bool IsOpen => shopView != null && shopView.IsVisible;
|
|
|
|
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);
|
|
}
|
|
|
|
private void HandleCloseRequested()
|
|
{
|
|
OnCloseRequested?.Invoke();
|
|
}
|
|
}
|
|
}
|