173 lines
5.7 KiB
C#
173 lines
5.7 KiB
C#
using System;
|
||
using System.Collections;
|
||
using RR.Common.Managers;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using TMPro;
|
||
|
||
using RR.Common.UI;
|
||
using RR.Game2D.Core.Managers;
|
||
using UnityEngine.EventSystems;
|
||
|
||
public class LoseBannerController : MonoBehaviour
|
||
{
|
||
[SerializeField] private GameEndUIController bannersController;
|
||
|
||
[Header("Кнопки")]
|
||
[SerializeField] private TextMeshProUGUI loseText;
|
||
[SerializeField] private TextMeshProUGUI creditText;
|
||
[SerializeField] private Button continueButton;
|
||
[SerializeField] private TextMeshProUGUI continueButtonText;
|
||
[SerializeField] private float continueButtonCountdown = 30f;
|
||
|
||
[Header("Тексты")]
|
||
[SerializeField] private string creditTextFormat = "У вас осталось: {0}";
|
||
|
||
private PaymentManager _paymentManager;
|
||
private Coroutine _continueCountdownCoroutine;
|
||
|
||
public event Action ContinueClicked;
|
||
|
||
private void OnEnable()
|
||
{
|
||
bannersController.LoseBannerShown += OnLoseBannerShown;
|
||
|
||
_paymentManager = GameManager.Instance.Payment;
|
||
|
||
if (_paymentManager != null)
|
||
{
|
||
int credits = _paymentManager.CreditsCount;
|
||
creditText.text = string.Format(creditTextFormat, credits);
|
||
_paymentManager.OnCreditAdded.AddListener(UnlockContinueButton);
|
||
}
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
bannersController.LoseBannerShown -= OnLoseBannerShown;
|
||
// Остановим корутину, если объект деактивируется
|
||
if (_continueCountdownCoroutine != null)
|
||
{
|
||
StopCoroutine(_continueCountdownCoroutine);
|
||
_continueCountdownCoroutine = null;
|
||
}
|
||
|
||
if (_paymentManager != null)
|
||
{
|
||
_paymentManager.OnCreditAdded.RemoveListener(UnlockContinueButton);
|
||
}
|
||
}
|
||
|
||
private void Awake()
|
||
{
|
||
if (continueButton) continueButton.onClick.AddListener(OnContinueClicked);
|
||
|
||
// Инициализируем видимость/интерактивность кнопок в безопасное состояние
|
||
if (continueButton) continueButton.gameObject.SetActive(false);
|
||
if (continueButton && continueButtonText) continueButtonText.text = string.Empty;
|
||
}
|
||
|
||
private void OnLoseBannerShown(float gameTime)
|
||
{
|
||
// EventSystem.current.firstSelectedGameObject = continueButton.gameObject;
|
||
EventSystem.current.SetSelectedGameObject(continueButton.gameObject);
|
||
|
||
loseText.text = $"Время игры: {GameEndUIController.FormatTime(gameTime)}";
|
||
creditText.text = string.Format(creditTextFormat, _paymentManager.CreditsCount);
|
||
|
||
if (continueButtonCountdown <= 0f)
|
||
{
|
||
ContinueClicked?.Invoke();
|
||
GameManager.Instance.SetPause(false);
|
||
GameManager.Instance.LoadScene("MainMenu", false);
|
||
}
|
||
|
||
if (continueButton)
|
||
{
|
||
continueButton.gameObject.SetActive(true);
|
||
continueButton.interactable = _paymentManager.CreditsCount > 0;
|
||
}
|
||
|
||
// Остановим предыдущую корутину, если есть
|
||
if (_continueCountdownCoroutine != null)
|
||
{
|
||
StopCoroutine(_continueCountdownCoroutine);
|
||
_continueCountdownCoroutine = null;
|
||
}
|
||
|
||
_continueCountdownCoroutine = StartCoroutine(ContinueCountdownCoroutine(continueButtonCountdown));
|
||
}
|
||
|
||
private IEnumerator ContinueCountdownCoroutine(float duration)
|
||
{
|
||
|
||
float elapsed = 0f;
|
||
// Обновляем сразу первоначальный текст
|
||
int initialSeconds = Mathf.RoundToInt(duration);
|
||
UpdateContinueText(initialSeconds);
|
||
|
||
while (elapsed < duration)
|
||
{
|
||
// Using yield return null and manual timing with unscaled time for more reliable updates
|
||
float startTime = Time.unscaledTime;
|
||
yield return null;
|
||
|
||
// Wait for approximately 1 second using unscaled time
|
||
while (Time.unscaledTime - startTime < 1.0f)
|
||
{
|
||
yield return null;
|
||
}
|
||
|
||
elapsed += 1f;
|
||
int remaining = Mathf.Max(0, Mathf.RoundToInt(duration - elapsed));
|
||
UpdateContinueText(remaining);
|
||
}
|
||
|
||
if (continueButton)
|
||
{
|
||
ContinueClicked?.Invoke();
|
||
GameManager.Instance.SetPause(false);
|
||
GameManager.Instance.LoadScene("MainMenu", false);
|
||
}
|
||
|
||
_continueCountdownCoroutine = null;
|
||
}
|
||
|
||
private void UpdateContinueText(int secondsRemaining)
|
||
{
|
||
if (continueButtonText)
|
||
continueButtonText.text = $"Продолжить? ({secondsRemaining})";
|
||
}
|
||
|
||
private void UnlockContinueButton(int newCredits)
|
||
{
|
||
if (_paymentManager.IsFreePlayEnabled() || SettingsManager.Instance.GetCurrentGameSettings().isFreeGame)
|
||
{
|
||
if (continueButton)
|
||
{
|
||
continueButton.interactable = true;
|
||
}
|
||
return;
|
||
}
|
||
|
||
if (continueButton)
|
||
{
|
||
continueButton.interactable = _paymentManager.CreditsCount > 0;
|
||
}
|
||
}
|
||
|
||
private void OnContinueClicked()
|
||
{
|
||
// При клике на "Продолжить" — остановим таймер и продолжим игру
|
||
if (_continueCountdownCoroutine != null)
|
||
{
|
||
StopCoroutine(_continueCountdownCoroutine);
|
||
_continueCountdownCoroutine = null;
|
||
}
|
||
|
||
ContinueClicked?.Invoke();
|
||
GameManager.Instance.SetPause(false);
|
||
GameManager.Instance.ContinueGame();
|
||
}
|
||
}
|