Skip to main content

Creating Score Manager, Destroy boundary and Gameplay Controller

Operating System


Creating Score Manager

We will now create the scoring of the game. This will track down the score of the player every time the enemy is defeated. To do that go to GameObject and select UI, then choose Text. After creating the text set the components as shown below.

Score

tut81

Next create another UI Text as a child of the component above. Then set the components as shown below.

Score Text

tut82

After creating the Score text we will now create the Highscore UI text. Same as what you did to the UI Score, create a text element and set the component as show below.

Highscore

tut83

Then create a child UI Text and set the component as shown below also.
tut84

Creating Destroy boundary

We will create now the destroy boundary. This will take care of all the outside boundary objects within the screen. To do that simple create two GameObjects and add a Box Collider 2D to each GameObject. After that go to the Scripts directory then create a new directory namely Boundary. Then create a C# script called Boundary. Next write these block of code inside the class:

  1. // Use this for initialization
  2. void Start () {
  3.  
  4. }
  5.  
  6. // Update is called once per frame
  7. void Update () {
  8.  
  9. }
  10.  
  11. void OnTriggerExit2D(Collider2D collider){
  12. if (collider.CompareTag ("Player Bullet") || collider.CompareTag ("Object") || collider.CompareTag ("Enemy")) {
  13. Destroy (collider.gameObject);
  14. }
  15. }

After creating the script attach the scripts to the GameObject inspector and set the components as shown below.
tut85

Creating Gameplay Controller

Now we will create the Gameplay Controller. This will be the game mechanics of the game. It will handle the flow of the game to make it playable. Create a GameObject then name it as Gameplay Controller. Then create a script and save it to Game Controllers folder as GameplayController.

Write these important variables to make the game work properly:

  1. public static GameplayController instance;
  2.  
  3. public bool gameInProgress;
  4. public Transform player;
  5. public CameraFollow camera;
  6. public Text scoreText, shotText, highscore;
  7. public GameObject gameOverPanel, gameWinPanel, pausePanel;
  8.  
  9. [HideInInspector]
  10. public bool lastShot;
  11.  
  12. private bool gameFinished, checkGameStatus;
  13. private List<GameObject> enemies;
  14. private List<GameObject> objects;
  15. private float timeAfterLastShot, distance, time, timeSinceStartedShot;
  16. private int prevLevel;

Then write the rest of the codes

  1. void Awake(){
  2. CreateInstance ();
  3. }
  4.  
  5. // Use this for initialization
  6. void Start () {
  7. InitializeVariables ();
  8.  
  9. if (GameController.instance != null && MusicController.instance != null) {
  10. if (GameController.instance.isMusicOn) {
  11. MusicController.instance.GameplaySound ();
  12. } else {
  13. MusicController.instance.StopAllSounds ();
  14. }
  15. }
  16.  
  17. }
  18.  
  19. // Update is called once per frame
  20. void Update () {
  21. if (gameInProgress) {
  22. GameIsOnPlay ();
  23. DistanceBetweenCannonAndBullet ();
  24. }
  25.  
  26.  
  27. if(GameController.instance != null){
  28. UpdateGameplayController ();
  29. }
  30.  
  31. }
  32.  
  33. void CreateInstance(){
  34. if(instance == null){
  35. instance = this;
  36. }
  37. }
  38.  
  39. void UpdateGameplayController(){
  40. scoreText.text = GameController.instance.score.ToString("N0");
  41. shotText.text = "X" + PlayerBullet ();
  42. }
  43.  
  44. void InitializeVariables(){
  45. gameInProgress = true;
  46. enemies = new List<GameObject> (GameObject.FindGameObjectsWithTag ("Enemy"));
  47. objects = new List<GameObject> (GameObject.FindGameObjectsWithTag ("Object"));
  48. distance = 10f;
  49. if(GameController.instance != null){
  50. GameController.instance.score = 0;
  51. prevLevel = GameController.instance.currentLevel;
  52. highscore.transform.GetChild (0).transform.GetComponent<Text> ().text = GameController.instance.highscore [GameController.instance.currentLevel - 1].ToString ("N0");
  53.  
  54. if(GameController.instance.highscore[GameController.instance.currentLevel - 1] > 0){
  55. highscore.gameObject.SetActive (true);
  56. }
  57.  
  58. }
  59.  
  60. }
  61.  
  62. void GameIsOnPlay(){
  63. /*if (PlayerBullet () == 0) {
  64. timeAfterLastShot += Time.deltaTime;
  65. camera.isFollowing = false;
  66. if (timeAfterLastShot > 2f) {
  67. if (AllStopMoving () && AllEnemiesDestroyed ()) {
  68. if (!gameFinished) {
  69. gameFinished = true;
  70. Debug.Log ("Hello World");
  71. }
  72. } else if (AllStopMoving () && !AllEnemiesDestroyed ()) {
  73. if (!gameFinished) {
  74. gameFinished = true;
  75. Debug.Log ("Hi World");
  76. }
  77. }
  78. }
  79.  
  80. }*/
  81.  
  82. if(checkGameStatus){
  83. timeAfterLastShot += Time.deltaTime;
  84. if (timeAfterLastShot > 2f) {
  85. if (AllStopMoving () || Time.time - timeSinceStartedShot > 8f) {
  86. if (AllEnemiesDestroyed ()) {
  87. if (!gameFinished) {
  88. gameFinished = true;
  89. GameWin ();
  90. timeAfterLastShot = 0;
  91. checkGameStatus = false;
  92. }
  93. } else {
  94. if (PlayerBullet () == 0) {
  95. if (!gameFinished) {
  96. gameFinished = true;
  97. timeAfterLastShot = 0;
  98. checkGameStatus = false;
  99. GameLost ();
  100. }
  101. } else {
  102. checkGameStatus = false;
  103. camera.isFollowing = false;
  104. timeAfterLastShot = 0;
  105. }
  106. }
  107. }
  108. }
  109.  
  110. }
  111.  
  112. }
  113.  
  114. void GameWin(){
  115. if(GameController.instance != null && MusicController.instance != null){
  116. if(GameController.instance.isMusicOn){
  117. AudioSource.PlayClipAtPoint (MusicController.instance.winSound, Camera.main.transform.position);
  118. }
  119.  
  120. if(GameController.instance.score > GameController.instance.highscore[ GameController.instance.currentLevel - 1]){
  121. GameController.instance.highscore [ GameController.instance.currentLevel - 1] = GameController.instance.score;
  122. }
  123.  
  124. highscore.text = GameController.instance.highscore [GameController.instance.currentLevel].ToString ("N0");
  125.  
  126. int level = GameController.instance.currentLevel;
  127. level++;
  128. if(!(level-1 >= GameController.instance.levels.Length)){
  129. GameController.instance.levels [level - 1] = true;
  130. }
  131.  
  132. GameController.instance.Save ();
  133. GameController.instance.currentLevel = level;
  134. }
  135. gameWinPanel.SetActive (true);
  136.  
  137. }
  138.  
  139. void GameLost(){
  140. if(GameController.instance != null && MusicController.instance != null){
  141. if(GameController.instance.isMusicOn){
  142. AudioSource.PlayClipAtPoint (MusicController.instance.loseSound, Camera.main.transform.position);
  143. }
  144. }
  145. gameOverPanel.SetActive (true);
  146. }
  147.  
  148.  
  149. public int PlayerBullet(){
  150. int playerBullet = GameObject.FindGameObjectWithTag ("Player").transform.GetChild (0).transform.GetComponent<Cannon> ().shot;
  151. return playerBullet;
  152. }
  153.  
  154.  
  155.  
  156. private bool AllEnemiesDestroyed(){
  157. return enemies.All(x => x == null);
  158. }
  159.  
  160.  
  161. private bool AllStopMoving(){
  162. foreach (var item in objects.Union(enemies)) {
  163. if(item != null && item.GetComponent<Rigidbody2D>().velocity.sqrMagnitude > 0){
  164. return false;
  165. }
  166.  
  167. }
  168.  
  169. return true;
  170. }
  171.  
  172. void DistanceBetweenCannonAndBullet(){
  173. GameObject[] bullet = GameObject.FindGameObjectsWithTag ("Player Bullet");
  174. foreach (GameObject distanceToBullet in bullet) {
  175. if (!distanceToBullet.transform.GetComponent<CannonBullet> ().isIdle) {
  176. if (distanceToBullet.transform.position.x - player.position.x > distance) {
  177. camera.isFollowing = true;
  178. checkGameStatus = true;
  179. timeSinceStartedShot = Time.time;
  180. TimeSinceShot ();
  181. camera.target = distanceToBullet.transform;
  182. } else {
  183. if(PlayerBullet() == 0){
  184. camera.isFollowing = true;
  185. checkGameStatus = true;
  186. timeSinceStartedShot = Time.time;
  187. TimeSinceShot ();
  188. camera.target = distanceToBullet.transform;
  189. }
  190. }
  191. }
  192. }
  193. /*if (GameObject.FindGameObjectWithTag ("Player Bullet") != null) {
  194. if (!GameObject.FindGameObjectWithTag ("Player Bullet").transform.GetComponent<CannonBullet> ().isIdle) {
  195. Transform distanceToBullet = GameObject.FindGameObjectWithTag ("Player Bullet").transform;
  196. if (distanceToBullet.position.x - player.position.x > distance) {
  197. camera.isFollowing = true;
  198. checkGameStatus = true;
  199. TimeSinceShot ();
  200. camera.target = distanceToBullet;
  201. }
  202. }
  203.  
  204. }*/
  205. }
  206.  
  207. void TimeSinceShot(){
  208. time += Time.deltaTime;
  209. if (time > 3f) {
  210. time = 0f;
  211. GameObject.FindGameObjectWithTag ("Player Bullet").transform.GetComponent<CannonBullet> ().isIdle = true;
  212. }
  213.  
  214. }
  215.  
  216. public void RestartGame(){
  217. SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex);
  218. if(GameController.instance != null){
  219. GameController.instance.currentLevel = prevLevel;
  220. }
  221. }
  222.  
  223. public void BackToLevelMenu(){
  224. if(GameController.instance != null && MusicController.instance != null){
  225. if (GameController.instance.isMusicOn) {
  226. MusicController.instance.PlayBgMusic ();
  227. } else {
  228. MusicController.instance.StopAllSounds ();
  229. }
  230. }
  231. SceneManager.LoadScene ("Level Menu");
  232. Time.timeScale = 1;
  233. }
  234.  
  235. public void ContinueGame(){
  236. if (GameController.instance != null) {
  237. SceneManager.LoadScene ("Level " + GameController.instance.currentLevel);
  238. }
  239. }
  240.  
  241. public void PauseGame(){
  242. if (gameInProgress) {
  243. gameInProgress = false;
  244. Time.timeScale = 0;
  245. pausePanel.SetActive (true);
  246. }
  247. }
  248.  
  249. public void ResumeGame(){
  250. Time.timeScale = 1;
  251. gameInProgress = true;
  252. pausePanel.SetActive (false);
  253. }

After creating the script attach it to the Gameplay Controller component. And then attach all the needed components in the Inspector of GameplayController script.
tut86

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.