UPF/Assets/Game/Scripts/Shop/ChestButton.cs
2023-02-24 22:14:55 +05:30

75 lines
2.0 KiB
C#
Executable File

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ChestButton : MonoBehaviour
{
public bool IsSpecial = false;
public bool readFromTexts;
public TMP_Text txtPrice;
public TMP_Text txtChestName;
public ChestDataObject chestData;
public int Price;
public bool overridePrice;
public Button btnInfo;
public Button adButton;
[TextArea]
public string infoTxt;
void Start()
{
btnInfo.onClick.AddListener(OnClickedInfo);
if(!IsSpecial){GetComponent<Button>().onClick.AddListener(OnClick);adButton.onClick.AddListener(OnAdClicked);}
}
void OnClickedInfo()
{
MessageDialog.instance.ShowMessage(chestData.name, $"This chest will drop following items.\n\n{chestData.getInfo()}");
}
void OnClick(){
MessageDialog.instance.ShowQuestion("Are you sure?", $"Are you sure to purchase {chestData.name} for {Price} Gems?", Buy,null);
}
void OnAdClicked(){
Debug.Log("Showing ad");
AdsManager.instance.ShowRewarded(BuyFree);
}
void Buy(){
if(DBmanager.Gems < Price){
MessageDialog.instance.ShowMessage("Failed","Insufficient Gems to complete the Purchase!");
return;
}
DBmanager.SetGems(DBmanager.Gems - Price);
ChestOpener.instance.OpenChest(chestData);
}
public void BuyFree(){
// MessageDialog.instance.ShowMessage("Voila!", "Here's your gift!");
StartCoroutine(buyFree());
}
IEnumerator buyFree(){
yield return new WaitForSeconds(1);
ChestOpener.instance.OpenChest(chestData);
}
void OnValidate()
{
infoTxt = chestData.getInfo();
if(!overridePrice){Price=chestData.Price;}
txtPrice.text = Price.ToString();
txtChestName.text = chestData.name;
if (!readFromTexts) { return; }
// if (txtPrice != null)
// {
// Price = int.Parse(txtPrice.text);
// }
}
}