sea-battle-vr/Assets/Scripts/Menu/MenuControllerOneRegime.cs
2026-06-11 11:19:22 +03:00

107 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 _instructionCommon;
[SerializeField] private CanvasGroup _instructionPeriscope;
[SerializeField] PeriscopeController periscopeController;
[SerializeField] private MenuAnimation menuAnimation;
[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;
menuAnimation.Init(EnoughMoney);
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;
menuAnimation.PerformTransition();
SoundManager.PlayReplica(ready);
}
private void InitLayout()
{
bool needPay = !freeplay;
BalanceLabel.enabled = needPay;
PriceLabel.enabled = needPay;
FreeplayLabel.enabled = freeplay;
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
}