You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

151 lines
3.8 KiB

using System;
using Equip;
using UnityEngine;
using TMPro;
namespace Shop
{
public enum ShopType
{
Coins,
Gems,
Items
}
public enum BuyType
{
Gems,
Real,
Free,
}
public class ShopItem : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI _nameLabel;
[SerializeField] private GameObject _adsIcon;
[SerializeField] private TextMeshProUGUI _timer;
[SerializeField] private string _sku;
[SerializeField] private ShopType _type;
[SerializeField] private int _count;
[SerializeField] private BuyType _buyType;
[SerializeField] private int _cost;
[SerializeField] private int _nameTextId;
[SerializeField] private TextMeshProUGUI _itemCount;
[SerializeField] private TextMeshProUGUI _itemPrice;
public static event Action EquipBuy;
private void Start()
{
InitItemInfo();
InitTexts();
}
private void InitTexts()
{
if (_nameLabel)
_nameLabel.text = GlobalsVar.gGameTextMng.GetGameText(_nameTextId);
}
private void InitItemInfo()
{
if (_buyType == BuyType.Free)
{
InitAdsItem();
}
else
{
if (_type != ShopType.Items)
{
StoreOffersInfo info = CommonFunctions.GetRealPurchaseBySKU(_sku);
if (info == null)
return;
_itemCount.text = info.m_count.ToString();
_itemPrice.text = info.m_price.ToString();
}
}
}
public void InitAdsItem()
{
if (GlobalsVar.gCurrentTimeToFreeStoreCoins > 0 || !CommonFunctions.IsRewardedAdCanBeShown())
{
CanvasGroup cg = gameObject.GetComponent<CanvasGroup>();
cg.alpha = 0.5f;
_timer.gameObject.SetActive(true);
if (GlobalsVar.gCurrentTimeToFreeStoreCoins > 0)
{
_adsIcon.SetActive(false);
_timer.text = CommonFunctions.GetTimeStr((int)GlobalsVar.gCurrentTimeToFreeStoreCoins);
}
else
{
_adsIcon.SetActive(true);
_timer.gameObject.SetActive(false);
}
}
else
{
_adsIcon.SetActive(true);
_timer.gameObject.SetActive(false);
}
}
public void SetCurrentTime(float time)
{
_timer.text = CommonFunctions.GetTimeStr((int)time);
}
public void BuyItem()
{
if (_buyType == BuyType.Real)
{
Proceed();
return;
}
if (_buyType == BuyType.Free)
{
if (GlobalsVar.gCurrentTimeToFreeStoreCoins > 0f)
return;
if (CommonFunctions.IsRewardedAdCanBeShown())
CommonFunctions.StartShowAd();
return;
}
if (_cost <= GlobalsVar.gUser.Crystals)
{
GlobalsVar.gUser.IncCurrency(ShopType.Gems, -_cost);
Proceed();
}
}
private void Proceed()
{
switch (_type)
{
case ShopType.Coins:
GlobalsVar.gPurchaser.BuyConsumable(_sku);
break;
case ShopType.Gems:
GlobalsVar.gPurchaser.BuyConsumable(_sku);
break;
case ShopType.Items:
GameObject dlg = GlobalsVar.gBoard.StartDialog("StoreSafeDialog");
dlg.GetComponent<StoreSafeDialog>().Init();
if (EquipBuy != null)
EquipBuy();
break;
}
MainMenu mainMenu = GlobalsVar.gBoard as MainMenu;
if (mainMenu != null)
mainMenu.Repaint();
}
}
}