coding_giants

l16 - GameManager

Mar 26th, 2024
50
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. using TMPro;
  5.  
  6. public class GameManager : MonoBehaviour
  7. {
  8.     //Game Manager Instance
  9.     public static GameManager instance;
  10.     //List of bricks
  11.     public List<GameObject> bricks = new List<GameObject>();
  12.     //Our ball script
  13.     public ArcanoidBall ball;
  14.     //is the game in progress?
  15.     bool gameRun = false;
  16.  
  17.     public GameObject EndGameText;
  18.     public TMP_Text bricksCounter;
  19.     public TMP_Text livesCounter;
  20.     public TMP_Text levelCounter;
  21.     public TMP_Text winText;
  22.  
  23.     int currentLevel;
  24.     public int maxLevel = 3;
  25.     public BricksGenerator brickList;
  26.     public int lives;
  27.  
  28.  
  29.     void Awake()
  30.     {
  31.         currentLevel = 1;
  32.         lives = 3;
  33.  
  34.         EndGameText.SetActive(false);
  35.  
  36.         if (instance == null)
  37.         {
  38.             instance = this;
  39.         }
  40.  
  41.         //bricks.AddRange(GameObject.FindGameObjectsWithTag("Brick"));
  42.        
  43.     }
  44.  
  45.     void Update()
  46.     {
  47.         //Starting the game when clicking space (if it's not on yet)
  48.         if (Input.GetKeyDown(KeyCode.Space) && !gameRun)
  49.         {
  50.             ball.RunBall();
  51.             gameRun = true;
  52.         }
  53.  
  54.         //Starting ball movement in new level or when losing a life
  55.         if (Input.GetKeyDown(KeyCode.Space) && !ball.inMove)
  56.         {
  57.             ball.RunBall();
  58.         }
  59.  
  60.         //Game over when losing all lives
  61.         if (gameRun && lives <= 0)
  62.         {
  63.             EndGame(false);
  64.         }
  65.  
  66.        
  67.         //Game over when destroying all bricks in the last level
  68.         if (gameRun && bricks.Count == 0 && currentLevel == maxLevel)
  69.         {
  70.             EndGame(true);
  71.         }
  72.  
  73.         ////
  74.         if ((gameRun && bricks.Count == 0 && currentLevel < maxLevel))
  75.         {
  76.             currentLevel++;
  77.             ball.StopBall();
  78.             brickList.StartLevel(currentLevel);
  79.         }
  80.  
  81.  
  82.  
  83.         if (!gameRun)
  84.         {
  85.             if(Input.GetKeyDown(KeyCode.R))
  86.             {
  87.                 SceneManager.LoadScene(0);
  88.             }
  89.         }
  90.     }
  91.  
  92.  
  93.     ////
  94.     public void EndGame(bool win)
  95.     {
  96.         EndGameText.SetActive(true);
  97.         gameRun = false;
  98.         string txt = win ? "Wygrana!" : "Przegrana!";
  99.         winText.text = win ? "Win!" : "Lose!";
  100.         Debug.Log(txt);
  101.         ball.StopBall();
  102.     }
  103.  
  104.     //
  105.     public void UpdateUI()
  106.     {
  107.         bricksCounter.text = "Bricks to destroy: " + bricks.Count;
  108.         livesCounter.text = "Lives: " + lives.ToString();
  109.         levelCounter.text = "Level: " + currentLevel.ToString();
  110.     }
  111.  
  112. }
  113.  
Add Comment
Please, Sign In to add comment