106 lines
3.6 KiB
C#
106 lines
3.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine.UI;
|
|
|
|
public class ChestOpener : MonoBehaviour
|
|
{
|
|
public static ChestOpener instance;
|
|
|
|
|
|
public GameObject chestOpenPopup;
|
|
public bool active => chestOpenPopup.activeSelf;
|
|
public Animator chestAnim;
|
|
public GameObject gemsDrop;
|
|
public GameObject goldDrop;
|
|
public GameObject skinDrop;
|
|
public ParticleSystem skinBgParticle;
|
|
|
|
public SkinsData skins;
|
|
|
|
public GameObject okButton;
|
|
void Awake(){
|
|
instance = this;
|
|
}
|
|
|
|
public async void OpenChest(int minLuck, int maxLuck){
|
|
chestOpenPopup.SetActive(true);
|
|
okButton.SetActive(false);
|
|
int luckiness= Random.Range(minLuck,maxLuck);
|
|
List<SkinShopItemData> baseSkins = new List<SkinShopItemData>();
|
|
List<SkinShopItemData> rareSkins = new List<SkinShopItemData>();
|
|
List<SkinShopItemData> legendarySkins = new List<SkinShopItemData>();
|
|
|
|
SkinShopItemData selectedSkin = null;
|
|
int gemsCount = 0;
|
|
int goldCount = 0;
|
|
if(luckiness > 70){
|
|
//Skin is rewarded
|
|
foreach(SkinShopItemData skin in skins.skins){
|
|
if(!DBmanager.SkinsPurchased.Contains(skin.name)){
|
|
switch(skin.skinType){
|
|
case SkinType.Base:
|
|
baseSkins.Add(skin);
|
|
break;
|
|
case SkinType.Rare:
|
|
rareSkins.Add(skin);
|
|
break;
|
|
case SkinType.Legendary:
|
|
legendarySkins.Add(skin);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if(luckiness > 95 && legendarySkins.Count > 0){
|
|
selectedSkin = legendarySkins[Random.Range(0,legendarySkins.Count)];
|
|
}else if(luckiness > 85 && rareSkins.Count > 0){
|
|
selectedSkin = rareSkins[Random.Range(0,rareSkins.Count)];
|
|
}else if(baseSkins.Count > 0){
|
|
selectedSkin = baseSkins[Random.Range(0,baseSkins.Count)];
|
|
}
|
|
}
|
|
|
|
if(luckiness > 50){
|
|
gemsCount = Mathf.CeilToInt((float)luckiness / 20f) * 10;
|
|
}
|
|
goldCount = Mathf.CeilToInt((float)luckiness / 10f) * 1000;
|
|
|
|
goldDrop.SetActive(goldCount > 0);
|
|
gemsDrop.SetActive(gemsCount > 0);
|
|
skinDrop.SetActive(selectedSkin!=null);
|
|
goldDrop.transform.GetComponentInChildren<TMP_Text>().text = goldCount.ToString("0,000");
|
|
gemsDrop.transform.GetComponentInChildren<TMP_Text>().text = gemsCount.ToString();
|
|
|
|
if(selectedSkin!=null){
|
|
skinDrop.transform.GetChild(1).GetComponent<Image>().sprite = selectedSkin.image;
|
|
Color bgColor = new Color(0,1,0);
|
|
if(rareSkins.Contains(selectedSkin)){
|
|
bgColor = new Color(0,1,1);
|
|
}else if(legendarySkins.Contains(selectedSkin)){
|
|
bgColor = new Color(1,0,0);
|
|
}
|
|
|
|
skinBgParticle.startColor = new Color(bgColor.r,bgColor.g,bgColor.b, skinBgParticle.startColor.a);
|
|
}
|
|
|
|
chestAnim.CrossFadeInFixedTime("openAnim",0.1f);
|
|
|
|
while(chestAnim.GetCurrentAnimatorStateInfo(0).IsName("openAnim")){
|
|
await Task.Delay(10);
|
|
}
|
|
|
|
DBmanager.SetGems(DBmanager.Gems + gemsCount);
|
|
DBmanager.SetCoins(DBmanager.Coins + goldCount);
|
|
|
|
if(selectedSkin!=null){
|
|
DBmanager.AddSkin(selectedSkin);
|
|
}
|
|
|
|
|
|
okButton.SetActive(true);
|
|
|
|
}
|
|
}
|