Scriptum/Assets/Scripts/REFACTORING/Application/Shop/ShopItemCardKeeper.cs

189 lines
5.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public enum ShopItemCardMode
{
Buy,
Sell
}
class ShopItemCardKeeper : MonoBehaviour
{
[Header("Current Item")]
public EquippableItem SelectedItem;
public int OriginSlotNumver;
[Header("Shopping Card")]
[SerializeField] private Button _buyButton;
[SerializeField] private Button _sellButton;
[Header("Item Preveiw Section")]
[SerializeField] public ShopItemCardMode ShopMode;
[SerializeField] private TextMeshProUGUI _itemPreviewName;
[SerializeField] private TextMeshProUGUI _itemPreviewDescription;
[SerializeField] private TextMeshProUGUI _itemPreviewStats;
[SerializeField] private TextMeshProUGUI _itemPreviewStatsValue;
[SerializeField] private Text _itemPreviewPrice;
public void ShowItemDetails(ItemSlot itemSlot, ShopItemCardMode mode = ShopItemCardMode.Buy)
{
SelectedItem = itemSlot.Item;
OriginSlotNumver = itemSlot.Number;
ShopMode = mode;
gameObject.active = true;
BildCardContent();
}
public void BildCardContent()
{
// Clear poanel
if (!SelectedItem)
{
_itemPreviewName.text = "";
_itemPreviewDescription.text = "";
_itemPreviewStats.text = "";
_itemPreviewStatsValue.text = "";
_itemPreviewPrice.text = "";
_buyButton.gameObject.SetActive(false);
_sellButton.gameObject.SetActive(false);
return;
}
_itemPreviewName.text = SelectedItem.Name;
_itemPreviewDescription.text = SelectedItem.Description;
_itemPreviewStats.text = BuildItemStats();
_itemPreviewStatsValue.text = SelectedItem.Value.ToString();
_itemPreviewPrice.text = $"Price: {CountPrice()}";
switch(ShopMode)
{
case ShopItemCardMode.Buy:
{
_sellButton.gameObject.SetActive(false);
_buyButton.gameObject.SetActive(true);
break;
}
case ShopItemCardMode.Sell:
{
_buyButton.gameObject.SetActive(false);
_sellButton.gameObject.SetActive(true);
break;
}
}
}
public string BuildItemStats()
{
if(SelectedItem.EquipmentType == EquipmentTypeEnum.Weapon)
return $"Attack: \n";
if (SelectedItem.EquipmentType == EquipmentTypeEnum.Helmet || SelectedItem.EquipmentType == EquipmentTypeEnum.Chest)
return $"Deffence: \n";
if (SelectedItem.EquipmentType == EquipmentTypeEnum.Boots)
return $"Speed: \n";
return "";
}
public int CountPrice()
{
return SelectedItem.Price;
}
public void BuyItem()
{
if (!_buyButton.IsActive())
return;
2022-12-24 15:14:41 +01:00
var itemPrice = SelectedItem.Price;
// Check player account balance
if (!CheckPlayerAccoutnBalance(itemPrice))
{
Debug.Log($"You don't have enouht gold - required {itemPrice}");
return;
}
// Change account balance status (refresh view)
AccountBalanceManager.Instance.DecreaseAccountBalanceValue(SelectedItem.Price);
// 1. Buy transaction
// 1.1.1 Add to player Inventory Panel + refresh view
InventoryUIManager.Instance.Add(SelectedItem);
if (InventoryUIManager.Instance.GetPanelStatus())
GameObject.FindObjectOfType<InventoryPanelController>().BuildPanelContent(InventoryUIManager.Instance.GetList());
// 1.1.2 Save chnages in Inventory Panel
SceneInventoryDataManager.Instance.SaveDynamicData();
// 1.2.1 Remove item from Shop Panel
ShopContentUIManager.Instance.RemoveByPosition(OriginSlotNumver);
if (ShopUIManager.Instance.GetPanelStatus())
GameObject.FindObjectOfType<ShopPanelController>().BuildPanelContent(ShopContentUIManager.Instance.GetList());
2022-12-24 15:14:41 +01:00
// 2 Save changes
2022-12-24 15:14:41 +01:00
// 2.1 Save chnages in Shop Panel
SceneInventoryDataManager.Instance.SaveDynamicData();
2022-12-24 15:14:41 +01:00
// 2.2 Save account balance current status
AccountBalanceManager.Instance.SaveValue();
}
public void SellItem()
{
if (!_sellButton.IsActive())
return;
// 1. Sell transaction
// 1.1.1 Remove from player Inventory Panel by position + refresh view
InventoryUIManager.Instance.RemoveByPosition(OriginSlotNumver);
if (InventoryUIManager.Instance.GetPanelStatus())
GameObject.FindObjectOfType<InventoryPanelController>().BuildPanelContent(InventoryUIManager.Instance.GetList());
2022-12-24 15:14:41 +01:00
// 2. Change account balance status (refresh view)
AccountBalanceManager.Instance.IncreaseAccountBalanceValue(SelectedItem.Price);
// 3 Save changes
// 3.1 Save chnages in Shop Panel
SceneInventoryDataManager.Instance.SaveDynamicData();
2022-12-24 15:14:41 +01:00
// 3.2 Save account balance current status
AccountBalanceManager.Instance.SaveValue();
}
2022-12-24 15:14:41 +01:00
public bool CheckPlayerAccoutnBalance(int requiredAmount)
{
return AccountBalanceManager.Instance.Gold > requiredAmount;
}
}