160 lines
5.3 KiB
C#
Executable File
160 lines
5.3 KiB
C#
Executable File
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 chestAnimLucky;
|
|
public Animator chestAnimEpic;
|
|
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;
|
|
|
|
okButton.GetComponent<Button>().onClick.AddListener(OnOkButton);
|
|
}
|
|
|
|
void OnOkButton(){
|
|
chestOpenPopup.SetActive(false);
|
|
chestAnimLucky.gameObject.SetActive(false);
|
|
chestAnimEpic.gameObject.SetActive(false);
|
|
}
|
|
|
|
public async void OpenChest(ChestDataObject chestData){
|
|
|
|
|
|
chestOpenPopup.SetActive(true);
|
|
okButton.SetActive(false);
|
|
|
|
List<SkinShopItemData> baseSkins = new List<SkinShopItemData>();
|
|
List<SkinShopItemData> rareSkins = new List<SkinShopItemData>();
|
|
List<SkinShopItemData> legendarySkins = new List<SkinShopItemData>();
|
|
|
|
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<TMP_Text>().text = goldCount.ToString();
|
|
gemsDrop.transform.GetComponentInChildren<TMP_Text>().text = gemsCount.ToString();
|
|
|
|
if(selectedSkin!=null){
|
|
skinDrop.transform.GetChild(1).GetComponent<Image>().sprite = selectedSkin.image;
|
|
Color bgColor = SkinShopManager.getRarityColor(selectedSkin);
|
|
|
|
skinBgParticle.startColor = new Color(bgColor.r,bgColor.g,bgColor.b, skinBgParticle.startColor.a);
|
|
}
|
|
|
|
|
|
if(chestData.name.ToLower().Contains("epic")){
|
|
chestAnimEpic.CrossFadeInFixedTime("openAnim",0.1f);
|
|
chestAnimEpic.gameObject.SetActive(true);
|
|
}else{
|
|
chestAnimLucky.CrossFadeInFixedTime("openAnim",0.1f);
|
|
chestAnimLucky.gameObject.SetActive(true);
|
|
|
|
}
|
|
|
|
|
|
//Chest opens here
|
|
confettiFX.Play();
|
|
StartCoroutine(PlaySFX(chestData == EpicChestData));
|
|
Debug.Log("Chest opening");
|
|
// while (chestAnim.GetCurrentAnimatorStateInfo(0).IsName("openAnim")){
|
|
// await Task.Delay(10);
|
|
// }
|
|
StartCoroutine(EnableOkayButton());
|
|
Debug.Log("Chest opened");
|
|
|
|
DBmanager.SetGems(DBmanager.Gems + gemsCount);
|
|
DBmanager.SetCoins(DBmanager.Coins + goldCount);
|
|
|
|
if(selectedSkin!=null){
|
|
DBmanager.AddSkin(selectedSkin);
|
|
}
|
|
|
|
|
|
okButton.SetActive(true);
|
|
|
|
}
|
|
|
|
IEnumerator PlaySFX(bool isEpic){
|
|
yield return new WaitForSeconds(1f);
|
|
|
|
if(isEpic){
|
|
AudioManager.instnace.EpicChestOpen();
|
|
}else{
|
|
AudioManager.instnace.ChestOpen();
|
|
}
|
|
yield return new WaitForSeconds(0.8f);
|
|
confettiFX.GetComponent<AudioSource>().Play();
|
|
|
|
}
|
|
|
|
IEnumerator EnableOkayButton(){
|
|
yield return new WaitForSeconds(1.5f);
|
|
|
|
okButton.SetActive(true);
|
|
}
|
|
}
|