105 lines
2.8 KiB
C#
105 lines
2.8 KiB
C#
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 string ChestName => txtChestName.text;
|
|
public int Price;
|
|
public float minLuck = 0;
|
|
public float maxLuck = 100;
|
|
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(ChestName, $"This chest will drop following items.\n\n{getItemsProbability()}");
|
|
}
|
|
|
|
void OnClick(){
|
|
MessageDialog.instance.ShowQuestion("Are you sure?", $"Are you sure to purchase {ChestName} 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((int)minLuck, (int)maxLuck);
|
|
}
|
|
|
|
public void BuyFree(){
|
|
// MessageDialog.instance.ShowMessage("Voila!", "Here's your gift!");
|
|
StartCoroutine(buyFree());
|
|
}
|
|
|
|
IEnumerator buyFree(){
|
|
yield return new WaitForSeconds(1);
|
|
ChestOpener.instance.OpenChest((int)minLuck, (int)maxLuck);
|
|
}
|
|
|
|
public string getItemsProbability()
|
|
{
|
|
string items = "Gold";
|
|
|
|
if (maxLuck > 50)
|
|
{
|
|
float probability = ((maxLuck - 50f) / (maxLuck - minLuck)) * 100f;
|
|
items += $"\nGems : {probability.ToString("n1")}%";
|
|
}
|
|
|
|
if (maxLuck > 70)
|
|
{
|
|
//some skins
|
|
float probability = ((maxLuck - 70f) / (maxLuck - minLuck)) * 100f;
|
|
items += $"\nCommon Skin : {probability.ToString("n1")}%";
|
|
}
|
|
if (maxLuck > 85)
|
|
{
|
|
float probability = ((maxLuck - 85f) / (maxLuck - minLuck)) * 100f;
|
|
items += $"\nRare Skin : {probability.ToString("n1")}%";
|
|
}
|
|
if (maxLuck > 95)
|
|
{
|
|
float probability = ((maxLuck - 95f) / (maxLuck - minLuck)) * 100f;
|
|
items += $"\nLegendary Skin : {probability.ToString("n1")}%";
|
|
}
|
|
|
|
|
|
return items;
|
|
}
|
|
|
|
void OnValidate()
|
|
{
|
|
infoTxt = getItemsProbability();
|
|
|
|
if (!readFromTexts) { return; }
|
|
|
|
if (txtPrice != null)
|
|
{
|
|
Price = int.Parse(txtPrice.text);
|
|
}
|
|
}
|
|
}
|