Loading...

Creating Score Manager, Destroy boundary and Gameplay Controller

View: 184    Dowload: 0   Comment: 0   Post by: daii   Category: Mobile applications   Fields: Game - Entertainment

Download  creating-score-manager.zip (13.73 MB)

You need to Sign In to download the file creating-score-manager.zip
If you do not have an account then Sign up for free here

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

Creating Score Manager, Destroy boundary and Gameplay Controller

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 Next create another UI Text as a child of the component above. Then set the components as shown below. Score Text After...

Posted on 04-04-2018 

Comment:

To comment you must be logged in members.

Files with category

  • Android - Simple Image Capture

    Android - Simple Image Capture

    View: 589    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    Learn on how to create a Simple Image Capture using Android. This simple application can be used to any system that needed the information about the location of the user. The android is an open source operating system it's free and user friendly to...

  • Android - Simple Video Player

    Android - Simple Video Player

    View: 441    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    Learn on how to create a Simple Video Player using Android. This simple application can be use in viewing some videos. The android is an open source operating system it's free and user friendly to mobile developers. Android is available to any...

  • An Experience about Using Webview in Android Studio 3.0

    An Experience about Using Webview in Android Studio 3.0

    View: 221    Download: 0   Comment: 0

    Category: Android     Fields: Other

    I am blogging on ngocminhtran.com (using WordPress platform). Like most websites, I want to write an Android application to help viewers have a lot of great experiences with my blog.

  • Android - Simple Sending SMS

    Android - Simple Sending SMS

    View: 502    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    Learn on how to create a Simple Sending SMS using Android. This tutorial is only an example for sending an SMS, it does not contain the receiving message yet. Android is a mobile operating system developed by Google. It used in several gadget like...

  • Bomb Explode! (An Android App Strategy Game)

    Bomb Explode! (An Android App Strategy Game)

    View: 453    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    This android app named Bomb Explode! is a capstone project that I made for a particular school. BOMB EXPLODE! is a Human Versus Computer strategy game. The objective is to eliminate the opponent by blowing up their BOMBS. Each player takes a turn by...

  • GIS App for Flood-Prone and Disease Affected Areas

    GIS App for Flood-Prone and Disease Affected Areas

    View: 451    Download: 1   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    GIS App for Flood-Prone and Disease Affected Areas is one of the capstone project that I have created in a particular school. Features of the application: - Geographic Map - GIS - Flood Prone Map - Urban Map - Search a particular area - Has SQLite...

  • NSO Demographic Application

    NSO Demographic Application

    View: 492    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    This app is entitled NSO Demographic Application that was one of the capstone project/thesis project that I made for a particular school. This app features to provide a demographic survey of the National Statistics Office that will send to the...

  • HandEBook: An IT Portable Book Application

    HandEBook: An IT Portable Book Application

    View: 464    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    This is an android mobile application entitled HandEBook: An IT Portable Book Application was one one of the capstone project that I have made in our clients at Cotabato. We used the Basic4Android software for the frontend and SQLite for the database...

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd