task timer + logic
This commit is contained in:
159
Unity/Assets/Scripts/TaskTimer.cs
Normal file
159
Unity/Assets/Scripts/TaskTimer.cs
Normal file
@ -0,0 +1,159 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.SceneManagement;
|
||||
using TMPro;
|
||||
|
||||
public class TaskTimer : MonoBehaviour
|
||||
{
|
||||
[Header("Timer Settings")]
|
||||
[Tooltip("Total time for the task in seconds")]
|
||||
public float taskDuration = 300f; // 5 minutes default
|
||||
|
||||
[Tooltip("Start timer automatically on scene load")]
|
||||
public bool autoStart = true;
|
||||
|
||||
[Tooltip("Show warning when time is low (in seconds)")]
|
||||
public float warningThreshold = 30f;
|
||||
|
||||
[Header("UI References")]
|
||||
[Tooltip("TextMeshPro component to display timer (recommended for VR)")]
|
||||
public TextMeshProUGUI timerTextTMP;
|
||||
|
||||
[Tooltip("Legacy UI Text component (if not using TextMeshPro)")]
|
||||
public Text timerTextLegacy;
|
||||
|
||||
[Header("Visual Settings")]
|
||||
[Tooltip("Normal color for timer text")]
|
||||
public Color normalColor = Color.white;
|
||||
|
||||
[Tooltip("Warning color when time is running low")]
|
||||
public Color warningColor = Color.red;
|
||||
|
||||
[Tooltip("Format: 'MM:SS' or 'HH:MM:SS'")]
|
||||
public bool showHours = false;
|
||||
|
||||
private float timeRemaining;
|
||||
private bool isRunning;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
timeRemaining = taskDuration;
|
||||
|
||||
if (autoStart)
|
||||
{
|
||||
StartTimer();
|
||||
}
|
||||
|
||||
UpdateTimerDisplay();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!isRunning)
|
||||
return;
|
||||
|
||||
timeRemaining -= Time.deltaTime;
|
||||
|
||||
// Check if time is up
|
||||
if (timeRemaining <= 0)
|
||||
{
|
||||
timeRemaining = 0;
|
||||
OnTimerExpired();
|
||||
}
|
||||
|
||||
UpdateTimerDisplay();
|
||||
}
|
||||
|
||||
public void StartTimer()
|
||||
{
|
||||
isRunning = true;
|
||||
Debug.Log($"TaskTimer: Timer started for {taskDuration} seconds");
|
||||
}
|
||||
|
||||
public void PauseTimer()
|
||||
{
|
||||
isRunning = false;
|
||||
Debug.Log("TaskTimer: Timer paused");
|
||||
}
|
||||
|
||||
public void ResumeTimer()
|
||||
{
|
||||
isRunning = true;
|
||||
Debug.Log("TaskTimer: Timer resumed");
|
||||
}
|
||||
|
||||
public void ResetTimer()
|
||||
{
|
||||
timeRemaining = taskDuration;
|
||||
UpdateTimerDisplay();
|
||||
Debug.Log("TaskTimer: Timer reset");
|
||||
}
|
||||
|
||||
public void AddTime(float seconds)
|
||||
{
|
||||
timeRemaining += seconds;
|
||||
Debug.Log($"TaskTimer: Added {seconds} seconds. New time: {timeRemaining}");
|
||||
}
|
||||
|
||||
private void UpdateTimerDisplay()
|
||||
{
|
||||
string timeText = FormatTime(timeRemaining);
|
||||
|
||||
// Update TextMeshPro
|
||||
if (timerTextTMP != null)
|
||||
{
|
||||
timerTextTMP.text = timeText;
|
||||
timerTextTMP.color = timeRemaining <= warningThreshold ? warningColor : normalColor;
|
||||
}
|
||||
|
||||
// Update Legacy Text
|
||||
if (timerTextLegacy != null)
|
||||
{
|
||||
timerTextLegacy.text = timeText;
|
||||
timerTextLegacy.color = timeRemaining <= warningThreshold ? warningColor : normalColor;
|
||||
}
|
||||
}
|
||||
|
||||
private string FormatTime(float time)
|
||||
{
|
||||
int hours = Mathf.FloorToInt(time / 3600f);
|
||||
int minutes = Mathf.FloorToInt((time % 3600f) / 60f);
|
||||
int seconds = Mathf.FloorToInt(time % 60f);
|
||||
|
||||
if (showHours)
|
||||
{
|
||||
return string.Format("{0:00}:{1:00}:{2:00}", hours, minutes, seconds);
|
||||
}
|
||||
else
|
||||
{
|
||||
int totalMinutes = Mathf.FloorToInt(time / 60f);
|
||||
return string.Format("{0:00}:{1:00}", totalMinutes, seconds);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTimerExpired()
|
||||
{
|
||||
isRunning = false;
|
||||
Debug.Log("TaskTimer: Time expired! Returning to Lobby...");
|
||||
|
||||
// Load Lobby scene
|
||||
SceneManager.LoadScene("Lobby");
|
||||
}
|
||||
|
||||
// Public methods for external control
|
||||
public float GetTimeRemaining()
|
||||
{
|
||||
return timeRemaining;
|
||||
}
|
||||
|
||||
public bool IsRunning()
|
||||
{
|
||||
return isRunning;
|
||||
}
|
||||
|
||||
public float GetProgress()
|
||||
{
|
||||
return 1f - (timeRemaining / taskDuration);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user