419 lines
14 KiB
C#
419 lines
14 KiB
C#
using Invector;
|
|
using Sirenix.OdinInspector;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public enum TradeCategory
|
|
{ Weapons, Consumables, Scrolls, Resources };
|
|
|
|
public enum TradeOption
|
|
{ Buy, Sell };
|
|
|
|
namespace Beyond
|
|
{
|
|
public class TradeMenuController : vMonoBehaviour
|
|
{
|
|
[SerializeField] private MenuScroll menuScroll;
|
|
public bItemWindow buyWindow, sellWindow;
|
|
|
|
[SerializeField] private GameObject selectorSubmenuWindow, buySelectorWindow, sellSelectorWindow, currentSubmenuWindow, buyWeaponsWindow, sellWeaponsWindow;
|
|
|
|
private List<bItemType> weaponsFilter = new List<bItemType>() { bItemType.Swords, bItemType.Axes },
|
|
scrollsFilter = new List<bItemType>() { bItemType.PowerScroll },
|
|
consumablesFilter = new List<bItemType>() { bItemType.Consumable },
|
|
resourcesFilter = new List<bItemType>() { bItemType.Resources },
|
|
currentBuyFilter = new(),
|
|
currentSellFilter = new();
|
|
|
|
[HideInInspector] public List<bItemType> itemsFilter = new List<bItemType>() { 0 };
|
|
|
|
private int defaultItemValue = 10;
|
|
private int currentItemValue = 0;
|
|
|
|
[Header("Buy Window")]
|
|
[SerializeField] private Button buyButton; //could just be gameobject->simplier
|
|
|
|
[SerializeField] private GameObject backBuyButton; //could just be gameobject->simplier
|
|
|
|
[SerializeField] private TMP_Text goldBalanceBuyWindowText, goldBuyText;
|
|
|
|
[Header("Sell Window")]
|
|
[SerializeField] private Button sellButton; //could just be gameobject->simplier
|
|
|
|
[SerializeField] private GameObject backSellButton; //could just be gameobject->simplier
|
|
[SerializeField] private TMP_Text goldBalanceSellWindowText, goldSellText;
|
|
private float sellMultiplier = 0.9f;
|
|
private int currentItemSellValue = 0;
|
|
private string goldName = "Gold Coin", descriptionNameGold = " Gold Coins";
|
|
|
|
[Header("")]
|
|
public bItemListData itemListData;
|
|
|
|
[SerializeField] private bItemSlot itemSlotDefaultPrefab, itemSlotScrollPrefab;
|
|
public List<ItemReference> shopItemRefs = new List<ItemReference>();
|
|
[HideInInspector] public List<bItem> shopItems;
|
|
private bItem goldItemReference;
|
|
|
|
private void Awake()
|
|
{
|
|
TryToSetGoldReference();
|
|
buyWindow.onSelectSlot.AddListener(SetBuyButton);
|
|
buyWindow.onSubmitSlot.AddListener(BuyItem);
|
|
sellWindow.onSelectSlot.AddListener(SetSellValue);
|
|
sellWindow.onSubmitSlot.AddListener(SellItem);
|
|
|
|
shopItemRefs.ForEach(itemRef =>
|
|
{
|
|
shopItems.Add(
|
|
itemListData.items.Find(item => item.id == itemRef.id));
|
|
}
|
|
);
|
|
currentSubmenuWindow = selectorSubmenuWindow;
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
TryToSetGoldReference();
|
|
}
|
|
|
|
private void TryToSetGoldReference()
|
|
{
|
|
if (!goldItemReference)
|
|
{
|
|
bItemManager itemManager = Player.Instance.ItemManager;
|
|
goldItemReference = itemManager.GetItem(goldName);
|
|
}
|
|
}
|
|
|
|
private void SetBuyButton(bItemSlot itemSlot)
|
|
{
|
|
if (!itemSlot)
|
|
{
|
|
goldBuyText.text = "";
|
|
return;
|
|
}
|
|
bItem item = itemSlot.item;
|
|
bItemAttribute costAttribute = item.GetItemAttribute(bItemAttributes.Cost);
|
|
SetCurrentItemValue(item);
|
|
goldBuyText.text = currentItemValue + descriptionNameGold;
|
|
|
|
if (!goldItemReference || goldItemReference.amount < currentItemValue)
|
|
{
|
|
buyButton.gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
buyButton.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
|
|
private void SetCurrentItemValue(bItem item)
|
|
{
|
|
bItemAttribute costAttribute = item.GetItemAttribute(bItemAttributes.Cost);
|
|
if (costAttribute != null)
|
|
{
|
|
currentItemValue = costAttribute.value;
|
|
}
|
|
else
|
|
{
|
|
currentItemValue = defaultItemValue;
|
|
}
|
|
}
|
|
|
|
private void BuyItem(bItemSlot itemSlot)
|
|
{
|
|
bItemManager itemManager = Player.Instance.ItemManager;
|
|
bItem item = itemSlot.item;
|
|
itemManager.AddItemByID(item.id);
|
|
goldItemReference.amount -= currentItemValue;
|
|
goldBalanceBuyWindowText.text = goldItemReference.amount.ToString() + descriptionNameGold;
|
|
if (PopupMenuController.Instance != null)
|
|
{
|
|
PopupMenuController.Instance.TryToShowPopupMesssage("Acquired: " + item.name);
|
|
}
|
|
SetBuyButton(itemSlot);
|
|
}
|
|
|
|
private void SetSellValue(bItemSlot itemSlot)
|
|
{
|
|
if (itemSlot == null)
|
|
{
|
|
goldSellText.text = "";
|
|
currentItemSellValue = 0;
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
bItem item = itemSlot.item;
|
|
SetCurrentItemValue(item);
|
|
currentItemSellValue = (int)(currentItemValue * sellMultiplier);
|
|
goldSellText.text = currentItemSellValue + descriptionNameGold;
|
|
}
|
|
if (currentItemValue <= 0)
|
|
{
|
|
sellButton.gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
sellButton.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
|
|
private void SellItem(bItemSlot itemSlot)
|
|
{
|
|
bItemManager itemManager = Player.Instance.ItemManager;
|
|
bItem item = itemSlot.item;
|
|
itemManager.DestroyItem(item, 1);
|
|
itemManager.DisplayRemovedItem(item);
|
|
|
|
if (!goldItemReference)
|
|
{
|
|
bItem goldItem = itemListData.items.Find(item => item.name == goldName);
|
|
itemManager.AddItem(goldItem, currentItemSellValue);
|
|
goldItemReference = itemManager.GetItem(goldName);
|
|
}
|
|
else
|
|
{
|
|
goldItemReference.amount += currentItemSellValue;
|
|
}
|
|
goldBalanceSellWindowText.text = goldItemReference.amount.ToString() + descriptionNameGold;
|
|
List<bItem> items = itemManager.items;
|
|
sellWindow.CreateEquipmentWindow(items, currentSellFilter); //could maybe optimize it, to just remove/update 1 element
|
|
if (PopupMenuController.Instance != null)
|
|
{
|
|
PopupMenuController.Instance.TryToShowPopupMesssage("Sold: " + item.name);
|
|
}
|
|
SetSellValue(sellWindow.currentSelectedSlot);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set on concrete button in unity, when player returns here
|
|
/// </summary>
|
|
public void OpenSelectorWindow()
|
|
{
|
|
StartCoroutine(menuScroll.ChangeVisibleCanvaCoroutine(currentSubmenuWindow, selectorSubmenuWindow));
|
|
currentSubmenuWindow = selectorSubmenuWindow;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets selector submenu window when opening it from scroll
|
|
/// </summary>
|
|
public void SetSelectorWindow()
|
|
{
|
|
if (currentSubmenuWindow)
|
|
{
|
|
currentSubmenuWindow.gameObject.SetActive(false);
|
|
}
|
|
currentSubmenuWindow = selectorSubmenuWindow;
|
|
currentSubmenuWindow.gameObject.SetActive(true);
|
|
}
|
|
|
|
public void OpenBuySelectionWindow()
|
|
{
|
|
StartCoroutine(menuScroll.ChangeVisibleCanvaCoroutine(currentSubmenuWindow, buySelectorWindow));
|
|
currentSubmenuWindow = buySelectorWindow;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set on concrete buttons in unity
|
|
/// </summary>
|
|
public void OpenSellSelectionWindow()
|
|
{
|
|
StartCoroutine(menuScroll.ChangeVisibleCanvaCoroutine(currentSubmenuWindow, sellSelectorWindow));
|
|
currentSubmenuWindow = sellSelectorWindow;
|
|
}
|
|
|
|
public void OpenBuyWeaponsWindow()
|
|
{
|
|
SetBuyItemsFilter(weaponsFilter);
|
|
OpenBuyWindowFromMenu();
|
|
}
|
|
|
|
private void SetBuyItemsFilter(List<bItemType> newFilter)
|
|
{
|
|
SetItemsFilter(newFilter, ref currentBuyFilter, buyWindow);
|
|
}
|
|
|
|
private void SetItemsFilter(List<bItemType> newFilter, ref List<bItemType> currentFilter, bItemWindow window)
|
|
{
|
|
if (currentFilter == newFilter)
|
|
{
|
|
return;
|
|
}
|
|
if (newFilter == scrollsFilter)
|
|
{
|
|
window.ClearSlots();
|
|
window.slotPrefab = itemSlotScrollPrefab;
|
|
}
|
|
else if (currentFilter == scrollsFilter)
|
|
{
|
|
window.ClearSlots();
|
|
window.slotPrefab = itemSlotDefaultPrefab;
|
|
}
|
|
currentFilter = newFilter;
|
|
}
|
|
|
|
private void OpenBuyWindowFromMenu()
|
|
{
|
|
menuScroll.ChangeVisibleCanva(currentSubmenuWindow, buyWeaponsWindow);
|
|
backBuyButton.SetActive(true);
|
|
OpenBuyWindowBase();
|
|
}
|
|
|
|
private void OpenBuyWindowBase()
|
|
{
|
|
currentSubmenuWindow = buyWeaponsWindow;
|
|
buyWindow.CreateEquipmentWindow(shopItems, currentBuyFilter);
|
|
SetBuyButton(buyWindow.currentSelectedSlot);
|
|
if (goldItemReference)
|
|
{
|
|
goldBalanceBuyWindowText.text = goldItemReference.amount.ToString() + descriptionNameGold;
|
|
}
|
|
else
|
|
{
|
|
goldBalanceBuyWindowText.text = "0" + descriptionNameGold;
|
|
}
|
|
}
|
|
|
|
public void OpenBuyWindowStandalone(TradeCategory category)
|
|
{
|
|
switch (category)
|
|
{
|
|
case TradeCategory.Weapons:
|
|
SetBuyItemsFilter(weaponsFilter);
|
|
break;
|
|
|
|
case TradeCategory.Consumables:
|
|
SetBuyItemsFilter(consumablesFilter);
|
|
break;
|
|
|
|
case TradeCategory.Scrolls:
|
|
SetBuyItemsFilter(scrollsFilter);
|
|
break;
|
|
|
|
case TradeCategory.Resources:
|
|
SetBuyItemsFilter(resourcesFilter);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
currentSubmenuWindow.SetActive(false);
|
|
buyWeaponsWindow.SetActive(true);
|
|
OpenBuyWindowBase();
|
|
backBuyButton.SetActive(false);
|
|
}
|
|
|
|
public void OpenBuyScrollsWindow()
|
|
{
|
|
SetBuyItemsFilter(scrollsFilter);
|
|
OpenBuyWindowFromMenu();
|
|
}
|
|
|
|
public void OpenBuyConsumablesWindow()
|
|
{
|
|
SetBuyItemsFilter(consumablesFilter);
|
|
OpenBuyWindowFromMenu();
|
|
}
|
|
|
|
public void OpenBuyResourcesWindow()
|
|
{
|
|
SetBuyItemsFilter(resourcesFilter);
|
|
OpenBuyWindowFromMenu();
|
|
}
|
|
|
|
public void OpenSellWindowStandalone(TradeCategory category)
|
|
{
|
|
switch (category)
|
|
{
|
|
case TradeCategory.Weapons:
|
|
SetSellItemsFilter(weaponsFilter);
|
|
break;
|
|
|
|
case TradeCategory.Consumables:
|
|
SetSellItemsFilter(consumablesFilter);
|
|
break;
|
|
|
|
case TradeCategory.Scrolls:
|
|
SetSellItemsFilter(scrollsFilter);
|
|
break;
|
|
|
|
case TradeCategory.Resources:
|
|
SetSellItemsFilter(resourcesFilter);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
currentSubmenuWindow.SetActive(false);
|
|
sellWeaponsWindow.SetActive(true);
|
|
OpenSellWindowBase();
|
|
backSellButton.SetActive(false);
|
|
}
|
|
|
|
private void SetSellItemsFilter(List<bItemType> newFilter)
|
|
{
|
|
SetItemsFilter(newFilter, ref currentSellFilter, sellWindow);
|
|
}
|
|
|
|
private void OpenSellWindowBase()
|
|
{
|
|
currentSubmenuWindow = sellWeaponsWindow;
|
|
SetSellWindowItemContent();
|
|
if (goldItemReference)
|
|
{
|
|
goldBalanceSellWindowText.text = goldItemReference.amount.ToString() + descriptionNameGold;
|
|
}
|
|
else
|
|
{
|
|
goldBalanceSellWindowText.text = "0" + descriptionNameGold;
|
|
}
|
|
}
|
|
|
|
public void SetSellWindowItemContent()
|
|
{
|
|
backSellButton.SetActive(true);
|
|
bItemManager itemManager = Player.Instance.ItemManager;
|
|
List<bItem> items = itemManager.items;
|
|
sellWindow.CreateEquipmentWindow(items, currentSellFilter);
|
|
}
|
|
|
|
/// <summary>
|
|
/// used on unity button
|
|
/// </summary>
|
|
public void OpenSellWeaponsWindow()
|
|
{
|
|
SetSellItemsFilter(weaponsFilter);
|
|
OpenSellWindowFromMenu();
|
|
}
|
|
|
|
private void OpenSellWindowFromMenu()
|
|
{
|
|
backSellButton.SetActive(true);
|
|
menuScroll.ChangeVisibleCanva(currentSubmenuWindow, sellWeaponsWindow);
|
|
// menuScroll.OnOpened += SetSellWindowContent;
|
|
OpenSellWindowBase();
|
|
}
|
|
|
|
public void OpenSellScrollsWindow()
|
|
{
|
|
SetSellItemsFilter(scrollsFilter);
|
|
OpenSellWindowFromMenu();
|
|
}
|
|
|
|
public void OpenSellConsumablesWindow()
|
|
{
|
|
SetSellItemsFilter(consumablesFilter);
|
|
OpenSellWindowFromMenu();
|
|
}
|
|
|
|
public void OpenSellResourcesWindow()
|
|
{
|
|
SetSellItemsFilter(resourcesFilter);
|
|
OpenSellWindowFromMenu();
|
|
}
|
|
}
|
|
} |