sea-battle/Assets/Scripts/UI/TurnTimerUI.cs

65 lines
1.3 KiB
C#
Raw Permalink 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 UnityEngine;
using UnityEngine.UI;
public class TurnTimerUI : MonoBehaviour
{
public Image timerBar; // сам бар
public float maxTime = 30f; // время таймера
private float currentTime;
private bool isRunning = false;
public GameManager gameManager;
private float originalWidth;
void Awake()
{
RectTransform rt = timerBar.rectTransform;
// Якорим и смещаем сверху
//rt.anchorMin = new Vector2(0.5f, 1f);
//rt.anchorMax = new Vector2(0.5f, 1f);
//rt.pivot = new Vector2(0.5f, 1f);
originalWidth = rt.sizeDelta.y;
StartTimer();
}
public void StartTimer()
{
currentTime = maxTime;
isRunning = true;
gameObject.SetActive(true);
}
public void StopTimer()
{
isRunning = false;
gameObject.SetActive(false);
}
public void ResumeTimer()
{
isRunning = true;
gameObject.SetActive(true);
}
void Update()
{
if (!isRunning) return;
currentTime -= Time.deltaTime;
timerBar.fillAmount = Mathf.Clamp01(currentTime / maxTime);
if (currentTime <= 0f)
{
isRunning = false;
gameManager.EndPlayerTurn(); // Переход хода к боту
}
}
}