Add timer to spelling bee

This commit is contained in:
lvrossem
2023-03-02 17:11:57 +01:00
parent 359b1db905
commit 0abf2096e8
5 changed files with 226 additions and 35 deletions

View File

@@ -31,36 +31,65 @@ public class SpellingBeeController : MonoBehaviour {
// The input field for testing purposes
public TMP_InputField input;
// Current value of timer in seconds
private float timerValue;
// Timer display
public TMP_Text timerText;
// Amount of seconds user gets per letter of the current word
private int secondsPerLetter = 6;
// Start is called before the first frame update
void Start() {
themeList = ThemeLoader.loadJson();
currentTheme = findThemeByName(PlayerPrefs.GetString("themeName"));
words = currentTheme.words;
SetRandomWord();
addSeconds(currentWord.Length * secondsPerLetter + 1);
}
// Update is called once per frame
void Update() {
if (input.text.Length > 0) {
if (Char.ToUpper(input.text[0]) == Char.ToUpper(currentWord[currentIndex]) && input.text.Length == 1) {
letters[currentIndex].GetComponent<Image>().color = Color.green;
input.text = "";
currentIndex++;
checkChar(input.text[0]);
}
if (currentIndex >= currentWord.Length) {
for (int i = 0; i < currentWord.Length; i++) {
Destroy(letters[i]);
}
timerValue -= Time.deltaTime;
StartCoroutine(Wait());
SetRandomWord();
if (timerValue <= 0.0f)
{
timerValue = 0.0f;
// Handle end of timer
}
int minutes = Mathf.FloorToInt(timerValue / 60.0f);
int seconds = Mathf.FloorToInt(timerValue % 60.0f);
timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
void checkChar(char letter) {
if (Char.ToUpper(letter) == Char.ToUpper(currentWord[currentIndex]) && input.text.Length == 1) {
letters[currentIndex].GetComponent<Image>().color = Color.green;
input.text = "";
currentIndex++;
if (currentIndex >= currentWord.Length) {
for (int i = 0; i < currentWord.Length; i++) {
Destroy(letters[i]);
}
StartCoroutine(Wait());
SetRandomWord();
}
}
}
void addSeconds(int seconds) {
timerValue += (float) seconds;
}
Theme findThemeByName(string themeName) {
int themeIndex = 0;
@@ -87,6 +116,7 @@ public class SpellingBeeController : MonoBehaviour {
currentWord = randomWord;
changeSprite(randomWord);
displayWord(randomWord);
addSeconds(currentWord.Length * secondsPerLetter + 1);
}
// Displays the word that needs to be spelled