UPF/Assets/Game/Scripts/SelectedItemMenu.cs
2022-12-14 01:49:10 +05:30

418 lines
20 KiB
C#

using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class SelectedItemMenu : MonoBehaviour
{
public static SelectedItemMenu instance;
public Text nameTxt;
public Button upgradeMenuBtn;
public Button infoBtn;
public CollectBtn collectBtn;
// public Button joinHallBtn;
public Button repairRocketsBtn;
public Button tradingPostBtn;
public Button designLabBtn;
public TradingPost tradingPost;
public DesignLab designLab;
[Header("info menu")]
public GameObject infoMenu;
public TMP_Text levelTxt;
public TMP_Text buildingName;
public TMP_Text descriptionTxt;
[Header("upgrade menu")]
public GameObject upgradeMenu;
// public GameObject goldmineUpgradeMenu;
// public TMP_Text goldMineUpgradeMenuTitle;
public Button btn_gold_prod;
public Button btn_gold_cap;
public Transform upgrade_statParent;
public TMP_Text upgrade_tierTxt;
public TMP_Text upgrade_buildingName;
public TMP_Text extraDetailsTxt;
public Button upgradeBtn;
[Header("Special buildings")]
public BuildingData goldMineBuilding;
public BuildingData solarPanelsBuilding;
public BuildingData designLabBuilding;
public BuildingData rocketRepair;
public BuildingData tradePost;
void Awake(){
instance=this;
}
void Start()
{
Selector.OnSelectedChanged.AddListener(OnSelectionChanged);
upgradeMenuBtn.onClick.AddListener(OnUpgradeMenuClicked);
infoBtn.onClick.AddListener(OnInfoClicked);
upgradeBtn.onClick.AddListener(OnUpgrade);
collectBtn.btn.onClick.AddListener(OnCollect);
repairRocketsBtn.onClick.AddListener(OpenSkinMenu);
tradingPostBtn.onClick.AddListener(OpenTradingPost);
designLabBtn.onClick.AddListener(OpenDesignLab);
btn_gold_cap.onClick.AddListener(OnGoldmineCapUpgrade);
btn_gold_prod.onClick.AddListener(OnGoldmineProdUpgrade);
}
Building lastSelectedBuilding = null;
IEnumerator DoubleClickTimer(Building b){
yield return new WaitForSeconds(0.05f);
lastSelectedBuilding = Selector.selectedBuilding;
yield return new WaitForSeconds(0.2f);
lastSelectedBuilding = null;
Debug.Log("Reset tap");
}
void OnSelectionChanged()
{
if(lastSelectedBuilding == null){
Debug.Log("First tap");
}else{
//Double Clicked
Debug.Log("Second tap");
if(Selector.selectedData == rocketRepair){
OpenSkinMenu();
}else if(Selector.selectedData == tradePost){
OpenTradingPost();
}else if(Selector.selectedData == designLabBuilding){
OpenDesignLab();
}else if(Selector.selectedData == goldMineBuilding || Selector.selectedData == solarPanelsBuilding){
Debug.Log("Double tapped on a collectable");
CollectBtn.instance.OnClick();
}
}
StartCoroutine(DoubleClickTimer(Selector.selectedBuilding));
if (Selector.selectedBuilding != null)
{
infoBtn.gameObject.SetActive(true);
if (Selector.selectedData.levels.Count <= 1)
{
upgradeMenuBtn.gameObject.SetActive(false);
}
else
{
upgradeMenuBtn.gameObject.SetActive(true);
}
if(Selector.selectedData.collectable){
collectBtn.gameObject.SetActive(true);
Debug.Log("Last collected : " +Selector.selectedBuilding.lastCollected );
collectBtn.Set(Selector.selectedData.buildingName,Selector.selectedBuilding.lastCollected,Selector.selectedData.productinoRates[Selector.selectedBuilding.curLevel],Selector.selectedData.resourceType);
}else{
collectBtn.gameObject.SetActive(false);
}
// joinHallBtn.gameObject.SetActive((Selector.selectedData == mainHall));
repairRocketsBtn.gameObject.SetActive((Selector.selectedData == rocketRepair));
tradingPostBtn.gameObject.SetActive((Selector.selectedData == tradePost));
designLabBtn.gameObject.SetActive((Selector.selectedData == designLabBuilding));
}
else
{
upgradeMenuBtn.gameObject.SetActive(false);
infoBtn.gameObject.SetActive(false);
collectBtn.gameObject.SetActive(false);
// joinHallBtn.gameObject.SetActive(false);
repairRocketsBtn.gameObject.SetActive(false);
}
upgradeMenu.SetActive(false);
infoMenu.SetActive(false);
}
public void OnUpgradeMenuClicked()
{
extraDetailsTxt.gameObject.SetActive(false);
// Debug.Log("Opening Upgrade Menu for : " + Selector.selectedBuilding.buildingData.name);
AudioManager.instnace.UIPopup();
upgradeMenu.SetActive(true);
upgrade_buildingName.text = Selector.selectedBuilding.buildingData.buildingName;
bool isGoldMine = Selector.selectedBuilding.GetComponent<GoldMine>()!=null;
bool isSolarPanel = Selector.selectedBuilding.GetComponent<SolarPanels>()!=null;
List<BuildingStat> stats = Selector.selectedBuilding.buildingData.levels[Selector.selectedBuilding.curLevel].stats;
for (int i = 0; i < upgrade_statParent.childCount; i++)
{
bool hasDataForThis = i < stats.Count;
upgrade_statParent.GetChild(i).gameObject.SetActive(hasDataForThis);
if (hasDataForThis)
{
upgrade_statParent.GetChild(i).GetChild(0).GetComponent<TMP_Text>().text = stats[i].name;
string nextLevelStat = ((Selector.selectedBuilding.buildingData.levels.Count-1 > Selector.selectedBuilding.curLevel) ? $" <color=green> -> {Selector.selectedBuilding.buildingData.levels[Selector.selectedBuilding.curLevel+1].stats[i].value}</color>" : "");
upgrade_statParent.GetChild(i).GetChild(1).GetComponent<TMP_Text>().text = stats[i].value + (isGoldMine || isSolarPanel? "":nextLevelStat);
upgrade_statParent.GetChild(i).GetChild(2).GetComponent<Image>().sprite = stats[i].image;
}
}
if(isGoldMine || isSolarPanel){ //Special path for gold mine
// for(int i=0; i < upgrade_tierIndicators.Length;i++){ //No rarity tag for you
// upgrade_tierIndicators[i].SetActive(false);
// }
upgrade_tierTxt.text = "";
if (Selector.selectedBuilding.curLevel < Selector.selectedData.levels.Count - 1){
// upgradeBtn.interactable = true;
// // upgradeBtn.GetComponentInChildren<TMP_Text>().text = "Upgrade";
// upgradeBtn.transform.GetChild(1).gameObject.SetActive(false);
// upgradeBtn.transform.GetChild(2).gameObject.SetActive(false);
// upgradeBtn.transform.GetChild(3).gameObject.SetActive(true);
upgradeBtn.gameObject.SetActive(false);
btn_gold_cap.gameObject.SetActive(true);
btn_gold_prod.gameObject.SetActive(true);
OnUpgrade();
}
return;
}
upgradeBtn.gameObject.SetActive(true);
btn_gold_cap.gameObject.SetActive(false);
btn_gold_prod.gameObject.SetActive(false);
upgradeBtn.transform.GetChild(3).gameObject.SetActive(false); //Not gold mine, no choice
upgrade_tierTxt.text = BuildingManager.GetStatValue(Selector.selectedBuilding,"Level");
if (Selector.selectedBuilding.curLevel < Selector.selectedData.levels.Count - 1)
{
int goldPrice = Selector.selectedData.levels[Selector.selectedBuilding.curLevel + 1].price;
int metalPrice = Selector.selectedData.levels[Selector.selectedBuilding.curLevel + 1].metal_price;
upgradeBtn.interactable = goldPrice <= DBmanager.Coins && metalPrice <= DBmanager.Metal;
upgradeBtn.transform.GetChild(1).gameObject.SetActive(true);
upgradeBtn.transform.GetChild(2).gameObject.SetActive(false);
upgradeBtn.transform.GetChild(1).GetChild(0).gameObject.SetActive(goldPrice>0);
upgradeBtn.transform.GetChild(1).GetChild(0).GetComponentInChildren<TMP_Text>().text = goldPrice.ToString();
upgradeBtn.transform.GetChild(1).GetChild(1).gameObject.SetActive(metalPrice>0);
upgradeBtn.transform.GetChild(1).GetChild(1).GetComponentInChildren<TMP_Text>().text = metalPrice.ToString();
}
else
{
upgradeBtn.interactable = false;
// upgradeBtn.GetComponentInChildren<TMP_Text>().text = "MAX!";
upgradeBtn.transform.GetChild(1).gameObject.SetActive(false);
upgradeBtn.transform.GetChild(2).gameObject.SetActive(true);
Debug.Log("Already max");
}
// if(Selector.selectedData == rocketRepair){
// SkinShopManager.instance.Populate();
// upgradeBtn.interactable= (SkinShopManager.availableToUpgrade);
// if(!SkinShopManager.availableToUpgrade && Selector.selectedBuilding.curLevel < Selector.selectedData.levels.Count-1){
// // MessageDialog.instance.ShowMessage("Notice!","You have to purchase half of the best rarity skins available to upgrade");
// extraDetailsTxt.gameObject.SetActive(true);
// extraDetailsTxt.text = "You have to purchase half of the best rarity skins available to upgrade";
// extraDetailsTxt.color = Color.red;
// }
// }
if (upgrade_statParent.childCount < stats.Count)
{
Debug.LogError("Please add another slot for stats in upgrade menu.\nSlots not enough for " + Selector.selectedBuilding.buildingData.name + " , " + upgrade_statParent.childCount + " slots for " + stats.Count + " stats");
}
}
public void HideUpgradeMenu(){
upgradeMenu.SetActive(false);
}
public void OnUpgrade()
{
if(Selector.selectedBuilding.GetComponent<GoldMine>()!=null){
AudioManager.instnace.UIPopup();
// goldMineUpgradeMenuTitle.text = "Upgrade Gold Mine";
// goldmineUpgradeMenu.SetActive(true);
int L = Selector.selectedBuilding.curLevel+1;
int C = GoldMine.GetCapacityRateByLevel(L);
int P = GoldMine.GetProductionRateByLevel(L);
//Calculate prices
int gold_cost_p = GoldMine.GetGoldCostForProduction(L);
int metal_cost_p = GoldMine.GetMetalCostForProduction(L);
int gold_cost_c = GoldMine.GetGoldCostForCapacity(L);
int metal_cost_c = GoldMine.GetMetalCostForCapacity(L);
if(P>=GoldMine.LevelsCount){
btn_gold_prod.interactable=false;
btn_gold_prod.transform.Find("Price/txt_price").GetComponent<TMP_Text>().text = "Max!";
btn_gold_prod.transform.Find("Price/txt_price_metal").gameObject.SetActive(false);
btn_gold_prod.transform.Find("Price/txt_price").gameObject.SetActive(true);
}
else{
bool affroadable = DBmanager.Coins >= gold_cost_p && DBmanager.Metal >= metal_cost_p;
btn_gold_prod.interactable=affroadable;
btn_gold_prod.transform.Find("Price/txt_price").GetComponent<TMP_Text>().text = gold_cost_p.ToString();
btn_gold_prod.transform.Find("Price/txt_price_metal").GetComponent<TMP_Text>().text = metal_cost_p.ToString();
btn_gold_prod.transform.Find("Price/txt_price").gameObject.SetActive(gold_cost_p>0);
btn_gold_prod.transform.Find("Price/txt_price_metal").gameObject.SetActive(metal_cost_p>0);
}
if(C>=GoldMine.LevelsCount){
btn_gold_cap.interactable = false;
btn_gold_cap.transform.Find("Price/txt_price").GetComponent<TMP_Text>().text = "Max!";
btn_gold_cap.transform.Find("Price/txt_price_metal").gameObject.SetActive(false);
btn_gold_cap.transform.Find("Price/txt_price").gameObject.SetActive(true);
}
else{
bool affroadable = DBmanager.Coins >= gold_cost_c && DBmanager.Metal >= metal_cost_c;
btn_gold_cap.interactable=affroadable;
btn_gold_cap.transform.Find("Price/txt_price").GetComponent<TMP_Text>().text = gold_cost_c.ToString();
btn_gold_cap.transform.Find("Price/txt_price_metal").GetComponent<TMP_Text>().text = metal_cost_c.ToString();
btn_gold_cap.transform.Find("Price/txt_price").gameObject.SetActive(gold_cost_c > 0);
btn_gold_cap.transform.Find("Price/txt_price_metal").gameObject.SetActive(metal_cost_c > 0);
}
return;
}else if(Selector.selectedBuilding.GetComponent<SolarPanels>()!=null){
AudioManager.instnace.UIPopup();
// goldmineUpgradeMenu.SetActive(true);
// goldMineUpgradeMenuTitle.text = "Upgrade Solar Panels";
int L = Selector.selectedBuilding.curLevel+1;
int C = SolarPanels.GetCapacityRateByLevel(L);
int P = SolarPanels.GetProductionRateByLevel(L);
//Calculate prices
int gold_cost_p = SolarPanels.GetGoldCostForProduction(L);
int metal_cost_p = SolarPanels.GetMetalCostForProduction(L);
int gold_cost_c = SolarPanels.GetGoldCostForCapacity(L);
int metal_cost_c = SolarPanels.GetMetalCostForCapacity(L);
if(P>=SolarPanels.LevelsCount){
btn_gold_prod.interactable=false;
btn_gold_prod.transform.Find("Price/txt_price").GetComponent<TMP_Text>().text = "Max!";
btn_gold_prod.transform.Find("Price/txt_price_metal").gameObject.SetActive(false);
btn_gold_prod.transform.Find("Price/txt_price").gameObject.SetActive(true);
}
else{
bool affroadable = DBmanager.Coins >= gold_cost_p && DBmanager.Metal >= metal_cost_p;
btn_gold_prod.interactable=affroadable;
btn_gold_prod.transform.Find("Price/txt_price").GetComponent<TMP_Text>().text = gold_cost_p.ToString();
btn_gold_prod.transform.Find("Price/txt_price_metal").GetComponent<TMP_Text>().text = metal_cost_p.ToString();
btn_gold_prod.transform.Find("Price/txt_price").gameObject.SetActive(gold_cost_p>0);
btn_gold_prod.transform.Find("Price/txt_price_metal").gameObject.SetActive(metal_cost_p>0);
}
if(C>=SolarPanels.LevelsCount){
btn_gold_cap.interactable = false;
btn_gold_cap.transform.Find("Price/txt_price").GetComponent<TMP_Text>().text = "Max!";
btn_gold_cap.transform.Find("Price/txt_price_metal").gameObject.SetActive(false);
btn_gold_cap.transform.Find("Price/txt_price").gameObject.SetActive(true);
}
else{
bool affroadable = DBmanager.Coins >= gold_cost_c && DBmanager.Metal >= metal_cost_c;
btn_gold_cap.interactable=affroadable;
btn_gold_cap.transform.Find("Price/txt_price").GetComponent<TMP_Text>().text = gold_cost_c.ToString();
btn_gold_cap.transform.Find("Price/txt_price_metal").GetComponent<TMP_Text>().text = metal_cost_c.ToString();
btn_gold_cap.transform.Find("Price/txt_price").gameObject.SetActive(gold_cost_c > 0);
btn_gold_cap.transform.Find("Price/txt_price_metal").gameObject.SetActive(metal_cost_c > 0);
}
return;
}
upgradeBtn.interactable= false;
Selector.selectedBuilding.Upgrade();
// OnUpgradeMenuClicked();
}
public void OnGoldmineCapUpgrade(){
if(Selector.selectedBuilding.GetComponent<GoldMine>()!=null){
int L = Selector.selectedBuilding.curLevel+1;
int C = GoldMine.GetCapacityRateByLevel(L);
if(C >=GoldMine.LevelsCount){return;}
int P = GoldMine.GetProductionRateByLevel(L);
int newLevel = GoldMine.GetLevelByRates(C + 1, P);
upgradeBtn.interactable= false;
Selector.selectedBuilding.Upgrade(newLevel:newLevel-1, gold_cost:GoldMine.GetGoldCostForCapacity(L), metal_cost: GoldMine.GetMetalCostForCapacity(L) );
// goldmineUpgradeMenu.SetActive(false);
HideUpgradeMenu();
OnUpgradeMenuClicked();
}else{
int L = Selector.selectedBuilding.curLevel+1;
int C = SolarPanels.GetCapacityRateByLevel(L);
if(C >=SolarPanels.LevelsCount){return;}
int P = SolarPanels.GetProductionRateByLevel(L);
int newLevel = SolarPanels.GetLevelByRates(C + 1, P);
upgradeBtn.interactable= false;
Selector.selectedBuilding.Upgrade(newLevel:newLevel-1, gold_cost:SolarPanels.GetGoldCostForCapacity(L), metal_cost: SolarPanels.GetMetalCostForCapacity(L) );
// goldmineUpgradeMenu.SetActive(false);
OnUpgradeMenuClicked();
}
HideUpgradeMenu();
}
public void OnGoldmineProdUpgrade(){
if(Selector.selectedBuilding.GetComponent<GoldMine>()!=null){
int L = Selector.selectedBuilding.curLevel+1;
int C = GoldMine.GetCapacityRateByLevel(L);
int P = GoldMine.GetProductionRateByLevel(L);
if(P >=GoldMine.LevelsCount){return;}
int newLevel = GoldMine.GetLevelByRates(C, P+1);
upgradeBtn.interactable= false;
Selector.selectedBuilding.Upgrade(newLevel:newLevel-1, gold_cost: GoldMine.GetGoldCostForProduction(L), metal_cost: GoldMine.GetMetalCostForProduction(L));
}else{
int L = Selector.selectedBuilding.curLevel+1;
int C = SolarPanels.GetCapacityRateByLevel(L);
int P = SolarPanels.GetProductionRateByLevel(L);
if(P >=SolarPanels.LevelsCount){return;}
int newLevel = SolarPanels.GetLevelByRates(C, P+1);
upgradeBtn.interactable= false;
Selector.selectedBuilding.Upgrade(newLevel:newLevel-1, gold_cost: SolarPanels.GetGoldCostForProduction(L), metal_cost: SolarPanels.GetMetalCostForProduction(L));
}
// goldmineUpgradeMenu.SetActive(false);
HideUpgradeMenu();
OnUpgradeMenuClicked();
}
void OnInfoClicked()
{
Debug.Log("Opening Info Menu for : " + Selector.selectedBuilding.buildingData.name);
infoMenu.SetActive(true);
buildingName.text = Selector.selectedBuilding.buildingData.buildingName;
levelTxt.text = BuildingManager.GetStatValue(Selector.selectedBuilding, "Level");
descriptionTxt.text = Selector.selectedBuilding.buildingData.description;
AudioManager.instnace.UIPopup();
}
public void OpenSkinMenu(){
SkinShopManager.instance.Show();
AudioManager.instnace.UIPopup();
}
public void OpenTradingPost(){
tradingPost.Show();
AudioManager.instnace.UIPopup();
}
public void OpenDesignLab(){
designLab.Show();
AudioManager.instnace.UIPopup();
}
void OnCollect(){
Debug.Log("Collecting from : " + Selector.selectedData.buildingName);
}
}