130 lines
3.4 KiB
C#
130 lines
3.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
using System.Collections;
|
|
using RTLTMPro;
|
|
|
|
public class timerStarter : MonoBehaviour
|
|
{
|
|
public TextMeshProUGUI timerText;
|
|
public RTLTextMeshPro questionText;
|
|
|
|
public GameObject startButton;
|
|
public GameObject answerButton;
|
|
public GameObject resumeButton;
|
|
public GameObject endButton;
|
|
|
|
private float countdownTime = 300f;
|
|
private float timeLeft;
|
|
private Coroutine countdownCoroutine;
|
|
private bool isPaused = false;
|
|
private int cycleCount = 0;
|
|
private const int maxCycles = 4;
|
|
|
|
// Manual messages for each cycle
|
|
public string[] questions = {
|
|
"Question 1: What is your name?",
|
|
"Question 2: What is your quest?",
|
|
"Question 3: What is your favorite color?",
|
|
"Question 4: What is the airspeed velocity of an unladen swallow?"
|
|
};
|
|
|
|
void Start()
|
|
{
|
|
ResetUI();
|
|
}
|
|
|
|
public void OnStartButtonPressed()
|
|
{
|
|
cycleCount = 0;
|
|
timeLeft = countdownTime;
|
|
SetButtons(activeStart: false, activeAnswer: true, activeResume: false, activeEnd: false);
|
|
ShowNextQuestion();
|
|
countdownCoroutine = StartCoroutine(StartCountdown());
|
|
}
|
|
|
|
public void OnAnswerButtonPressed()
|
|
{
|
|
if (countdownCoroutine != null)
|
|
{
|
|
StopCoroutine(countdownCoroutine);
|
|
}
|
|
isPaused = true;
|
|
SetButtons(activeStart: false, activeAnswer: false, activeResume: true, activeEnd: false);
|
|
}
|
|
|
|
public void OnResumeButtonPressed()
|
|
{
|
|
cycleCount++;
|
|
if (cycleCount >= maxCycles)
|
|
{
|
|
SetButtons(activeStart: false, activeAnswer: false, activeResume: false, activeEnd: true);
|
|
}
|
|
else
|
|
{
|
|
ShowNextQuestion();
|
|
SetButtons(activeStart: false, activeAnswer: true, activeResume: false, activeEnd: false);
|
|
}
|
|
|
|
isPaused = false;
|
|
countdownCoroutine = StartCoroutine(StartCountdown());
|
|
}
|
|
|
|
public void OnEndButtonPressed()
|
|
{
|
|
ResetUI();
|
|
}
|
|
|
|
IEnumerator StartCountdown()
|
|
{
|
|
while (timeLeft > 0)
|
|
{
|
|
int minutes = Mathf.FloorToInt(timeLeft / 60);
|
|
int seconds = Mathf.FloorToInt(timeLeft % 60);
|
|
timerText.text = $"{minutes:D2}:{seconds:D2}";
|
|
yield return new WaitForSeconds(1f);
|
|
timeLeft -= 1f;
|
|
|
|
if (isPaused) yield break;
|
|
}
|
|
|
|
// If time runs out mid-cycle, reset everything
|
|
ResetUI();
|
|
}
|
|
|
|
void ShowNextQuestion()
|
|
{
|
|
if (cycleCount < questions.Length)
|
|
{
|
|
questionText.text = questions[cycleCount];
|
|
}
|
|
else
|
|
{
|
|
questionText.text = "No more questions.";
|
|
}
|
|
}
|
|
|
|
void ResetUI()
|
|
{
|
|
if (countdownCoroutine != null)
|
|
{
|
|
StopCoroutine(countdownCoroutine);
|
|
}
|
|
|
|
timerText.text = "00:00";
|
|
questionText.text = "Press Start to begin.";
|
|
timeLeft = countdownTime;
|
|
isPaused = false;
|
|
cycleCount = 0;
|
|
SetButtons(activeStart: true, activeAnswer: false, activeResume: false, activeEnd: false);
|
|
}
|
|
|
|
void SetButtons(bool activeStart, bool activeAnswer, bool activeResume, bool activeEnd)
|
|
{
|
|
startButton.SetActive(activeStart);
|
|
answerButton.SetActive(activeAnswer);
|
|
resumeButton.SetActive(activeResume);
|
|
endButton.SetActive(activeEnd);
|
|
}
|
|
}
|