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 ChestDataObject EpicChestData; public bool active => chestOpenPopup.activeSelf; public Animator chestAnim; public GameObject gemsDrop; public GameObject goldDrop; public GameObject skinDrop; public ParticleSystem skinBgParticle; public ParticleSystem confettiFX; public SkinsData skins; public GameObject okButton; void Awake(){ instance = this; } public async void OpenChest(ChestDataObject chestData){ if(chestData == EpicChestData){ AudioManager.instnace.EpicChestOpen(); }else{ AudioManager.instnace.ChestOpen(); } 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.GetSkinIdByName(skin.name) < 0){ 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 if(legendarySkins.Count == 0){ goldCount += 50000; }else{ selectedSkin = legendarySkins[Random.Range(0,legendarySkins.Count)]; } }else if(skinsLucky < chestData.rareChance && rareSkins.Count > 0){ if(rareSkins.Count == 0){ goldCount += 5000; }else{ selectedSkin = rareSkins[Random.Range(0,rareSkins.Count)]; }}else if(baseSkins.Count > 0){ if(baseSkins.Count == 0){ goldCount+= 500; }else{ selectedSkin = baseSkins[Random.Range(0,baseSkins.Count)]; } } } 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); //Chest opens here confettiFX.Play(); 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); } }