coding_giants

l16 - GameManager

Mar 26th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4.  
  5. public class GameManager : MonoBehaviour
  6. {
  7.     //Game Manager Instance
  8.     public static GameManager instance;
  9.     //List of bricks
  10.     public List<GameObject> bricks = new List<GameObject>();
  11.     //Our ball script
  12.     public ArcanoidBall ball;
  13.     //is the game in progress?
  14.     bool gameRun = false;
  15.  
  16.     void Awake()
  17.     {
  18.         //Assigning an object to instance. Creating Singleton
  19.         if(instance == null)
  20.         {
  21.             instance = this;
  22.         }
  23.  
  24.         //Getting all Bricks and (by Brick tag) and adding them to the list
  25.         bricks.AddRange(GameObject.FindGameObjectsWithTag("Brick"));
  26.        
  27.     }
  28.  
  29.     void Update()
  30.     {
  31.         //Starting the game when clicking space (if it's not on yet)
  32.         if (Input.GetKeyDown(KeyCode.Space) && !gameRun)
  33.         {
  34.             ball.RunBall();
  35.             gameRun = true;
  36.         }
  37.  
  38.         //Ending the game when all bricks are gone
  39.         if(gameRun && bricks.Count == 0)
  40.         {
  41.             EndGame(true);
  42.         }
  43.  
  44.         //If the game is over, reset the scene with R key
  45.         if(!gameRun)
  46.         {
  47.             if(Input.GetKeyDown(KeyCode.R))
  48.             {
  49.                 SceneManager.LoadScene(0);
  50.             }
  51.         }
  52.     }
  53.  
  54.     //Ending game function
  55.     public void EndGame(bool win)
  56.     {
  57.         gameRun = false;
  58.         string txt = win ? "Wygrana!" : "Przegrana!";
  59.         Debug.Log(txt);
  60.         ball.StopBall();
  61.     }
  62. }
Add Comment
Please, Sign In to add comment