Kinda sorta finished

This commit is contained in:
lvrossem
2023-03-02 19:29:37 +01:00
parent 630c94836f
commit 56f3d0c195
3 changed files with 844 additions and 43 deletions

View File

@@ -37,62 +37,106 @@ public class SpellingBeeController : MonoBehaviour {
// Timer display
public TMP_Text timerText;
// First score display
public TMP_Text correctWordsText;
// Second score display
public TMP_Text correctLettersText;
// The game over panel
public GameObject gameOverPanel;
public Button replayButton;
// Indicates if the game is still going
private bool gameOver;
// Amount of seconds user gets per letter of the current word
private int secondsPerLetter = 6;
private int secondsPerLetter = 1;
// Counter that keeps track of how many words have been spelled correctly
private int spelledLetters;
// Counter that keeps track of how many letters have been spelled correctly
private int spelledWords;
// Start is called before the first frame update
void Start() {
spelledLetters = 0;
// We use -1 instead of 0 so SetRandomWord can simply increment it each time
spelledWords = -1;
gameOver = false;
gameOverPanel.SetActive(false);
replayButton.onClick.AddListener(() => Start());
themeList = ThemeLoader.loadJson();
currentTheme = findThemeByName(PlayerPrefs.GetString("themeName"));
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) {
checkChar(input.text[0]);
void Update() {
if (!gameOver) {
if (input.text.Length > 0) {
CheckChar(input.text[0]);
}
timerValue -= Time.deltaTime;
if (timerValue <= 0.0f) {
timerValue = 0.0f;
ActivateGameOver();
}
int minutes = Mathf.FloorToInt(timerValue / 60.0f);
int seconds = Mathf.FloorToInt(timerValue % 60.0f);
timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
}
timerValue -= Time.deltaTime;
// Displays the game over panel and score values
void ActivateGameOver() {
DeleteWord();
correctLettersText.text = "Correctly spelled letters: " + spelledLetters.ToString();
correctWordsText.text = "Correctly spelled words: " + spelledWords.ToString();
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);
gameOverPanel.SetActive(true);
gameOverPanel.transform.SetAsLastSibling();
gameOver = true;
}
// Check if the correct char has been given as input
void checkChar(char letter) {
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 = "";
spelledLetters++;
currentIndex++;
if (currentIndex >= currentWord.Length) {
for (int i = 0; i < currentWord.Length; i++) {
Destroy(letters[i]);
}
DeleteWord();
StartCoroutine(Wait());
SetRandomWord();
}
}
}
void DeleteWord() {
for (int i = 0; i < currentWord.Length; i++) {
Destroy(letters[i]);
}
}
// Adds seconds to timer
void addSeconds(int seconds) {
void AddSeconds(int seconds) {
timerValue += (float) seconds;
}
// Find the chosen theme by its name
Theme findThemeByName(string themeName) {
Theme FindThemeByName(string themeName) {
int themeIndex = 0;
while (themeIndex < themeList.themes.Length) {
@@ -110,19 +154,20 @@ public class SpellingBeeController : MonoBehaviour {
// Pick a new random word from words and initialize the needed variables
void SetRandomWord() {
currentIndex = 0;
spelledWords++;
int randomIndex = UnityEngine.Random.Range(0, words.Length - 1);
string randomWord = words[randomIndex];
letters = new GameObject[randomWord.Length];
currentWord = randomWord;
changeSprite(randomWord);
displayWord(randomWord);
addSeconds(currentWord.Length * secondsPerLetter + 1);
ChangeSprite(randomWord);
DisplayWord(randomWord);
AddSeconds(currentWord.Length * secondsPerLetter + 1);
}
// Displays the word that needs to be spelled
void displayWord(string word) {
void DisplayWord(string word) {
int length = word.Length;
int canvasWidth = 1920;
int maxWidth = 1080;
@@ -164,7 +209,7 @@ public class SpellingBeeController : MonoBehaviour {
}
// Change the image that is being displayed
void changeSprite(string spriteName) {
void ChangeSprite(string spriteName) {
Image imageComponent = image.GetComponent<Image>();
// Load the new sprite from the Resources folder

View File

@@ -5,18 +5,14 @@ using UnityEngine.UI;
using TMPro;
public class SpellingBeeThemeSelectionController : MonoBehaviour {
public Button buttonPrefab;
public Transform parentTransform;
private int buttonHeight = 140;
private int buttonWidth = 600;
// Start is called before the first frame update
void Start()
{
int canvasWidth = 1920;
int canvasHeight = 1080;
private int canvasWidth = 1920;
private int canvasHeight = 1080;
// Start is called before the first frame update
void Start() {
ThemeList themeList = ThemeLoader.loadJson();
for (int i = 0; i < themeList.themes.Length; i++) {
@@ -60,8 +56,7 @@ public class SpellingBeeThemeSelectionController : MonoBehaviour {
}
}
void OnButtonClick(string clickedTheme)
{
void OnButtonClick(string clickedTheme) {
PlayerPrefs.SetString("themeName", clickedTheme);
ChangeSceneOnClick.loadScene("SpellingBee");
}