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(ChestDataObject chestData){ chestOpenPopup.SetActive(true); okButton.SetActive(false); List baseSkins = new List(); List rareSkins = new List(); List legendarySkins = new List(); SkinShopItemData selectedSkin = null; float gemLucky = Random.Range(0,100f); float goldLucky = Random.Range(0,100f); int gemsCount = (gemLucky < chestData.gemsChance) ? (int)(chestData.minGems + ((chestData.maxGems - chestData.minGems)*(gemLucky/100f) )) : 0; int goldCount = (int)(chestData.minGold + ((chestData.maxGold - chestData.minGold)*(goldLucky/100f) )); float skinsLucky = Random.Range(0,100f); if(skinsLucky < chestData.commonChance){ //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(skinsLucky < chestData.legendaryChance && legendarySkins.Count > 0){ //Legend selectedSkin = legendarySkins[Random.Range(0,legendarySkins.Count)]; goldCount += 50000; }else if(skinsLucky < chestData.rareChance && rareSkins.Count > 0){ selectedSkin = rareSkins[Random.Range(0,rareSkins.Count)]; goldCount += 5000; }else if(baseSkins.Count > 0){ selectedSkin = baseSkins[Random.Range(0,baseSkins.Count)]; goldCount+= 500; } } goldDrop.SetActive(goldCount > 0); gemsDrop.SetActive(gemsCount > 0); skinDrop.SetActive(selectedSkin!=null); goldDrop.transform.GetComponentInChildren().text = goldCount.ToString(); gemsDrop.transform.GetComponentInChildren().text = gemsCount.ToString(); if(selectedSkin!=null){ skinDrop.transform.GetChild(1).GetComponent().sprite = selectedSkin.image; Color bgColor = SkinShopManager.getRarityColor(selectedSkin); 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); } }