UPF/Assets/Game/Scripts/Shop/ChestButton.cs
2022-09-20 00:16:35 +05:30

85 lines
2.2 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;
[TextArea]
public string infoTxt;
void Start()
{
btnInfo.onClick.AddListener(OnClickedInfo);
if(!IsSpecial)GetComponent<Button>().onClick.AddListener(OnClick);
}
void OnClickedInfo()
{
MessageDialog.instance.ShowDialog(ChestName, $"This chest will drop following items.\n\n{getItemsProbability()}");
}
void OnClick(){
if(DBmanager.Gems < Price){
MessageDialog.instance.ShowDialog("Failed","Insufficient Gems to complete the Purchase!");
return;
}
DBmanager.SetGems(DBmanager.Gems - Price);
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);
}
}
}