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

View File

@@ -11,7 +11,10 @@ public class SpellingBeeController : MonoBehaviour {
private string[] words; private string[] words;
// Where we currently are in the word // 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 // The word that is currently being spelled
private string currentWord; private string currentWord;
@@ -37,6 +40,9 @@ public class SpellingBeeController : MonoBehaviour {
// Timer display // Timer display
public TMP_Text timerText; public TMP_Text timerText;
// "Game over" or "You win!"
public TMP_Text endText;
// First score display // First score display
public TMP_Text correctWordsText; public TMP_Text correctWordsText;
@@ -44,12 +50,13 @@ public class SpellingBeeController : MonoBehaviour {
public TMP_Text correctLettersText; public TMP_Text correctLettersText;
// The game over panel // The game over panel
public GameObject gameOverPanel; public GameObject gameEndedPanel;
// Button for restarting the game
public Button replayButton; public Button replayButton;
// Indicates if the game is still going // Indicates if the game is still going
private bool gameOver; private bool gameEnded;
// Amount of seconds user gets per letter of the current word // Amount of seconds user gets per letter of the current word
// Set to 1 for testing; should be increased later // 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 // Start is called before the first frame update
void Start() { void Start() {
spelledLetters = 0; 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; spelledWords = -1;
gameOver = false; gameEnded = false;
wordIndex = 0;
input.text = ""; input.text = "";
gameOverPanel.SetActive(false); timerValue = 0.0f;
gameEndedPanel.SetActive(false);
replayButton.onClick.AddListener(() => Start()); replayButton.onClick.AddListener(() => Start());
themeList = ThemeLoader.loadJson(); themeList = ThemeLoader.loadJson();
currentTheme = FindThemeByName(PlayerPrefs.GetString("themeName")); currentTheme = FindThemeByName(PlayerPrefs.GetString("themeName"));
words = currentTheme.words; words = currentTheme.words;
SetRandomWord(); ShuffleWords();
SetNextWord();
} }
// Update is called once per frame // Update is called once per frame
void Update() { void Update() {
if (!gameOver) { if (!gameEnded) {
if (input.text.Length > 0) { if (input.text.Length > 0) {
CheckChar(input.text[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 // Displays the game over panel and score values
void ActivateGameOver() { void ActivateGameOver() {
DeleteWord(); DeleteWord();
endText.text = "GAME OVER";
correctLettersText.text = "Correctly spelled letters: " + spelledLetters.ToString(); correctLettersText.text = "Correctly spelled letters: " + spelledLetters.ToString();
correctWordsText.text = "Correctly spelled words: " + spelledWords.ToString(); correctWordsText.text = "Correctly spelled words: " + spelledWords.ToString();
gameOverPanel.SetActive(true); gameEndedPanel.SetActive(true);
gameOverPanel.transform.SetAsLastSibling(); 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 // 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) { if (Char.ToUpper(letter) == Char.ToUpper(currentWord[letterIndex]) && input.text.Length == 1) {
letters[currentIndex].GetComponent<Image>().color = Color.green; letters[letterIndex].GetComponent<Image>().color = Color.green;
input.text = ""; input.text = "";
spelledLetters++; spelledLetters++;
currentIndex++; letterIndex++;
if (currentIndex >= currentWord.Length) { if (letterIndex >= currentWord.Length) {
DeleteWord(); DeleteWord();
StartCoroutine(Wait()); StartCoroutine(Wait());
SetRandomWord(); SetNextWord();
} }
} }
} }
@@ -154,19 +209,22 @@ public class SpellingBeeController : MonoBehaviour {
return null; return null;
} }
// Pick a new random word from words and initialize the needed variables // Display next word in the series
void SetRandomWord() { void SetNextWord() {
currentIndex = 0;
spelledWords++; spelledWords++;
int randomIndex = UnityEngine.Random.Range(0, words.Length - 1); if (wordIndex < words.Length) {
string randomWord = words[randomIndex]; currentWord = words[wordIndex];
letters = new GameObject[randomWord.Length]; ChangeSprite(currentWord);
currentWord = randomWord; DisplayWord(currentWord);
ChangeSprite(randomWord);
DisplayWord(randomWord);
AddSeconds(currentWord.Length * secondsPerLetter + 1); AddSeconds(currentWord.Length * secondsPerLetter + 1);
letterIndex = 0;
wordIndex++;
} else {
ActivateWin();
}
} }
// Displays the word that needs to be spelled // Displays the word that needs to be spelled

View File

@@ -23,4 +23,10 @@ EditorBuildSettings:
- enabled: 1 - enabled: 1
path: Assets/Common/Scenes/SettingsScreen.unity path: Assets/Common/Scenes/SettingsScreen.unity
guid: 082b8c0d94b135d4a98f83699c92e45b guid: 082b8c0d94b135d4a98f83699c92e45b
- enabled: 1
path: Assets/SpellingBee/Scenes/SpellingBee.unity
guid: 652195ae9a0ff3ad18f6338db6a909bd
- enabled: 1
path: Assets/SpellingBee/Scenes/SpellingBeeThemeSelection.unity
guid: 2dfa1265c9d65014c90941ac4240a977
m_configObjects: {} m_configObjects: {}