JohnnyTurbo

Untitled

Oct 7th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.26 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;               // Included to modify Unity UI components
  6. using UnityEngine.SceneManagement;  // Included to load into different scenes
  7.  
  8. /// <summary>
  9. /// GameController class is attached to one GameObject only
  10. /// This keeps track of all the variables in the game such as number of demons killed
  11. /// and the amount of time the player has been playing for. Also contains code for
  12. /// displaying UI on the HUD.
  13. /// </summary>
  14. public class GameController : MonoBehaviour
  15. {
  16.     // Simple implementation of a Singleton. See MusicController.cs for a better
  17.     // way to implement a Singleton class.
  18.     public static GameController instance;
  19.  
  20.     // Public GameObjects we set in the Unity Inspector
  21.     public GameObject demonContainer, hudContainer, gameOverPanel;
  22.  
  23.     // Public Text components we set in the Unity Inspector
  24.     public Text demonCounter, countdownText;
  25.     //public Text timeCounter;
  26.  
  27.     // True if the game is playing, false if it is not
  28.     // Returns false during countdown at beginning and after the last Demon is destroyed
  29.     // Other classes can read this variable, but it can only be set within this class
  30.     public bool gamePlaying { get; private set; }
  31.  
  32.     // Number of seconds to count down before starting the game
  33.     int countdownTime;
  34.  
  35.     // Number of Demons in the scene and number of demons we've destroyed
  36.     private int numTotalDemons, numSlayedDemons;
  37.  
  38.     // Time in seconds of when the game began and how long the game has been playing
  39.     private float startTime, elapsedTime;
  40.  
  41.     // Amount of time the game has been playing. Can be formatted nicely
  42.     //TimeSpan timePlaying;
  43.  
  44.     /// <summary>
  45.     /// Called before the start function when the scene is loaded
  46.     /// </summary>
  47.     private void Awake()
  48.     {
  49.         // Assigns this object to the public static instance (singleton)
  50.         // See the MusicController.cs for a better implementation of a singleton
  51.         instance = this;
  52.     }
  53.  
  54.     /// <summary>
  55.     /// Start is called before the first frame update
  56.     /// </summary>
  57.     private void Start()
  58.     {
  59.         // Gets the number of total demons in the world based off the number of children in the demonContainer GameObject
  60.         numTotalDemons = demonContainer.transform.childCount;
  61.  
  62.         // At the beginning of the level, the player has defeated 0 demons
  63.         numSlayedDemons = 0;
  64.  
  65.         // Initialize the Demon counter UI
  66.         demonCounter.text = "Demons: 0 / " + numTotalDemons;
  67.  
  68.         // Initialize the time counter UI
  69.         //timeCounter.text = "Time: 00:00.00";
  70.  
  71.         // Game is set to false until countdown completes
  72.         gamePlaying = false;
  73.  
  74.         // Begins the countdown at the start of the game
  75.         //StartCoroutine(CountdownToStart());
  76.     }
  77.  
  78.     /// <summary>
  79.     /// Sets some variables at the start of the game
  80.     /// </summary>
  81.     public void BeginGame()
  82.     {
  83.         // Game is now playing and player can move around
  84.         gamePlaying = true;
  85.  
  86.         TimerController.instance.BeginTimer();
  87.     }
  88.  
  89.     /// <summary>
  90.     /// Update is called once per frame
  91.     /// </summary>
  92.  
  93.     /*
  94.     private void Update()
  95.     {
  96.         // If the game is in a playing state ...
  97.         if (gamePlaying)
  98.         {
  99.             // ... Calculate the time elapsed since the start of the game
  100.             elapsedTime = Time.time - startTime;
  101.  
  102.             // Generate a TimeSpan from the number of seconds the game has been going on for
  103.             timePlaying = TimeSpan.FromSeconds(elapsedTime);
  104.  
  105.             // Nicely formatted string of the time the game has been playing
  106.             // Example format - Time: 01:23.45
  107.             string timePlayingStr = "Time: " + timePlaying.ToString("mm':'ss'.'ff");
  108.  
  109.             // Sets the time counter UI to our nicely formatted string
  110.             timeCounter.text = timePlayingStr;
  111.         }
  112.     }
  113.     */
  114.  
  115.     /// <summary>
  116.     /// Called from the DemonController.cs when a Demon is defeated
  117.     /// </summary>
  118.     public void SlayDemon()
  119.     {
  120.         // Increment the number of Demons slayed by 1
  121.         numSlayedDemons++;
  122.  
  123.         // Creates a string to display for the number of demons we have defeated out of the total number of Demons
  124.         // Example format - Demons: 4 / 18
  125.         string demonCounterStr = "Demons: " + numSlayedDemons + " / " + numTotalDemons;
  126.        
  127.         // Sets the Demon counter UI to the string we generated above
  128.         demonCounter.text = demonCounterStr;
  129.  
  130.         // Checks if we have defeated all the demons in the level
  131.         if(numSlayedDemons >= numTotalDemons)
  132.         {
  133.             // Calls the End Game function
  134.             EndGame();
  135.         }
  136.     }
  137.  
  138.     /// <summary>
  139.     /// Called when all demons have been defeated
  140.     /// </summary>
  141.     private void EndGame()
  142.     {
  143.         // Game is no longer playing so player cannot input anything on the controls
  144.         gamePlaying = false;
  145.         TimerController.instance.EndTimer();
  146.         // Call the ShowGameOverScreen() function in 1.25 seconds
  147.         Invoke("ShowGameOverScreen", 1.25f);
  148.     }
  149.  
  150.     /// <summary>
  151.     /// Displays the Game Over screen at the end of the level
  152.     /// </summary>
  153.     private void ShowGameOverScreen()
  154.     {
  155.         // Enable the Game Over screen
  156.         gameOverPanel.SetActive(true);
  157.  
  158.         // Disable the HUD
  159.         hudContainer.SetActive(false);
  160.  
  161.         // Creates a nicely formatted time string for the final time
  162.         string timePlayingStr = "Time: ";// + timePlaying.ToString("mm':'ss'.'ff");
  163.  
  164.         // Sets the final time UI component on the Game Over screen
  165.         gameOverPanel.transform.Find("FinalTimeText").GetComponent<Text>().text = timePlayingStr;
  166.  
  167.         // Selects the try again button for controller input
  168.         gameOverPanel.transform.Find("RestartButton").GetComponent<Button>().Select();
  169.     }
  170.  
  171.     /// <summary>
  172.     /// Provides a graphical countdown at the beginning of the game
  173.     /// </summary>
  174.     IEnumerator CountdownToStart()
  175.     {
  176.         // While the countdown time is greater than zero...
  177.         while (countdownTime > 0)
  178.         {
  179.             // Set the countdown UI component to the current countdown time integer
  180.             countdownText.text = countdownTime.ToString();
  181.  
  182.             // Return in exactly 1 second
  183.             yield return new WaitForSeconds(1f);
  184.  
  185.             // Decrement the countdown time integer by 1
  186.             countdownTime--;
  187.         }
  188.  
  189.         // Once the countdown timer reaches 0, call the BeginGame() fucntion
  190.         BeginGame();
  191.  
  192.         // Sets the countdown UI to "GO!"
  193.         countdownText.text = "GO!";
  194.  
  195.         // Return in exactly 1 second
  196.         yield return new WaitForSeconds(1f);
  197.  
  198.         // Disable the countdown UI to hid the "GO!" text
  199.         countdownText.gameObject.SetActive(false);
  200.     }
  201.  
  202.     /// <summary>
  203.     /// Called when one of the buttons on the Game Over screen is pressed
  204.     /// </summary>
  205.     /// <param name="levelToLoad"> Specify the level that should be loaded </param>
  206.     public void OnButtonLoadLevel(string levelToLoad)
  207.     {
  208.         // Loads the specified scene
  209.         SceneManager.LoadScene(levelToLoad);
  210.     }
  211. }
Add Comment
Please, Sign In to add comment