149 lines
5.7 KiB
C#
Executable File
149 lines
5.7 KiB
C#
Executable File
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
public class XpPass : MonoBehaviour
|
|
{
|
|
public GameObject panel;
|
|
public TMP_Text nextLevelTxt;
|
|
public GameObject newRewardsNotification;
|
|
public float sliderWidthPerLevel = 450;
|
|
public Slider levelSlider;
|
|
|
|
public XpPassReward[] rewards => rewardsData.rewards;
|
|
public XPRewards rewardsData;
|
|
public Sprite goldRewardIcon,energyRewardIcon, gemRewardIcon, chestRewardIcon;
|
|
public GameObject rewardCardPrefab;
|
|
public GameObject xpLevelPointPrefab;
|
|
public Sprite disabledXpPointIcon;
|
|
public Transform xpLevelPointsParent, rewardCardsParent;
|
|
void Start()
|
|
{
|
|
Refresh();
|
|
}
|
|
|
|
public void Show(){
|
|
Refresh();
|
|
panel.SetActive(true);
|
|
}
|
|
|
|
public void Hide(){
|
|
panel.SetActive(false);
|
|
}
|
|
|
|
void Refresh(){
|
|
nextLevelTxt.text = (DBmanager.LevelInt +1).ToString();
|
|
int rewardsLeftToCollect= 0;
|
|
//Sort Rewards by Level
|
|
XpPassReward temp;
|
|
for (int j = 0; j <= rewards.Length - 2; j++) {
|
|
for (int i = 0; i <= rewards.Length - 2; i++) {
|
|
if (rewards[i].level > rewards[i + 1].level) {
|
|
temp= rewards[i + 1];
|
|
rewards[i + 1] = rewards[i];
|
|
rewards[i] = temp;
|
|
}
|
|
}
|
|
}
|
|
|
|
//Clear current items
|
|
rewardCardPrefab.SetActive(false); xpLevelPointPrefab.SetActive(false);
|
|
for(int i=0; i < xpLevelPointsParent.childCount;i++){
|
|
if(xpLevelPointsParent.GetChild(i)== xpLevelPointPrefab.transform){continue;}
|
|
Destroy(xpLevelPointsParent.GetChild(i).gameObject);
|
|
}
|
|
for(int i=0; i < rewardCardsParent.childCount;i++){
|
|
if(rewardCardsParent.GetChild(i) == rewardCardPrefab.transform){continue;}
|
|
Destroy(rewardCardsParent.GetChild(i).gameObject);
|
|
}
|
|
rewardCardPrefab.SetActive(true); xpLevelPointPrefab.SetActive(true);
|
|
|
|
|
|
Instantiate(xpLevelPointPrefab, xpLevelPointsParent).GetComponentInChildren<TMP_Text>().text="1";
|
|
foreach(XpPassReward reward in rewards){
|
|
GameObject rewardCard = Instantiate(rewardCardPrefab, rewardCardsParent);
|
|
GameObject xpLevelPoint = Instantiate(xpLevelPointPrefab, xpLevelPointsParent);
|
|
|
|
xpLevelPoint.GetComponentInChildren<TMP_Text>().text = reward.level.ToString();
|
|
|
|
rewardCard.GetComponentInChildren<TMP_Text>().text = (reward.amount >=1000) ? reward.amount.ToString("0,000") : reward.amount.ToString();
|
|
switch(reward.rewardType){
|
|
case XpRewardType.Gold:
|
|
rewardCard.transform.GetChild(1).GetComponent<Image>().sprite = goldRewardIcon;
|
|
break;
|
|
case XpRewardType.Energy:
|
|
rewardCard.transform.GetChild(1).GetComponent<Image>().sprite = energyRewardIcon;
|
|
break;
|
|
case XpRewardType.Gems:
|
|
rewardCard.transform.GetChild(1).GetComponent<Image>().sprite = gemRewardIcon;
|
|
break;
|
|
case XpRewardType.Chest:
|
|
rewardCard.transform.GetChild(1).GetComponent<Image>().sprite = chestRewardIcon;
|
|
rewardCard.GetComponentInChildren<TMP_Text>().text = "";
|
|
break;
|
|
}
|
|
|
|
if(reward.level > DBmanager.Level){
|
|
xpLevelPoint.GetComponent<Image>().sprite = disabledXpPointIcon;
|
|
rewardCard.GetComponent<Button>().interactable=false;
|
|
}
|
|
|
|
//If Collected already
|
|
if(DBmanager.ExpPassCollected.Contains(reward.level)){
|
|
rewardCard.GetComponent<Button>().interactable=false;
|
|
rewardCard.transform.GetChild(3).gameObject.SetActive(true);
|
|
}
|
|
if(rewardCard.GetComponent<Button>().interactable){
|
|
rewardsLeftToCollect++;
|
|
}
|
|
rewardCard.GetComponent<Button>().onClick.AddListener(()=>OnRewardCollect(reward.level));
|
|
}
|
|
levelSlider.GetComponent<RectTransform>().sizeDelta = new Vector2(sliderWidthPerLevel * rewards.Length-2 ,levelSlider.GetComponent<RectTransform>().sizeDelta.y);
|
|
rewardCardPrefab.SetActive(false); xpLevelPointPrefab.SetActive(false);
|
|
levelSlider.value = DBmanager.Level / (float)rewards[rewards.Length-1].level;
|
|
|
|
//Set notification
|
|
newRewardsNotification.SetActive(rewardsLeftToCollect>0);
|
|
newRewardsNotification.GetComponentInChildren<TMP_Text>().text = rewardsLeftToCollect.ToString();
|
|
}
|
|
|
|
public void OnRewardCollect(int level){
|
|
if(DBmanager.ExpPassCollected.Contains(level)){return;} //False call
|
|
AudioManager.instnace.Reward();
|
|
foreach(XpPassReward reward in rewards){
|
|
if(reward.level == level){
|
|
DBmanager.AddCollectedExpPass(level);
|
|
if(reward.rewardType== XpRewardType.Gold){
|
|
DBmanager.SetCoins(DBmanager.Coins+ reward.amount);
|
|
}else if(reward.rewardType == XpRewardType.Gems){
|
|
DBmanager.SetGems(DBmanager.Gems + reward.amount);
|
|
}else if(reward.rewardType == XpRewardType.Chest){
|
|
// StartCoroutine(destroyTimer(Instantiate(chestPrefab, chestSpawnParent),5));
|
|
ChestOpener.instance.OpenChest(reward.chestData);
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
Refresh();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
[System.Serializable]
|
|
public class XpPassReward{
|
|
string name => level.ToString();
|
|
public int level;
|
|
public XpRewardType rewardType;
|
|
public int amount;
|
|
public ChestDataObject chestData;
|
|
}
|
|
|
|
public enum XpRewardType{
|
|
Gold,
|
|
Gems,
|
|
Chest,
|
|
Energy
|
|
} |