Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;// For Stringbuilder
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- public class GameManager : MonoBehaviour {
- //list that will hold all of the words that are avaliable to be guessed
- public List<Word> wordList;
- //this is the word that is chosen to be guessed for
- public string wordToGuess;
- //this is whats actually displayed on the screen
- public Text wordDisplay;
- //what the player has guessed
- public string guess;
- //where the guess comes from
- public InputField inputText;
- //a list of all the letters that have already been used
- public List<string> usedLetterList;
- //where the used letter list will display
- public Text usedLetterDisplay;
- //what category the guessed word is in
- public Text categoryTxt;
- //the word that is partially guessed
- private string currentWord;
- //how many words there are
- public int wordCount;
- //number of lives tha player has left
- public int lives = 3;
- //the button in the game that allows the game to be replayed
- public GameObject playAgainButton;
- //boolean that checks if the game is over
- public bool gameOver = false;
- //button that can be pressed to make the hint text show
- public GameObject hintButton;
- //the actual hint text
- public Text hintText;
- //a button that will quit the game when called
- public GameObject quitGameButton;
- //number of points that the player has
- public int points;
- //how the points will be displayed
- public Text currentScore;
- //how the lives remaining will be displayed
- public Text currentLives;
- //the final score the player has, this is used so the final score may be displayed more prominantly
- public Text finalScore;
- //checks to see if when the game ends if the player won
- public bool winGame;
- //the time remaining
- //only used in timed mode
- public Timer time;
- //a string of allowed charachter, this makes it so that non alphabet charachters will not be counted against the player
- private string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
- // Use this for initialization, it will reset all of the variables and give the player the opportunity to enter the game or exit the game
- //will also pick a word to be tested for
- void Start ()
- {
- winGame = false;
- HideHint();
- HideScore();
- playAgainButton.SetActive(false);
- PickAWord();
- inputText.ActivateInputField();
- quitGameButton.SetActive(false);
- currentLives.text = "Lives: " + lives;
- currentScore.text = "Score: " + points;
- }
- //makes it so that the cursor is always in the input field so the player may always type
- private void Update()
- {
- inputText.ActivateInputField();
- }
- //makes it so that the word is hidden to the player
- void MaskWord(string input, char target)
- {
- StringBuilder sb = new StringBuilder(wordToGuess.Length);
- for(int i = 0; i < wordToGuess.Length; i++)
- {
- sb.Append(target);
- }
- currentWord = sb.ToString();
- //Update word to text on screen
- wordDisplay.text = currentWord;
- }
- //turns the *'s to letters as the letters are guessed
- void UnMaskWord(string input, char target)
- {
- //input.ToLower();
- StringBuilder sb = new StringBuilder(wordToGuess.Length);
- for (int i = 0; i < wordToGuess.Length; i++)
- {
- if(input.ToLower() == wordToGuess[i].ToString().ToLower())
- {
- sb.Append(target);
- wordCount++;
- }
- else
- {
- sb.Append(currentWord[i]);
- }
- }
- currentWord = sb.ToString();
- //Update word to text on screen
- currentWord = currentWord.Substring(0, 1).ToUpper() + currentWord.Substring(1).ToLower();
- wordDisplay.text = currentWord;
- //What condition can be checked to see if we've won or not?
- if(wordCount == wordToGuess.Length)
- {
- winGame = true;
- EndGame();
- }
- }
- //this picks the word to be guessed by the player
- void PickAWord()
- {
- int selectedWord = Random.Range(0, wordList.Count-1);
- Debug.Log("selectedWord int : " + selectedWord);
- wordToGuess = wordList[selectedWord].word;
- categoryTxt.text = wordList[selectedWord].category;
- hintText.text = wordList[selectedWord].hint;
- //wordList.Remove(wordList[selectedWord]);
- currentWord = wordToGuess;
- MaskWord(currentWord, '*');
- }
- //this compares the letter the player guesses with the word they are guessing for
- public void LetterGuess()
- {
- if(gameOver == false)
- {
- if (inputText.text != string.Empty)
- {
- guess = inputText.text.ToLower();
- AddLetterGuess();
- }
- }
- }
- //this checks if the letter guessed is in the word and if it is it will unmask the guessed letter
- //otherwise it will subtract a life
- void CheckWordForLetter()
- {
- if (wordToGuess.Contains(guess))
- {
- //Display letter
- UnMaskWord(guess, guess[0]);
- //Update word to text on screen
- //wordDisplay.text = wordToGuess;
- }
- else
- {
- lives--;
- currentLives.text = "Lives: " + lives;
- if(lives == 0)
- {
- EndGame();
- }
- }
- }
- //this tests the letters that are being used to see if theyre letters and if theyre actually in the word
- void AddLetterGuess()
- {
- if (!usedLetterList.Contains(guess) && (alphabet.Contains(guess)))
- {
- CheckWordForLetter();
- usedLetterList.Add(guess);
- string tempText = "";
- foreach (string str in usedLetterList)
- {
- tempText += str;
- }
- usedLetterDisplay.text = tempText;
- }
- ResetGuess();
- }
- //this will make it so that the player can enter a new guess
- void ResetGuess()
- {
- guess = string.Empty;
- inputText.text = string.Empty;
- inputText.ActivateInputField();
- }
- //this ends the game and makes it so that if they win they get a new word and if they lose the game ends
- public void EndGame()
- {
- if (winGame == true)
- {
- points++;
- //this will only be entered if they are playing the timed game mode
- if ((SceneManager.GetActiveScene()).name == "Timed Game Mode")
- {
- Invoke("Time.ResetTime()", 3);
- }
- currentScore.text = "Score: " + points;
- Debug.Log("User won");
- gameOver = true;
- wordDisplay.fontSize = 48;
- Invoke("ResetGame", 3);
- //this line is in here twice and it sholdnt be i think
- //Invoke("Time.ResetTime()", 3);
- inputText.gameObject.SetActive(false);
- }
- //this is if the player lost
- else
- {
- DisplayScore();
- categoryTxt.gameObject.SetActive(false);
- playAgainButton.SetActive(true);
- quitGameButton.SetActive(true);
- Debug.Log("User lost");
- gameOver = true;
- inputText.gameObject.SetActive(false);
- ResetScore();
- //should this line be here?
- }
- }
- //this will reset all of the used variables and give them the options to play again or quit
- public void ResetGame()
- {
- categoryTxt.gameObject.SetActive(true);
- playAgainButton.SetActive(false);
- quitGameButton.SetActive(false);
- winGame = false;
- gameOver = false;
- inputText.gameObject.SetActive(true);
- wordDisplay.fontSize = 36;
- lives = 3;
- if ((SceneManager.GetActiveScene()).name == "Timed Game Mode")
- {
- Debug.Log("it entered");
- time.ResetTime();
- }
- currentLives.text = "Lives: " + lives;
- usedLetterList.Clear();
- usedLetterDisplay.text = string.Empty;
- ResetGuess();
- wordCount = 0;
- HideHint();
- PickAWord();
- }
- //lets the player see a hint
- public void DisplayHint()
- {
- hintText.gameObject.SetActive(true);
- hintButton.SetActive(false);
- }
- //hides the hint
- public void HideHint()
- {
- hintText.gameObject.SetActive(false);
- hintButton.SetActive(true);
- }
- //shows the score the player has
- public void DisplayScore()
- {
- finalScore.gameObject.SetActive(true);
- finalScore.text = ("Final Score: " + points);
- }
- //hides the players score
- public void HideScore()
- {
- finalScore.gameObject.SetActive(false);
- }
- //when the player loses their score will reset
- public void ResetScore()
- {
- points = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement