131 lines
4.1 KiB
C#
131 lines
4.1 KiB
C#
using Periscope;
|
||
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] 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 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);
|
||
|
||
WalletManager.OnBalanceAdd += CheckEnoughMoney;
|
||
screenInstruction.SetActive(false);
|
||
CheckEnoughMoney(WalletManager.Balance);
|
||
|
||
initialized = true;
|
||
}
|
||
|
||
private void CheckEnoughMoney(int obj)
|
||
{
|
||
if (EnoughMoney)
|
||
{
|
||
WalletManager.OnBalanceAdd -= CheckEnoughMoney;
|
||
PeriscopeManager.MoveCenter();
|
||
screenInstruction.SetActive(true);
|
||
SoundManager.PlayReplica(ready);
|
||
}
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (!initialized) return;
|
||
|
||
if (EnoughMoney && In.GetButtonDown(InSignal.DownRight))
|
||
{
|
||
WalletManager.TakeMoney(ActualPrice);
|
||
CountersSystem.Increment(CountersSystem.CounterType.TotalStarts);
|
||
CountersSystem.Increment(CountersSystem.CounterType.StartsFromMenu);
|
||
SceneLoader.LoadScene(GameplaySceneName);
|
||
}
|
||
|
||
if (In.GetButtonDown(InSignal.Service))
|
||
SceneLoader.LoadScene("Settings");
|
||
|
||
PeriscopeProcess();
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
WalletManager.OnBalanceChanged -= UpdateUI;
|
||
WalletManager.OnBalanceAdd -= CheckEnoughMoney;
|
||
PeriscopeManager.StopAllForce();
|
||
}
|
||
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);
|
||
}
|
||
|
||
private void PeriscopeProcess()
|
||
{
|
||
if(PeriscopeManager.AutoRegime) return;
|
||
if(!EnoughMoney) return;
|
||
|
||
bool up = In.GetButton(InSignal.UpLeft) || Input.GetKey(KeyCode.UpArrow);
|
||
bool down = In.GetButton(InSignal.DownLeft) || Input.GetKey(KeyCode.DownArrow);
|
||
bool stop = (up == down);
|
||
|
||
if(stop)
|
||
PeriscopeManager.StopManually();
|
||
else if (up)
|
||
PeriscopeManager.UpManually();
|
||
else if (down)
|
||
PeriscopeManager.DownManually();
|
||
}
|
||
}
|