Add winning screen

This commit is contained in:
lvrossem
2023-03-02 22:28:54 +01:00
parent e00fb02f3b
commit 657777ad62
3 changed files with 94 additions and 29 deletions

View File

@@ -1529,7 +1529,7 @@ GameObject:
- component: {fileID: 1502459771}
- component: {fileID: 1502459770}
m_Layer: 5
m_Name: Game over
m_Name: Game ended
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
@@ -1774,9 +1774,10 @@ MonoBehaviour:
image: {fileID: 1338727891}
input: {fileID: 2029045139}
timerText: {fileID: 1843239267}
endText: {fileID: 1502459770}
correctWordsText: {fileID: 1769924966}
correctLettersText: {fileID: 978093276}
gameOverPanel: {fileID: 757133117}
gameEndedPanel: {fileID: 757133117}
replayButton: {fileID: 1346891278}
--- !u!1 &1769924964
GameObject:
@@ -1925,7 +1926,7 @@ GameObject:
- component: {fileID: 1843239268}
- component: {fileID: 1843239267}
m_Layer: 5
m_Name: Text (TMP)
m_Name: Timer
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0

View File

@@ -11,7 +11,10 @@ public class SpellingBeeController : MonoBehaviour {
private string[] words;
// Where we currently are in the word
private int currentIndex;
private int letterIndex;
// Where we currently are in the word list
private int wordIndex;
// The word that is currently being spelled
private string currentWord;
@@ -37,6 +40,9 @@ public class SpellingBeeController : MonoBehaviour {
// Timer display
public TMP_Text timerText;
// "Game over" or "You win!"
public TMP_Text endText;
// First score display
public TMP_Text correctWordsText;
@@ -44,12 +50,13 @@ public class SpellingBeeController : MonoBehaviour {
public TMP_Text correctLettersText;
// The game over panel
public GameObject gameOverPanel;
public GameObject gameEndedPanel;
// Button for restarting the game
public Button replayButton;
// Indicates if the game is still going
private bool gameOver;
private bool gameEnded;
// Amount of seconds user gets per letter of the current word
// Set to 1 for testing; should be increased later
@@ -64,22 +71,25 @@ public class SpellingBeeController : MonoBehaviour {
// 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
// We use -1 instead of 0 so SetNextWord can simply increment it each time
spelledWords = -1;
gameOver = false;
gameEnded = false;
wordIndex = 0;
input.text = "";
gameOverPanel.SetActive(false);
timerValue = 0.0f;
gameEndedPanel.SetActive(false);
replayButton.onClick.AddListener(() => Start());
themeList = ThemeLoader.loadJson();
currentTheme = FindThemeByName(PlayerPrefs.GetString("themeName"));
words = currentTheme.words;
SetRandomWord();
ShuffleWords();
SetNextWord();
}
// Update is called once per frame
void Update() {
if (!gameOver) {
if (!gameEnded) {
if (input.text.Length > 0) {
CheckChar(input.text[0]);
}
@@ -98,30 +108,75 @@ public class SpellingBeeController : MonoBehaviour {
}
}
// Randomly shuffle the list of words
void ShuffleWords() {
for (int i = words.Length - 1; i > 0; i--) {
// Generate a random index between 0 and i (inclusive)
int j = UnityEngine.Random.Range(0, i + 1);
// Swap the values at indices i and j
string temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
// Displays the game over panel and score values
void ActivateGameOver() {
DeleteWord();
endText.text = "GAME OVER";
correctLettersText.text = "Correctly spelled letters: " + spelledLetters.ToString();
correctWordsText.text = "Correctly spelled words: " + spelledWords.ToString();
gameOverPanel.SetActive(true);
gameOverPanel.transform.SetAsLastSibling();
gameEndedPanel.SetActive(true);
gameEndedPanel.transform.SetAsLastSibling();
gameOver = true;
gameEnded = true;
}
// Display win screen
void ActivateWin() {
DeleteWord();
endText.text = "YOU WIN!";
correctLettersText.text = "Your time: " + spelledLetters.ToString();
int totalWordsDuration = 0;
foreach (string word in words) {
totalWordsDuration += word.Length * secondsPerLetter + 1;
}
// How much time was spent by the player
int spentTime = totalWordsDuration - (int) timerValue;
int seconds = spentTime % 60;
int minutes = spentTime / 60;
if (minutes == 0) {
correctLettersText.text = "Your time: " + seconds + " seconds";
} else {
correctLettersText.text = "Your time: " + minutes + " minutes and " + seconds + " seconds";
}
correctWordsText.text = "";
gameEndedPanel.SetActive(true);
gameEndedPanel.transform.SetAsLastSibling();
gameEnded = true;
}
// Check if the correct char has been given as input
void CheckChar(char letter) {
if (Char.ToUpper(letter) == Char.ToUpper(currentWord[currentIndex]) && input.text.Length == 1) {
letters[currentIndex].GetComponent<Image>().color = Color.green;
if (Char.ToUpper(letter) == Char.ToUpper(currentWord[letterIndex]) && input.text.Length == 1) {
letters[letterIndex].GetComponent<Image>().color = Color.green;
input.text = "";
spelledLetters++;
currentIndex++;
letterIndex++;
if (currentIndex >= currentWord.Length) {
if (letterIndex >= currentWord.Length) {
DeleteWord();
StartCoroutine(Wait());
SetRandomWord();
SetNextWord();
}
}
}
@@ -154,19 +209,22 @@ public class SpellingBeeController : MonoBehaviour {
return null;
}
// Pick a new random word from words and initialize the needed variables
void SetRandomWord() {
currentIndex = 0;
// Display next word in the series
void SetNextWord() {
spelledWords++;
int randomIndex = UnityEngine.Random.Range(0, words.Length - 1);
string randomWord = words[randomIndex];
if (wordIndex < words.Length) {
currentWord = words[wordIndex];
letters = new GameObject[randomWord.Length];
currentWord = randomWord;
ChangeSprite(randomWord);
DisplayWord(randomWord);
AddSeconds(currentWord.Length * secondsPerLetter + 1);
ChangeSprite(currentWord);
DisplayWord(currentWord);
AddSeconds(currentWord.Length * secondsPerLetter + 1);
letterIndex = 0;
wordIndex++;
} else {
ActivateWin();
}
}
// Displays the word that needs to be spelled