You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

349 lines
12 KiB

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using Ability;
using JetBrains.Annotations;
using TMPro;
public class MainAbilityScreen : MonoBehaviour
{
List<Button> m_mainAbIcons = new List<Button>();
[SerializeField] Button m_randomSkillButton;
[SerializeField] Button m_unlockUpgradeButton;
GameObject m_selectedOver;
GameObject m_selectedIcon;
GameObject m_unlockLabelObj;
GameObject m_upgradeLabelObj;
GameObject m_randomPriceLabelObj;
[SerializeField] GameObject _deselectZone;
bool m_wasMouseDown = false;
float m_mouseDownTime = 0f;
//-----------------------------------------------------------------------------------------
void Start ()
{
InitIcons();
InitButton();
if (_deselectZone)
_deselectZone.SetActive(false);
}
//-----------------------------------------------------------------------------------------
void InitIcons()
{
Transform iconsContainerTrans = transform.Find("icons");
if (iconsContainerTrans)
{
m_selectedOver = iconsContainerTrans.Find("selected_icon").gameObject;
m_selectedOver.SetActive(false);
foreach (Transform child in iconsContainerTrans)
{
Button iconBtn = child.GetComponent<Button>();
if (iconBtn)
{
iconBtn.onClick.AddListener(ButtonPress);
m_mainAbIcons.Add(iconBtn);
}
MainAbilityIcon icon = child.GetComponent<MainAbilityIcon>();
if (icon)
{
icon.Init();
SetAbLvlUpOnIcon(icon);
icon.SetActiveState();
}
}
}
else
CommonFunctions.myassert(false);
}
//-----------------------------------------------------------------------------------------
void InitButton()
{
if (m_randomSkillButton)
{
m_randomSkillButton.onClick.AddListener(ButtonPress);
Transform textLabelTrans = m_randomSkillButton.transform.Find("random_text");
if (textLabelTrans)
textLabelTrans.Find("text").GetComponent<TextMeshProUGUI>().text = GlobalsVar.gGameTextMng.GetGameText(1).ToUpper();
Transform priceLabelTrans = m_randomSkillButton.transform.Find("price_text");
if (priceLabelTrans)
{
m_randomPriceLabelObj = priceLabelTrans.Find("price").gameObject;
m_randomPriceLabelObj.GetComponent<TextMeshProUGUI>().text = GetUpgradeCostForRandom().ToString();
}
}
if (m_unlockUpgradeButton)
{
Transform upgradeLabelTrans = m_unlockUpgradeButton.transform.Find("upgrade_text");
if (upgradeLabelTrans)
{
m_upgradeLabelObj = upgradeLabelTrans.gameObject;
m_upgradeLabelObj.transform.Find("text").GetComponent<TextMeshProUGUI>().text = GlobalsVar.gGameTextMng.GetGameText(3).ToUpper();
}
Transform unlockLabelTrans = m_unlockUpgradeButton.transform.Find("unlock_text");
if (unlockLabelTrans)
{
m_unlockLabelObj = unlockLabelTrans.gameObject;
m_unlockLabelObj.transform.Find("text").GetComponent<TextMeshProUGUI>().text = GlobalsVar.gGameTextMng.GetGameText(2).ToUpper();
}
m_unlockUpgradeButton.onClick.AddListener(ButtonPress);
SetUnlockButtonState("Unlock");
m_unlockUpgradeButton.GetComponent<Image>().material = GlobalsVar.gGrayscaleMaterial;
}
}
//-----------------------------------------------------------------------------------------
void SetUnlockButtonState(string state)
{
switch (state)
{
case "Upgrade":
m_upgradeLabelObj.SetActive(true);
m_unlockLabelObj.SetActive(false);
m_upgradeLabelObj.transform.Find("price").GetComponent<TextMeshProUGUI>().text = GetUpgradeCostForCurrentAb().ToString();
break;
case "Unlock":
m_upgradeLabelObj.SetActive(false);
m_unlockLabelObj.SetActive(true);
m_unlockLabelObj.transform.Find("price").GetComponent<TextMeshProUGUI>().text = GlobalsVar.gUnlockAbPrice.ToString();
break;
}
}
//-----------------------------------------------------------------------------------------
void ButtonPress()
{
GameObject clickedGO = EventSystem.current.currentSelectedGameObject;
for (int i = 0; i < m_mainAbIcons.Count; ++i)
{
if (clickedGO == m_mainAbIcons[i].gameObject)
{
SetSelected(m_mainAbIcons[i].gameObject);
return;
}
}
if (clickedGO == m_unlockUpgradeButton.gameObject)
{
StartUnlockAbility();
}
else if (clickedGO == m_randomSkillButton.gameObject)
{
UnlockRandomSkill();
}
}
//-----------------------------------------------------------------------------------------
void SetSelected(GameObject obj)
{
if (m_selectedIcon == obj)
return;
m_selectedIcon = obj;
m_selectedOver.SetActive(true);
m_unlockUpgradeButton.GetComponent<Image>().material = null;
Vector3 pos = obj.transform.localPosition;
m_selectedOver.transform.localPosition = pos;
string btnState = "Unlock";
if (m_selectedIcon.GetComponent<MainAbilityIcon>().IsActive)
btnState = "Upgrade";
SetUnlockButtonState(btnState);
_deselectZone.SetActive(true);
}
//-----------------------------------------------------------------------------------------
[UsedImplicitly]
public void StartUnlockAbility()
{
MainMenu mainMenu = GlobalsVar.gBoard as MainMenu;
if (m_selectedIcon)
{
MainAbilityIcon icon = m_selectedIcon.GetComponent<MainAbilityIcon>();
if (icon.IsActive)
{
int currentCrystalsCost = GetUpgradeCostForCurrentAb();
if (GlobalsVar.gUser.Crystals >= currentCrystalsCost)
{
GlobalsVar.gUser.IncCurrency(Shop.ShopType.Gems, -currentCrystalsCost);
if (mainMenu)
mainMenu.Repaint();
GlobalsVar.gMainAbilities.UpgradeByType(icon.Type);
SetAbLvlUpOnIcon(icon);
m_upgradeLabelObj.transform.Find("price").GetComponent<TextMeshProUGUI>().text = GetUpgradeCostForCurrentAb().ToString();
}
else
{
GameObject dlg = GlobalsVar.gBoard.StartDialog("NotEnoughDialog");
dlg.GetComponent<NotEnoughDialog>().Init(Shop.ShopType.Gems);
}
}
else
{
if (GlobalsVar.gUser.Crystals >= GlobalsVar.gUnlockAbPrice)
{
GlobalsVar.gUser.IncCurrency(Shop.ShopType.Gems, -GlobalsVar.gUnlockAbPrice);
if (mainMenu)
mainMenu.Repaint();
GlobalsVar.gMainAbilities.SetAbilityActiveByType(icon.Type);
icon.SetActiveState();
SetUnlockButtonState("Upgrade");
}
else
{
GameObject dlg = GlobalsVar.gBoard.StartDialog("NotEnoughDialog");
dlg.GetComponent<NotEnoughDialog>().Init(Shop.ShopType.Gems);
}
}
}
}
//-----------------------------------------------------------------------------------------
void UnlockRandomSkill()
{
MainMenu mainMenu = GlobalsVar.gBoard as MainMenu;
int currentCost = GetUpgradeCostForRandom();
if (GlobalsVar.gUser.Coins >= currentCost)
{
GlobalsVar.gUser.IncCurrency(Shop.ShopType.Coins, -currentCost);
if (mainMenu)
mainMenu.Repaint();
GlobalsVar.gCurrentAbRandomCount++;
m_randomPriceLabelObj.GetComponent<TextMeshProUGUI>().text = GetUpgradeCostForRandom().ToString();
GenerateRandomAbility();
}
else
{
GameObject dlg = GlobalsVar.gBoard.StartDialog("NotEnoughDialog");
dlg.GetComponent<NotEnoughDialog>().Init(Shop.ShopType.Coins);
}
}
//-----------------------------------------------------------------------------------------
private void GenerateRandomAbility()
{
RandomProb rand = new RandomProb();
for (int i = 0; i < m_mainAbIcons.Count; ++i)
{
MainAbilityType type = m_mainAbIcons[i].GetComponent<MainAbilityIcon>().Type;
int weight = m_mainAbIcons[i].GetComponent<MainAbilityIcon>().Weight;
rand.AddValue(type.ToString(), weight);
}
string randTypeStr = rand.GetRandomValue();
MainAbilityType randType = GlobalsVar.gMainAbilities.GetAbilityTypeByName(randTypeStr);
GlobalsVar.gMainAbilities.SetAbilityActiveByType(randType);
GlobalsVar.gMainAbilities.UpgradeByType(randType);
for (int i = 0; i < m_mainAbIcons.Count; ++i)
{
MainAbilityIcon icon = m_mainAbIcons[i].GetComponent<MainAbilityIcon>();
if (icon.Type == randType)
{
icon.SetActiveState();
SetAbLvlUpOnIcon(icon);
break;
}
}
}
//-----------------------------------------------------------------------------------------
void SetAbLvlUpOnIcon(MainAbilityIcon icon)
{
int lvlUp = GlobalsVar.gMainAbilities.GetAbilityLvlUpByType(icon.Type);
if (lvlUp == 0)
icon.transform.Find("lvlUpBack").gameObject.SetActive(false);
else
{
if (!icon.transform.Find("lvlUpBack").gameObject.activeSelf)
icon.transform.Find("lvlUpBack").gameObject.SetActive(true);
icon.SetLvlUp(lvlUp);
}
}
//-----------------------------------------------------------------------------------------
void Update ()
{
}
//-----------------------------------------------------------------------------------------
public void ResetSelection()
{
if (m_selectedIcon == null && !m_selectedOver.activeSelf)
return;
if (m_selectedOver)
m_selectedOver.SetActive(false);
m_selectedIcon = null;
SetUnlockButtonState("Unlock");
_deselectZone.SetActive(false);
m_unlockUpgradeButton.GetComponent<Image>().material = GlobalsVar.gGrayscaleMaterial;
}
//-----------------------------------------------------------------------------------------
int GetUpgradeCostForCurrentAb()
{
if (!m_selectedIcon)
{
CommonFunctions.myassert(false);
return 0;
}
MainAbilityIcon icon = m_selectedIcon.GetComponent<MainAbilityIcon>();
int lvlUp = GlobalsVar.gUser.GetMainAbLvlUp(icon.Type.ToString());
if (lvlUp == 1)
return GlobalsVar.gUpgradeAbBaseCrystals + GlobalsVar.gUpgradeAbBaseCrystals;
else
{
int coeff = CommonFunctions.GetCoeffForUpgrade(lvlUp);
return GlobalsVar.gUpgradeAbBaseCrystals * lvlUp + GlobalsVar.gUpgradeAbBaseCrystals * coeff;
}
}
//-----------------------------------------------------------------------------------------
int GetUpgradeCostForRandom()
{
if (GlobalsVar.gCurrentAbRandomCount == 0)
return GlobalsVar.gUpgradeAbBaseCoins;
else if (GlobalsVar.gCurrentAbRandomCount == 1)
return GlobalsVar.gUpgradeAbBaseCoins + GlobalsVar.gUpgradeAbBaseCoins;
else
{
int coeff = CommonFunctions.GetCoeffForUpgrade(GlobalsVar.gCurrentAbRandomCount);
return GlobalsVar.gUpgradeAbBaseCoins * GlobalsVar.gCurrentAbRandomCount + GlobalsVar.gUpgradeAbBaseCoins * coeff;
}
}
}