sea-battle-vr/Assets/Scripts/Menu/MenuControllerOneRegime.cs

119 lines
3.9 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";
[SerializeField] private TextMeshProUGUI BalanceLabel;
[SerializeField] private TextMeshProUGUI GamesPayedLabel;
[SerializeField] private TextMeshProUGUI PriceLabel;
[SerializeField] private TextMeshProUGUI FreeplayLabel;
[SerializeField] PeriscopeController periscopeController;
[SerializeField] private string BalancePrefix = "Баланс: ";
[SerializeField] private string GamesPayedPrefix = "Оплачено игр: ";
[SerializeField] private GameObject _instructionCommon;
[SerializeField] private GameObject _instructionPeriscope;
[SerializeField] private GameObject screenInstruction;
[SerializeField] private AudioClip ready;
private int gamePrice;
private bool initialized = false;
private string valuta;
private bool _freeplay;
private float gotoSettingsTimer = 0;
[SerializeField] private float timeToEnterSettings = 1;
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();
periscopeController.allowControls = false;
WalletManager.OnBalanceChanged += UpdateUI;
UpdateUI(WalletManager.Balance);
WalletManager.OnBalanceAdd += CheckEnoughMoney;
screenInstruction.SetActive(false);
CheckEnoughMoney(WalletManager.Balance);
initialized = true;
}
private void CheckEnoughMoney(int obj)
{
if (EnoughMoney)
{
WalletManager.OnBalanceAdd -= CheckEnoughMoney;
periscopeController.MoveCenter();
periscopeController.allowControls = true;
screenInstruction.SetActive(true);
SoundManager.PlayReplica(ready);
}
}
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(WalletManager.TryTakeMoney(ActualPrice))
{
CountersSystem.Increment(CountersSystem.CounterType.TotalStarts);
CountersSystem.Increment(CountersSystem.CounterType.StartsFromMenu);
SceneLoader.LoadScene(GameplaySceneName);
}
}
private void OnDestroy()
{
WalletManager.OnBalanceChanged -= UpdateUI;
WalletManager.OnBalanceAdd -= CheckEnoughMoney;
}
public void UpdateUI(int balance)
{
if (_freeplay)
{
_instructionCommon.SetActive(false);
_instructionPeriscope.SetActive(true);
return;
}
string valutaPluralized = valuta == "руб." ? PluralizeUtil.Rubles(balance) : PluralizeUtil.Tokens(balance);
PriceLabel.text = $"Цена: {ActualPrice} {valutaPluralized}";
BalanceLabel.text = $"{BalancePrefix}{balance} {valutaPluralized}";
GamesPayedLabel.text = $"{GamesPayedPrefix}{balance / gamePrice}";
bool enoughMoney = balance >= gamePrice;
_instructionCommon.SetActive(!enoughMoney);
_instructionPeriscope.SetActive(enoughMoney);
}
private void InitLayout()
{
bool needPay = !_freeplay;
BalanceLabel.gameObject.SetActive(needPay);
GamesPayedLabel.gameObject.SetActive(needPay);
PriceLabel.gameObject.SetActive(needPay);
FreeplayLabel.gameObject.SetActive(_freeplay);
}
}