UPF/Assets/Game/Scripts/DesignLab.cs
2023-02-24 22:14:55 +05:30

328 lines
13 KiB
C#
Executable File

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class DesignLab : MonoBehaviour
{
[Header("Box opening")]
public Building designLabBuilding;
public Button btnCommonBox,btnRareBox,btnEpicBox,btnLegendaryBox;
public int[] boxPrices;
public GameObject[] AllOwnedTooltips;
public GameObject newBoxPanel;
public GameObject newSkinPanel;
public GameObject goldsPanel;
public Button btnOpen;
public ParticleSystem confettiFX;
public ParticleSystem[] fullscreenFX;
public Button[] slips;
[Header("Blueprints")]
public GameObject blueprintsPanel;
public GameObject noBlueprintsPanel;
public Transform gridParent;
public GameObject gridChildPrefab;
public Transform selectedCard;
public int[] buildGoldPrices,buildMetalPrices;
public Button btn_build;
void Start()
{
btn_build.onClick.AddListener(OnBuildClicked);
btnCommonBox.onClick.AddListener(()=>{OnBoxSelected(0);});
btnRareBox.onClick.AddListener(()=>{OnBoxSelected(1);});
btnEpicBox.onClick.AddListener(()=>{OnBoxSelected(2);});
btnLegendaryBox.onClick.AddListener(()=>{OnBoxSelected(3);});
btnOpen.onClick.AddListener(OnOpen);
foreach(Button btn in slips){
btn.onClick.AddListener(OnSlipSelected);
}
Refresh();
}
public int selectedBoxIndex = -1;
int selectedBlueprintIndex= 0;
public void OnBoxSelected(int index){
selectedBoxIndex = index;
Refresh();
AudioManager.instnace.UIClick();
}
void Refresh()
{
RefreshBoxes();
RefreshBlueprints();
}
void RefreshBlueprints(){
for(int i=0; i < gridParent.childCount; i++){ //PURGE
Destroy(gridParent.GetChild(i).gameObject);
}
List<PurchasedSkin> blueprints = DBmanager.Blueprints;
if(blueprints.Count <= 0){
blueprintsPanel.SetActive(false);
noBlueprintsPanel.SetActive(true);
return;
}
blueprintsPanel.SetActive(true);
noBlueprintsPanel.SetActive(false);
blueprintItems = new List<GameObject>();
//Populate
for(int i =0; i < blueprints.Count; i++){
GameObject newItem = Instantiate(gridChildPrefab, gridParent);
SkinShopItemData skinData = SkinShopManager.GetSkinDataByName(blueprints[i].name);
newItem.transform.GetComponentInChildren<TMP_Text>().text = SkinShopManager.GetRarityName(skinData.skinType);
newItem.transform.GetComponentInChildren<TMP_Text>().color = SkinShopManager.GetRarityColor(skinData.skinType);
newItem.transform.Find("img_skin").GetComponent<Image>().sprite = skinData.image;
newItem.name = blueprints[i].name;
int index = i;
newItem.GetComponent<Button>().onClick.AddListener(()=>{OnBlueprintItemClicked(index);});
blueprintItems.Add(newItem);
}
OnBlueprintItemClicked(selectedBlueprintIndex);
}
List<GameObject> blueprintItems = new List<GameObject>();
void RefreshBoxes(){
// OnBoxSelected(-1);
List<SkinShopItemData> availableSkins0 = new List<SkinShopItemData>();
List<SkinShopItemData> availableSkins1 = new List<SkinShopItemData>();
List<SkinShopItemData> availableSkins2 = new List<SkinShopItemData>();
List<SkinShopItemData> availableSkins3 = new List<SkinShopItemData>();
foreach(SkinShopItemData item in SkinShopManager.SkinsData.skins){
if(DBmanager.GetSkinIdByName(item.name) >=0){continue;}
if(item.skinType == SkinType.Base){
availableSkins0.Add(item);
}else if( item.skinType == SkinType.Rare){
availableSkins1.Add(item);
}else if(item.skinType == SkinType.Epic){
availableSkins2.Add(item);
}else if( item.skinType == SkinType.Legendary){
availableSkins3.Add(item);
}
}
AllOwnedTooltips[0].SetActive(availableSkins0.Count <= 0);
AllOwnedTooltips[1].SetActive(availableSkins1.Count <= 0);
AllOwnedTooltips[2].SetActive(availableSkins2.Count <= 0);
AllOwnedTooltips[3].SetActive(availableSkins3.Count <= 0);
btnCommonBox.transform.GetChild(1).gameObject.SetActive(false);
btnRareBox.transform.GetChild(1).gameObject.SetActive(false);
btnEpicBox.transform.GetChild(1).gameObject.SetActive(false);
btnLegendaryBox.transform.GetChild(1).gameObject.SetActive(false);
switch(selectedBoxIndex){
case 0:
btnCommonBox.transform.GetChild(1).gameObject.SetActive(true);
break;
case 1:
btnRareBox.transform.GetChild(1).gameObject.SetActive(true);
break;
case 2:
btnEpicBox.transform.GetChild(1).gameObject.SetActive(true);
break;
case 3:
btnLegendaryBox.transform.GetChild(1).gameObject.SetActive(true);
break;
}
if(selectedBoxIndex <0 || selectedBoxIndex >= boxPrices.Length){ //Non Selected
btnOpen.gameObject.SetActive(false);
return;
}
btnOpen.gameObject.SetActive(true);
btnOpen.transform.Find("txt_metal").GetComponent<TMP_Text>().text = boxPrices[selectedBoxIndex].ToString();
btnOpen.transform.Find("txt_metal").GetComponent<TMP_Text>().color = (DBmanager.Metal >= boxPrices[selectedBoxIndex]) ? Color.white : Color.red;
btnOpen.interactable = DBmanager.Metal >= boxPrices[selectedBoxIndex] && designLabBuilding.curLevel >= selectedBoxIndex;
}
public void Show(){
selectedBoxIndex=-1;
gameObject.SetActive(true);
Refresh();
}
void OnBlueprintItemClicked(int index){
selectedBlueprintIndex = index;
Debug.Log("selecting item " + index);
SkinShopItemData skin = SkinShopManager.GetSkinDataByName(blueprintItems[index].name);
selectedCard.Find("img_skin").GetComponent<Image>().sprite = skin.image;
selectedCard.Find("rate_common").gameObject.SetActive(skin.skinType == SkinType.Base);
selectedCard.Find("rate_rare").gameObject.SetActive(skin.skinType == SkinType.Rare);
selectedCard.Find("rate_epic").gameObject.SetActive(skin.skinType == SkinType.Epic);
selectedCard.Find("rate_legendary").gameObject.SetActive(skin.skinType == SkinType.Legendary);
RocketLevel stats = SkinShopManager.GetStatsForRarity(skin.skinType)[0];
selectedCard.Find("txt_energy").GetChild(0).GetComponent<TMP_Text>().text = stats.moonMultiplier.ToString();
selectedCard.Find("txt_speed").GetChild(0).GetComponent<TMP_Text>().text = (stats.speedMultiplier * 100).ToString("n0") + "%";
selectedCard.Find("txt_boost").GetChild(0).GetComponent<TMP_Text>().text = (stats.boostConsumption * 100).ToString("n0") + "%";
int rarityIndex = SkinShopManager.GetRarityIndexFromName(blueprintItems[index].name);
btn_build.transform.GetChild(0).Find("txt_metal").GetComponent<TMP_Text>().text = buildMetalPrices[rarityIndex].ToString();
btn_build.transform.GetChild(0).Find("txt_metal").gameObject.SetActive(buildMetalPrices[rarityIndex] > 0);
btn_build.transform.GetChild(0).Find("txt_gold").GetComponent<TMP_Text>().text = buildGoldPrices[rarityIndex].ToString();
btn_build.transform.GetChild(0).Find("txt_gold").gameObject.SetActive(buildGoldPrices[rarityIndex] > 0);
btn_build.interactable = DBmanager.Coins >= buildGoldPrices[rarityIndex] && DBmanager.Metal >= buildMetalPrices[rarityIndex];
}
void OnBuildClicked(){
int rarityIndex = SkinShopManager.GetRarityIndexFromName(blueprintItems[selectedBlueprintIndex].name);
if(DBmanager.Coins >= buildGoldPrices[rarityIndex] && DBmanager.Metal >= buildMetalPrices[rarityIndex]){
string message = $@"Are you sure you want to build this skin for {buildGoldPrices[rarityIndex]} Golds
and {buildMetalPrices[rarityIndex]} Energy?";
MessageDialog.instance.ShowQuestion("Confirm",message, OnYes:OnBuildConfirmed, OnNo:()=>{});
}else{
MessageDialog.instance.ShowMessage("Error", "Not enough resources to build this blueprint");
}
Refresh();
}
void OnBuildConfirmed(){
int rarityIndex = SkinShopManager.GetRarityIndexFromName(blueprintItems[selectedBlueprintIndex].name);
if(DBmanager.Coins >= buildGoldPrices[rarityIndex] && DBmanager.Metal >= buildMetalPrices[rarityIndex]){
DBmanager.SetCoins(DBmanager.Coins - buildGoldPrices[rarityIndex]);
DBmanager.SetMetal(DBmanager.Metal - buildMetalPrices[rarityIndex]);
DBmanager.BuildBlueprint(blueprintItems[selectedBlueprintIndex].name);
AudioManager.instnace.BuildRocket();
}else{
MessageDialog.instance.ShowMessage("Error", "Not enough resources to build this blueprint");
}
//Fullscreen FX
foreach (ParticleSystem particle in fullscreenFX)
{
particle.Play();
}
Refresh();
}
public void OnOpen(){
if(DBmanager.Metal < boxPrices[selectedBoxIndex]){
MessageDialog.instance.ShowMessage("Error", "Not enough energy to open this box");
return;
}
DBmanager.SetMetal(DBmanager.Metal-boxPrices[selectedBoxIndex]);
AudioManager.instnace.OpenBP();
newBoxPanel.SetActive(true);
string rarityName = "Common";
if(selectedBoxIndex == 1){
rarityName = "Rare";
}else if(selectedBoxIndex == 2){
rarityName = "Epic";
}else if(selectedBoxIndex ==3){
rarityName = "Legendary";
}
newBoxPanel.transform.GetChild(0).GetComponent<TMP_Text>().text = rarityName + " Box";
}
public void OnSlipSelected(){
int random = Random.Range(0,100);
if(random < 20){
//Got a skin
List<SkinShopItemData> availableSkins = new List<SkinShopItemData>();
foreach(SkinShopItemData item in SkinShopManager.SkinsData.skins){
if(DBmanager.GetSkinIdByName(item.name) >=0){continue;}
if(selectedBoxIndex ==0 && item.skinType == SkinType.Base){
availableSkins.Add(item);
}else if(selectedBoxIndex ==1 && item.skinType == SkinType.Rare){
availableSkins.Add(item);
}else if(selectedBoxIndex ==2 && item.skinType == SkinType.Epic){
availableSkins.Add(item);
}else if(selectedBoxIndex ==3 && item.skinType == SkinType.Legendary){
availableSkins.Add(item);
}
}
if(availableSkins.Count <= 0){
Debug.LogError("No skin found to award");
int amount = (int)(boxPrices[selectedBoxIndex] / 4f);
AwardGold(amount);
// AudioManager.instnace.CollectGold(amount);
}else{
AwardSkin(availableSkins[Random.Range(0, availableSkins.Count-1)]);
AudioManager.instnace.FindBP();
}
}else{
AwardGold((int)(boxPrices[selectedBoxIndex] / 4f));
}
newBoxPanel.SetActive(false);
}
void AwardSkin(SkinShopItemData skin){
newSkinPanel.SetActive(true);
newSkinPanel.transform.Find("img_skin").GetComponent<Image>().sprite = skin.image;
//UpgradeEffect.top_instance.Show(newSkinPanel.GetComponent<RectTransform>().position);
DBmanager.AddSkinBlueprint(skin);
AudioManager.instnace.ConfettiExplosion();
//Fullscreen FX
foreach (ParticleSystem particle in fullscreenFX)
{
particle.Play();
}
Refresh();
}
void AwardGold(int amount){
goldsPanel.SetActive(true);
goldsPanel.transform.Find("txt_amount").GetComponent<TMP_Text>().text = amount.ToString();
// UpgradeEffect.top_instance.Show(goldsPanel.GetComponent<RectTransform>().position);
//Confetti
confettiFX.Play();
AudioManager.instnace.ConfettiExplosion();
DBmanager.SetCoins(DBmanager.Coins + amount);
Refresh();
}
const string infoTemplate = "By opening this Design Crate, You will either receive,\n\n{Golds} Golds\nor\n{Rarity} Blueprint";
public void ShowInfoCommon(){
int goldAmount = (int)(boxPrices[0] / 4f);
MessageDialog.instance.ShowMessage("Common Design Crate", infoTemplate.Replace("{Golds}", goldAmount.ToString()).Replace("{Rarity}", "Common"));
}
public void ShowInfoRare(){
int goldAmount = (int)(boxPrices[1] / 4f);
MessageDialog.instance.ShowMessage("Rare Design Crate", infoTemplate.Replace("{Golds}", goldAmount.ToString()).Replace("{Rarity}", "Rare"));
}
public void ShowInfoEpic(){
int goldAmount = (int)(boxPrices[2] / 4f);
MessageDialog.instance.ShowMessage("Epic Design Crate", infoTemplate.Replace("{Golds}", goldAmount.ToString()).Replace("{Rarity}", "Epic"));
}
public void ShowInfoLegendary(){
int goldAmount = (int)(boxPrices[3] / 4f);
MessageDialog.instance.ShowMessage("Legendary Design Crate", infoTemplate.Replace("{Golds}", goldAmount.ToString()).Replace("{Rarity}", "Legendary"));
}
public void Hide(){
gameObject.SetActive(false);
}
}