105 lines
3.3 KiB
C#
105 lines
3.3 KiB
C#
using TMPro;
|
||
using UnityEngine;
|
||
|
||
public class MenuControllerOneRegime : MonoBehaviour
|
||
{
|
||
[SerializeField] private string GameplaySceneName = "Gameplay";
|
||
|
||
[Header("UI")]
|
||
[SerializeField] private TextMeshProUGUI BalanceLabel;
|
||
[SerializeField] private TextMeshProUGUI PriceLabel;
|
||
[SerializeField] private TextMeshProUGUI FreeplayLabel;
|
||
[SerializeField] private CanvasGroup screenInstruction;
|
||
[SerializeField] private CanvasGroup _instructionCommon;
|
||
[SerializeField] private CanvasGroup _instructionPeriscope;
|
||
|
||
[SerializeField] PeriscopeController periscopeController;
|
||
|
||
[Header("Timer")]
|
||
[SerializeField] private float timeToEnterSettings = 1;
|
||
private float gotoSettingsTimer = 0;
|
||
|
||
[SerializeField] private AudioClip ready;
|
||
|
||
private int price;
|
||
private string valutaName;
|
||
private bool freeplay;
|
||
|
||
private bool EnoughMoney => freeplay || WalletManager.Balance >= price;
|
||
|
||
public void Initialize()
|
||
{
|
||
price = SettingsManager.GetInt(SettingKey.GamePrice, 100);
|
||
valutaName = SettingsManager.GetString(SettingKey.Valuta, "руб.");
|
||
freeplay = SettingsManager.GetBool(SettingKey.Freeplay, false);
|
||
InitLayout();
|
||
|
||
periscopeController.allowControls = false;
|
||
|
||
if (!freeplay) WalletManager.OnBalanceChanged += UpdateUI;
|
||
WalletManager.OnBalanceAdd += PrepareIfPayed;
|
||
|
||
UpdateUI(WalletManager.Balance);
|
||
PrepareIfPayed(WalletManager.Balance);
|
||
}
|
||
private void OnDestroy()
|
||
{
|
||
WalletManager.OnBalanceChanged -= UpdateUI;
|
||
WalletManager.OnBalanceAdd -= PrepareIfPayed;
|
||
}
|
||
|
||
private void PrepareIfPayed(int _)
|
||
{
|
||
if (!EnoughMoney) return;
|
||
|
||
WalletManager.OnBalanceAdd -= PrepareIfPayed;
|
||
|
||
periscopeController.MoveCenter();
|
||
periscopeController.allowControls = true;
|
||
screenInstruction.alpha = 1;
|
||
SoundManager.PlayReplica(ready);
|
||
}
|
||
|
||
private void InitLayout()
|
||
{
|
||
bool needPay = !freeplay;
|
||
BalanceLabel.enabled = needPay;
|
||
PriceLabel.enabled = needPay;
|
||
FreeplayLabel.enabled = freeplay;
|
||
screenInstruction.alpha = 0;
|
||
|
||
PriceLabel.text = $"Цена: {price} {GetValutaPluralized(price)}";
|
||
}
|
||
|
||
public void UpdateUI(int balance)
|
||
{
|
||
BalanceLabel.text = $"Баланс: {balance} {GetValutaPluralized(balance)}";
|
||
|
||
_instructionCommon.alpha = !EnoughMoney ? 1 : 0;
|
||
_instructionPeriscope.alpha = EnoughMoney ? 1 : 0;
|
||
}
|
||
|
||
private string GetValutaPluralized(int balance)
|
||
=> valutaName == "руб." ? PluralizeUtil.Rubles(balance) : PluralizeUtil.Tokens(balance);
|
||
|
||
#region Callback Methods
|
||
//Вызываются через UnityEvent
|
||
public void _ResetSettingsTimer() => gotoSettingsTimer = 0;
|
||
public void _EnterSettingsProcess()
|
||
{
|
||
gotoSettingsTimer += Time.deltaTime;
|
||
gotoSettingsTimer = Mathf.Min(gotoSettingsTimer, timeToEnterSettings);
|
||
if (gotoSettingsTimer == timeToEnterSettings)
|
||
SceneLoader.LoadScene("Settings");
|
||
}
|
||
public void _TryEnterGame()
|
||
{
|
||
if (freeplay || WalletManager.TryTakeMoney(price))
|
||
{
|
||
CountersSystem.Increment(CountersSystem.CounterType.TotalStarts);
|
||
CountersSystem.Increment(CountersSystem.CounterType.StartsFromMenu);
|
||
SceneLoader.LoadScene(GameplaySceneName);
|
||
}
|
||
}
|
||
#endregion
|
||
} |