91 lines
2.4 KiB
C#
91 lines
2.4 KiB
C#
using UnityEngine;
|
|
using VContainer;
|
|
using YachtDice.Economy;
|
|
using YachtDice.Modifiers.Runtime;
|
|
|
|
namespace YachtDice.Inventory
|
|
{
|
|
public class InventoryController : MonoBehaviour
|
|
{
|
|
[SerializeField] private InventoryView inventoryView;
|
|
|
|
private InventoryModel _model;
|
|
private CurrencyBank _currencyBank;
|
|
|
|
public InventoryModel Model => _model;
|
|
|
|
[Inject]
|
|
public void Construct(InventoryModel model, CurrencyBank currencyBank)
|
|
{
|
|
this._model = model;
|
|
this._currencyBank = currencyBank;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
inventoryView.OnActivateClicked += HandleActivate;
|
|
inventoryView.OnDeactivateClicked += HandleDeactivate;
|
|
inventoryView.OnSellClicked += HandleSell;
|
|
|
|
_model.OnInventoryChanged += HandleInventoryChanged;
|
|
|
|
RefreshView();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (inventoryView != null)
|
|
{
|
|
inventoryView.OnActivateClicked -= HandleActivate;
|
|
inventoryView.OnDeactivateClicked -= HandleDeactivate;
|
|
inventoryView.OnSellClicked -= HandleSell;
|
|
}
|
|
|
|
if (_model != null)
|
|
_model.OnInventoryChanged -= HandleInventoryChanged;
|
|
}
|
|
|
|
public void ToggleVisibility()
|
|
{
|
|
if (inventoryView == null) return;
|
|
|
|
if (inventoryView.IsVisible)
|
|
inventoryView.Hide();
|
|
else
|
|
inventoryView.Show();
|
|
}
|
|
|
|
private void HandleActivate(ModifierInstance instance)
|
|
{
|
|
_model.TryActivate(instance);
|
|
}
|
|
|
|
private void HandleDeactivate(ModifierInstance instance)
|
|
{
|
|
_model.Deactivate(instance);
|
|
}
|
|
|
|
private void HandleSell(ModifierInstance instance)
|
|
{
|
|
if (instance.Definition == null) return;
|
|
|
|
var sellPrice = instance.Definition.SellPrice;
|
|
_model.RemoveModifier(instance);
|
|
|
|
if (_currencyBank != null)
|
|
_currencyBank.Add(sellPrice);
|
|
}
|
|
|
|
private void HandleInventoryChanged()
|
|
{
|
|
RefreshView();
|
|
}
|
|
|
|
private void RefreshView()
|
|
{
|
|
if (inventoryView != null && _model != null)
|
|
inventoryView.Refresh(_model.OwnedModifiers, _model.MaxActiveSlots);
|
|
}
|
|
}
|
|
}
|