80 lines
2.1 KiB
C#
80 lines
2.1 KiB
C#
using Kryz.CharacterStats.Examples;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class VendorUI : MonoBehaviour
|
|
{
|
|
public GameObject vendorUI;
|
|
|
|
public List<VendorSlotUI> slots = new List<VendorSlotUI>();
|
|
[SerializeField] private GameEvent onVendorUIClosed;
|
|
[SerializeField] private GameEvent onCraftingAnvilReleased;
|
|
[SerializeField] private GameEventListener_VendorData onVendorInteracted;
|
|
[SerializeField] private GameEventListener onVendorReleased;
|
|
|
|
private VendorDataInstance currentOpenVendor;
|
|
|
|
private void Awake()
|
|
{
|
|
onVendorInteracted.Response.AddListener((vendorData) =>
|
|
{
|
|
OpenVendor(vendorData);
|
|
});
|
|
|
|
onVendorReleased.Response.AddListener(CloseVendor);
|
|
|
|
Inventory.Instance.OnItemRightClickedEventWhileVendoring.AddListener(SellItem);
|
|
|
|
vendorUI.SetActive(false);
|
|
}
|
|
|
|
private void InitializeVendorUI(VendorDataInstance vendorData)
|
|
{
|
|
for (int i = 0; i < slots.Count; i++)
|
|
{
|
|
slots[i].ClearSlot();
|
|
}
|
|
for (int i = 0; i < vendorData.items.Count; i++)
|
|
{
|
|
slots[i].AddItem(currentOpenVendor, vendorData.items[i]);
|
|
}
|
|
|
|
}
|
|
|
|
public void OpenVendor(VendorDataInstance vendor)
|
|
{
|
|
currentOpenVendor = vendor;
|
|
InitializeVendorUI(vendor);
|
|
|
|
vendorUI.SetActive(true);
|
|
|
|
Inventory.Instance.isVendoring = true;
|
|
Inventory.Instance.isCrafting = false;
|
|
Inventory.Instance.OpenInventory();
|
|
//close any existing crafint UI to avoid conflicts
|
|
onCraftingAnvilReleased.Raise();
|
|
}
|
|
|
|
public void CloseVendor()
|
|
{
|
|
vendorUI.SetActive(false);
|
|
currentOpenVendor = null;
|
|
|
|
Inventory.Instance.isVendoring = false;
|
|
Inventory.Instance.CloseInventory();
|
|
|
|
onVendorUIClosed.Raise();
|
|
}
|
|
|
|
private void SellItem(ItemInstance item)
|
|
{
|
|
if (Inventory.Instance.RemoveItem(item))
|
|
{
|
|
CoinBag.Instance.ChangeAmount(item.sellPricePlayer);
|
|
ItemTooltip.Instance.HideTooltip();
|
|
EquippedItemTooltip.Instance.HideTooltip();
|
|
}
|
|
}
|
|
}
|