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 ModifierCatalog catalog;
|
|
private CurrencyBank currencyBank;
|
|
private ShopModel model;
|
|
|
|
public ModifierCatalog Catalog => catalog;
|
|
|
|
[Inject]
|
|
public void Construct(ModifierCatalog 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(ModifierDefinition def)
|
|
{
|
|
model.TryPurchase(def);
|
|
}
|
|
|
|
private void HandleCurrencyChanged(int newBalance)
|
|
{
|
|
shopView.UpdateCurrencyDisplay(newBalance);
|
|
shopView.RefreshStates(catalog.All, model);
|
|
}
|
|
|
|
private void HandleItemPurchased(ModifierDefinition def)
|
|
{
|
|
shopView.RefreshStates(catalog.All, model);
|
|
}
|
|
}
|
|
}
|