From 8a27a1fe3b52d9b4db6b52dfefad97899b8c5808 Mon Sep 17 00:00:00 2001 From: lvrossem Date: Thu, 2 Mar 2023 00:33:16 +0100 Subject: [PATCH] Write comments; some cleanup --- Assets/Scripts/SpellingBeeController.cs | 48 +++++++++++++++++-------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/Assets/Scripts/SpellingBeeController.cs b/Assets/Scripts/SpellingBeeController.cs index 8eea945..8ecc3e3 100644 --- a/Assets/Scripts/SpellingBeeController.cs +++ b/Assets/Scripts/SpellingBeeController.cs @@ -6,11 +6,13 @@ using TMPro; using UnityEngine; using UnityEngine.UI; +// JSON structure containing all themes/words [System.Serializable] public class ThemeList { public Theme[] themes; } +// Object representing part of the JSON containing word data [System.Serializable] public class Theme { public string name; @@ -18,17 +20,29 @@ public class Theme { public string[] words; } -public class SpellingBeeController : MonoBehaviour { +public class SpellingBeeController : MonoBehaviour { + // All of the words that can be used in this session private string[] words; + + // Where we currently are in the word private int currentIndex; + + // The word that is currently being spelled private string currentWord; + // All of the available themes private ThemeList themeList; + + // The theme we are currently using private Theme currentTheme; + + // The GameObjects representing the letters private GameObject[] letters; + // The Image component for displaying the appropriate sprite public Image image; - public string inputString; + + // The input field for testing purposes public TMP_InputField input; // Start is called before the first frame update @@ -39,18 +53,6 @@ public class SpellingBeeController : MonoBehaviour { SetRandomWord(); } - void SetRandomWord() { - currentIndex = 0; - - int randomIndex = UnityEngine.Random.Range(0, words.Length - 1); - string randomWord = words[randomIndex]; - - letters = new GameObject[randomWord.Length]; - currentWord = randomWord; - changeSprite(randomWord); - changeWord(randomWord); - } - // Update is called once per frame void Update() { if (input.text.Length > 0) { @@ -71,7 +73,21 @@ public class SpellingBeeController : MonoBehaviour { } } - void changeWord(string word) { + // Pick a new random word from words and initialize the needed variables + void SetRandomWord() { + currentIndex = 0; + + int randomIndex = UnityEngine.Random.Range(0, words.Length - 1); + string randomWord = words[randomIndex]; + + letters = new GameObject[randomWord.Length]; + currentWord = randomWord; + changeSprite(randomWord); + displayWord(randomWord); + } + + // Displays the word that needs to be spelled + void displayWord(string word) { int length = word.Length; int canvasWidth = 1920; int maxWidth = 1080; @@ -112,6 +128,7 @@ public class SpellingBeeController : MonoBehaviour { } } + // Change the image that is being displayed void changeSprite(string spriteName) { Image imageComponent = image.GetComponent(); @@ -122,6 +139,7 @@ public class SpellingBeeController : MonoBehaviour { image.sprite = sprite; } + // Loads the JSON file containing all of the themes void loadJson() { TextAsset themeJson = Resources.Load("SpellingBee/words"); string jsonString = themeJson.text;