Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6.  
  7. public class GameManager : MonoBehaviour
  8. {
  9. public int score;
  10. public int lives;
  11. public int highscore;
  12. private bool gameInPlay = false;
  13. private int level;
  14.  
  15. ///////////////////////////////////////////////////////////////////////////
  16. ///
  17. /// TODO: Declare a list of gameobjects for the bricks, as per the design
  18. /// - Make sure it is accessible from other classes
  19. ///
  20. ///////////////////////////////////////////////////////////////////////////
  21.  
  22.  
  23. public List<Transform> bricks;
  24.  
  25. private Transform ball;
  26. private Transform bat;
  27.  
  28. public Transform brickPrefab;
  29. public Transform batPrefab;
  30. public Transform ballPrefab;
  31. public Text TextBoxScore;
  32. public Text TextBoxHighscore;
  33. public Text TextBoxTitle;
  34. public Text TextBoxNewHigh;
  35. public Text TextBoxPrompt;
  36. public Text TextBoxLives;
  37.  
  38. void Start()
  39. {
  40. TextBoxNewHigh.enabled = false;
  41. }
  42.  
  43. // Update is called once per frame
  44. void Update () {
  45.  
  46.  
  47.  
  48. if (gameInPlay)
  49. {
  50.  
  51. // check for the last brick being destroyed, and start a new level
  52. if (bricks.Count == 0)
  53. {
  54. level++;
  55. StartNewLevel(level);
  56. }
  57.  
  58. // game over when you're out of lives
  59. if (lives <= 0)
  60. {
  61. EndGame();
  62. }
  63. }
  64. else
  65. {
  66. // press Q to quit (will only work in standalone executable)
  67. if (Input.GetKeyDown(KeyCode.Q))
  68. {
  69. Application.Quit();
  70. }
  71.  
  72. if(Input.GetKeyDown(KeyCode.P)){
  73. gameInPlay = true;
  74. StartNewGame();
  75. }
  76. ///////////////////////////////////////////////////////////////////////////
  77. ///
  78. /// TODO: Check if the player has pressed the 'P' key when at game over
  79. /// - Respond by calling the function to start a new game,
  80. /// - and changing the appropriate variable so that the game is in play
  81. ///
  82. ///////////////////////////////////////////////////////////////////////////
  83.  
  84. }
  85.  
  86. // keep on-screen values up to date
  87. TextBoxScore.text = "SCORE: " + score;
  88. TextBoxLives.text = "LIVES: " + lives;
  89. TextBoxHighscore.text = "HIGH: " + highscore;
  90. }
  91.  
  92. private void StartNewGame()
  93. {
  94. score = 0;
  95. lives = 3;
  96. TextBoxLives.enabled = true;
  97. TextBoxTitle.enabled = false;
  98. TextBoxPrompt.enabled = false;
  99. TextBoxNewHigh.enabled = false;
  100. bat = Instantiate(batPrefab, new Vector3(0, -4, 0), Quaternion.identity);
  101. ball = Instantiate(ballPrefab, new Vector3(0, -2, 0), Quaternion.identity);
  102. level = 0;
  103. StartNewLevel(level);
  104. ///////////////////////////////////////////////////////////////////////////
  105. ///
  106. /// TODO: Call the function to start a new level
  107. /// - Include appropriate arguments for parameters
  108. ///
  109. ///////////////////////////////////////////////////////////////////////////
  110.  
  111.  
  112.  
  113.  
  114. }
  115.  
  116. private void StartNewLevel(int level){
  117. ball.position = new Vector3(0,-2,0);
  118.  
  119. level = level + 1;
  120. if(level > 1){
  121. score = score + 10;
  122. }
  123.  
  124. int gridHeight = 10;
  125. int gridWidth = 4;
  126. for(int x = 0; x < gridHeight; x++){
  127. for(float y = 0; y < gridWidth; y++){
  128. bricks.Add(Instantiate(brickPrefab, new Vector3(-4.5f + x * 1, 0 + y * 1, 0), Quaternion.identity));
  129.  
  130. }
  131. }
  132. }
  133.  
  134. ///////////////////////////////////////////////////////////////////////////
  135. ///
  136. /// TODO: Create the function to start a new level
  137. /// - Include appropriate parameters
  138. /// - Set the ball's co ordinates to 0, -2, 0
  139. /// - Give the player a bonus 10 points if it's level 2 or more
  140. /// - Use one or more FOR loops to iterate through 4 rows of 10 bricks
  141. /// - Each brick should be instantiated from the brick prefab
  142. /// - it's co-ordinates should be calculated so the bricks are in a grid
  143. /// - Add the new object created, into the list of bricks.
  144. ///
  145. ///////////////////////////////////////////////////////////////////////////
  146.  
  147. private void EndGame()
  148. {
  149. // Find out if we have a new highscore
  150. if (score>highscore)
  151. {
  152. highscore = score;
  153. TextBoxNewHigh.enabled = true;
  154. }
  155.  
  156. // Remove all bricks from the game and from the list
  157. for(int i=bricks.Count-1; i>=0; i--)
  158. {
  159. Destroy(bricks[i]);
  160. bricks.RemoveAt(i);
  161. }
  162. Destroy(ball.gameObject);
  163. Destroy(bat.gameObject);
  164. TextBoxLives.enabled = false;
  165. TextBoxTitle.enabled = true;
  166. TextBoxPrompt.enabled = true;
  167. gameInPlay = false;
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement