201 lines
6.5 KiB
C#
201 lines
6.5 KiB
C#
using System;
|
||
using System.Collections;
|
||
using RR.Common.Managers;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using TMPro;
|
||
|
||
using RR.Game2D.Core.Managers;
|
||
|
||
namespace RR.Common.UI
|
||
{
|
||
/// <summary>
|
||
/// Контроллер баннера проигрыша.
|
||
/// Модальное окно, которое отображается при поражении игрока.
|
||
/// </summary>
|
||
public class LoseBannerController : Modal
|
||
{
|
||
[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;
|
||
|
||
/// <summary>
|
||
/// Событие, вызываемое при нажатии "Продолжить" или закрытии баннера.
|
||
/// </summary>
|
||
public event Action ContinueClicked;
|
||
|
||
protected override void Awake()
|
||
{
|
||
base.Awake();
|
||
|
||
if (continueButton)
|
||
{
|
||
continueButton.onClick.AddListener(OnContinueButtonClicked);
|
||
}
|
||
}
|
||
|
||
protected override void OnEnable()
|
||
{
|
||
base.OnEnable();
|
||
|
||
_paymentManager = GameManager.Instance?.Payment;
|
||
|
||
if (_paymentManager != null)
|
||
{
|
||
int credits = _paymentManager.CreditsCount;
|
||
if (creditText)
|
||
{
|
||
creditText.text = string.Format(creditTextFormat, credits);
|
||
}
|
||
_paymentManager.OnCreditAdded.AddListener(UnlockContinueButton);
|
||
}
|
||
}
|
||
|
||
protected override void OnDisable()
|
||
{
|
||
// Остановим корутину, если объект деактивируется
|
||
if (_continueCountdownCoroutine != null)
|
||
{
|
||
StopCoroutine(_continueCountdownCoroutine);
|
||
_continueCountdownCoroutine = null;
|
||
}
|
||
|
||
if (_paymentManager != null)
|
||
{
|
||
_paymentManager.OnCreditAdded.RemoveListener(UnlockContinueButton);
|
||
}
|
||
|
||
base.OnDisable();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Показывает баннер проигрыша с указанным временем игры.
|
||
/// </summary>
|
||
/// <param name="gameTime">Время игры в секундах</param>
|
||
public void ShowLose(float gameTime)
|
||
{
|
||
if (loseText)
|
||
{
|
||
loseText.text = $"Время игры: {TimeFormatHelper.FormatTime(gameTime)}";
|
||
}
|
||
|
||
if (creditText && _paymentManager != null)
|
||
{
|
||
creditText.text = string.Format(creditTextFormat, _paymentManager.CreditsCount);
|
||
}
|
||
|
||
if (continueButtonCountdown <= 0f)
|
||
{
|
||
ContinueClicked?.Invoke();
|
||
GameManager.Instance?.SetPause(false);
|
||
GameManager.Instance?.LoadScene("MainMenu", false);
|
||
return;
|
||
}
|
||
|
||
if (continueButton)
|
||
{
|
||
continueButton.gameObject.SetActive(true);
|
||
continueButton.interactable = _paymentManager != null && _paymentManager.CreditsCount > 0;
|
||
}
|
||
|
||
Open();
|
||
|
||
// Остановим предыдущую корутину, если есть
|
||
if (_continueCountdownCoroutine != null)
|
||
{
|
||
StopCoroutine(_continueCountdownCoroutine);
|
||
}
|
||
|
||
_continueCountdownCoroutine = StartCoroutine(ContinueCountdownCoroutine(continueButtonCountdown));
|
||
}
|
||
|
||
private IEnumerator ContinueCountdownCoroutine(float duration)
|
||
{
|
||
float elapsed = 0f;
|
||
int initialSeconds = Mathf.RoundToInt(duration);
|
||
UpdateContinueText(initialSeconds);
|
||
|
||
while (elapsed < duration)
|
||
{
|
||
float startTime = Time.unscaledTime;
|
||
yield return null;
|
||
|
||
while (Time.unscaledTime - startTime < 1.0f)
|
||
{
|
||
yield return null;
|
||
}
|
||
|
||
elapsed += 1f;
|
||
int remaining = Mathf.Max(0, Mathf.RoundToInt(duration - elapsed));
|
||
UpdateContinueText(remaining);
|
||
}
|
||
|
||
// Таймер истёк — закрываем баннер и выходим в меню
|
||
Close();
|
||
ContinueClicked?.Invoke();
|
||
|
||
if (GameManager.Instance)
|
||
{
|
||
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 == null) return;
|
||
|
||
if (_paymentManager.IsFreePlayEnabled() || SettingsManager.Instance.GetCurrentGameSettings().isFreeGame)
|
||
{
|
||
if (continueButton)
|
||
{
|
||
continueButton.interactable = true;
|
||
}
|
||
return;
|
||
}
|
||
|
||
if (continueButton)
|
||
{
|
||
continueButton.interactable = _paymentManager.CreditsCount > 0;
|
||
}
|
||
}
|
||
|
||
private void OnContinueButtonClicked()
|
||
{
|
||
// При клике на "Продолжить" — остановим таймер и продолжим игру
|
||
if (_continueCountdownCoroutine != null)
|
||
{
|
||
StopCoroutine(_continueCountdownCoroutine);
|
||
_continueCountdownCoroutine = null;
|
||
}
|
||
|
||
Close();
|
||
ContinueClicked?.Invoke();
|
||
|
||
if (GameManager.Instance)
|
||
{
|
||
GameManager.Instance.SetPause(false);
|
||
GameManager.Instance.ContinueGame();
|
||
}
|
||
}
|
||
}
|
||
} |