69 lines
1.6 KiB
C#
69 lines
1.6 KiB
C#
using TMPro;
|
||
using UnityEngine;
|
||
|
||
public class ResultPanelTimeout : MonoBehaviour
|
||
{
|
||
[SerializeField] private TMP_Text _countdownText;
|
||
[SerializeField, Min(0f)] private float _timeoutSeconds = 20f;
|
||
|
||
private float _deadline;
|
||
private int _displayedSeconds = -1;
|
||
private bool _isRunning;
|
||
|
||
private void OnEnable()
|
||
{
|
||
_isRunning = false;
|
||
_displayedSeconds = -1;
|
||
SetCountdownText(Mathf.CeilToInt(_timeoutSeconds));
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (!_isRunning)
|
||
return;
|
||
|
||
RefreshCountdown();
|
||
|
||
if (Time.realtimeSinceStartup < _deadline)
|
||
return;
|
||
|
||
_isRunning = false;
|
||
ControlsPerformer.Instance?.SetControlable(null);
|
||
gameObject.SetActive(false);
|
||
GameManager.Instance?.GotoMenu();
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
_isRunning = false;
|
||
}
|
||
|
||
public void Cancel()
|
||
{
|
||
_isRunning = false;
|
||
}
|
||
|
||
public void StartCountdown()
|
||
{
|
||
_deadline = Time.realtimeSinceStartup + _timeoutSeconds;
|
||
_displayedSeconds = -1;
|
||
_isRunning = true;
|
||
RefreshCountdown();
|
||
}
|
||
|
||
private void RefreshCountdown()
|
||
{
|
||
int seconds = Mathf.CeilToInt(Mathf.Max(0f, _deadline - Time.realtimeSinceStartup));
|
||
SetCountdownText(seconds);
|
||
}
|
||
|
||
private void SetCountdownText(int seconds)
|
||
{
|
||
if (_countdownText == null || seconds == _displayedSeconds)
|
||
return;
|
||
|
||
_displayedSeconds = seconds;
|
||
_countdownText.text = $"Выход в меню через {seconds} с";
|
||
}
|
||
}
|