Chests done
This commit is contained in:
104
Assets/Game/Scripts/ChestOpener.cs
Normal file
104
Assets/Game/Scripts/ChestOpener.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
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 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(){
|
||||
chestOpenPopup.SetActive(true);
|
||||
okButton.SetActive(false);
|
||||
int luckiness= Random.Range(0,100);
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Game/Scripts/ChestOpener.cs.meta
Normal file
11
Assets/Game/Scripts/ChestOpener.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1ef65e4d176238e7fa103bbb9666983a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -334,6 +334,10 @@ public class DBmanager : MonoBehaviour
|
||||
return;
|
||||
}
|
||||
SetMetal(metal-data.price);
|
||||
AddSkin(data);
|
||||
}
|
||||
|
||||
public static void AddSkin(SkinShopItemData data){
|
||||
skinsPurchased.Add(data.name);
|
||||
UpdatePurchasedSkins();
|
||||
}
|
||||
|
||||
@@ -15,9 +15,6 @@ public class XpPass : MonoBehaviour
|
||||
public GameObject xpLevelPointPrefab;
|
||||
public Sprite disabledXpPointIcon;
|
||||
public Transform xpLevelPointsParent, rewardCardsParent;
|
||||
|
||||
public GameObject chestPrefab;
|
||||
public Transform chestSpawnParent;
|
||||
void Start()
|
||||
{
|
||||
Refresh();
|
||||
@@ -109,7 +106,8 @@ public class XpPass : MonoBehaviour
|
||||
}else if(reward.rewardType == XpRewardType.Gems){
|
||||
DBmanager.SetGems(DBmanager.Gems + reward.amount);
|
||||
}else if(reward.rewardType == XpRewardType.Chest){
|
||||
StartCoroutine(destroyTimer(Instantiate(chestPrefab, chestSpawnParent),5));
|
||||
// StartCoroutine(destroyTimer(Instantiate(chestPrefab, chestSpawnParent),5));
|
||||
ChestOpener.instance.OpenChest();
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -118,10 +116,6 @@ public class XpPass : MonoBehaviour
|
||||
Refresh();
|
||||
}
|
||||
|
||||
IEnumerator destroyTimer(GameObject item, float time){
|
||||
yield return new WaitForSeconds(time);
|
||||
Destroy(item);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user