70 lines
2.2 KiB
C#
70 lines
2.2 KiB
C#
using TMPro;
|
||
using UnityEngine;
|
||
|
||
public class MenuControllerOneRegime : MonoBehaviour
|
||
{
|
||
[SerializeField] private string GameplaySceneName = "Gameplay";
|
||
|
||
[SerializeField] private TextMeshProUGUI BalanceLabel;
|
||
[SerializeField] private TextMeshProUGUI GamesPayedLabel;
|
||
[SerializeField] private TextMeshProUGUI FreeplayLabel;
|
||
|
||
[SerializeField] private string BalancePrefix = "Баланс: ";
|
||
[SerializeField] private string GamesPayedPrefix = "Оплачено игр: ";
|
||
|
||
private int gamePrice;
|
||
private bool initialized = false;
|
||
private string valuta;
|
||
private bool _freeplay;
|
||
|
||
private bool EnoughMoney => _freeplay || WalletManager.Balance >= gamePrice;
|
||
private int ActualPrice => _freeplay ? 0 : gamePrice;
|
||
public void Initialize()
|
||
{
|
||
gamePrice = SettingsManager.GetInt(SettingKey.GamePrice, 100);
|
||
valuta = SettingsManager.GetString(SettingKey.Valuta, "руб.");
|
||
_freeplay = SettingsManager.GetBool(SettingKey.Freeplay, false);
|
||
InitLayout();
|
||
|
||
|
||
WalletManager.OnBalanceChanged += UpdateUI;
|
||
UpdateUI(WalletManager.Balance);
|
||
initialized = true;
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (!initialized) return;
|
||
|
||
if (EnoughMoney && In.GetButtonDown(InSignal.DownRight))
|
||
{
|
||
WalletManager.TakeMoney(ActualPrice);
|
||
SceneLoader.LoadScene(GameplaySceneName);
|
||
}
|
||
|
||
if (In.GetButtonDown(InSignal.Service))
|
||
SceneLoader.LoadScene("Settings");
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
WalletManager.OnBalanceChanged -= UpdateUI;
|
||
}
|
||
public void UpdateUI(int balance)
|
||
{
|
||
if(_freeplay) return;
|
||
string valutaPluralized = valuta == "руб." ? PluralizeUtil.Rubles(balance) : PluralizeUtil.Tokens(balance);
|
||
BalanceLabel.text = $"{BalancePrefix}{balance}/{ActualPrice} {valutaPluralized}";
|
||
GamesPayedLabel.text = $"{GamesPayedPrefix}{balance / gamePrice}";
|
||
}
|
||
|
||
private void InitLayout()
|
||
{
|
||
bool needPay = !_freeplay;
|
||
BalanceLabel.gameObject.SetActive(needPay);
|
||
GamesPayedLabel.gameObject.SetActive(needPay);
|
||
|
||
FreeplayLabel.gameObject.SetActive(_freeplay);
|
||
}
|
||
}
|