using System.Collections; using System.Collections.Generic; using System.IO; using System; using TMPro; using UnityEngine; using UnityEngine.UI; 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; // 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) { checkChar(input.text[0]); } timerValue -= Time.deltaTime; 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().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; while (themeIndex < themeList.themes.Length) { Theme theme = themeList.themes[themeIndex]; if (theme.name == themeName) { return theme; } themeIndex++; } Debug.Log("Requested theme not found"); return null; } // 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); addSeconds(currentWord.Length * secondsPerLetter + 1); } // Displays the word that needs to be spelled void displayWord(string word) { int length = word.Length; int canvasWidth = 1920; int maxWidth = 1080; int spacing = maxWidth / (length - 1); letters = new GameObject[length]; int[] xvalues = new int[length]; for (int i = 0; i < length; i++) { xvalues[i] = (canvasWidth - maxWidth) / 2 + i * spacing; } for (int i = 0; i < length; i++) { GameObject colorBox = new GameObject("Letter box"); colorBox.transform.SetParent(gameObject.transform); letters[i] = colorBox; // Add an Image component to the new GameObject Image background = colorBox.AddComponent(); background.color = Color.red; background.sprite = UnityEditor.AssetDatabase.GetBuiltinExtraResource("UI/Skin/UISprite.psd"); // Create a new child GameObject to hold the Text component GameObject letterObject = new GameObject("Text"); letterObject.transform.SetParent(colorBox.transform); // Add a Text component to the new child GameObject Text letterComponent = letterObject.AddComponent(); letterComponent.text = Char.ToString(Char.ToUpper(word[i])); letterComponent.alignment = TextAnchor.MiddleCenter; letterComponent.fontSize = 60; // Set the font of the Text component letterComponent.font = Resources.GetBuiltinResource("Arial.ttf"); // Set the position of the parent GameObject colorBox.transform.position = new Vector2(xvalues[i], 450); } } // Change the image that is being displayed void changeSprite(string spriteName) { Image imageComponent = image.GetComponent(); // Load the new sprite from the Resources folder Sprite sprite = Resources.Load("SpellingBee/images/" + spriteName); // Set the new sprite as the Image component's source image image.sprite = sprite; } IEnumerator Wait() { yield return new WaitForSecondsRealtime(2); } }