• We just launched and are currently in beta. Join us as we build and grow the community.

Creating Score Manager, Destroy boundary and Gameplay Controller

sedheali5

DAO Governance Leader
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
72
Likes
145
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
Operating System

Android

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

81_0.png


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

Score Text

82_0.png


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

83_0.png


Then create a child UI Text and set the component as shown below also.
84_0.png

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. // Update is called once per frame
  5. void

    Update (

    )

    {

  6. }

  7. void

    OnTriggerExit2D(

    Collider2D collider)

    {
  8. if

    (

    collider.

    CompareTag

    (

    "Player Bullet"

    )

    ||

    collider.

    CompareTag

    (

    "Object"

    )

    ||

    collider.

    CompareTag

    (

    "Enemy"

    )

    )

    {
  9. Destroy (

    collider.

    gameObject

    )

    ;
  10. }
  11. }

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

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. public

    bool

    gameInProgress;
  3. public

    Transform player;
  4. public

    CameraFollow camera;
  5. public

    Text scoreText, shotText, highscore;
  6. public

    GameObject gameOverPanel, gameWinPanel, pausePanel;

  7. [

    HideInInspector]
  8. public

    bool

    lastShot;

  9. private

    bool

    gameFinished, checkGameStatus;
  10. private

    List<

    GameObject>

    enemies;
  11. private

    List<

    GameObject>

    objects;
  12. private

    float

    timeAfterLastShot, distance, time, timeSinceStartedShot;
  13. private

    int

    prevLevel;

Then write the rest of the codes
  1. void

    Awake(

    )

    {
  2. CreateInstance (

    )

    ;
  3. }

  4. // Use this for initialization
  5. void

    Start (

    )

    {
  6. InitializeVariables (

    )

    ;

  7. if

    (

    GameController.

    instance

    !=

    null

    &&

    MusicController.

    instance

    !=

    null

    )

    {
  8. if

    (

    GameController.

    instance

    .

    isMusicOn

    )

    {
  9. MusicController.

    instance

    .

    GameplaySound

    (

    )

    ;
  10. }

    else

    {
  11. MusicController.

    instance

    .

    StopAllSounds

    (

    )

    ;
  12. }
  13. }

  14. }

  15. // Update is called once per frame
  16. void

    Update (

    )

    {
  17. if

    (

    gameInProgress)

    {
  18. GameIsOnPlay (

    )

    ;
  19. DistanceBetweenCannonAndBullet (

    )

    ;
  20. }


  21. if

    (

    GameController.

    instance

    !=

    null

    )

    {
  22. UpdateGameplayController (

    )

    ;
  23. }

  24. }

  25. void

    CreateInstance(

    )

    {
  26. if

    (

    instance ==

    null

    )

    {
  27. instance =

    this

    ;
  28. }
  29. }

  30. void

    UpdateGameplayController(

    )

    {
  31. scoreText.

    text

    =

    GameController.

    instance

    .

    score

    .

    ToString

    (

    "N0"

    )

    ;
  32. shotText.

    text

    =

    "X"

    +

    PlayerBullet (

    )

    ;
  33. }

  34. void

    InitializeVariables(

    )

    {
  35. gameInProgress =

    true

    ;
  36. enemies =

    new

    List<

    GameObject>

    (

    GameObject.

    FindGameObjectsWithTag

    (

    "Enemy"

    )

    )

    ;
  37. objects =

    new

    List<

    GameObject>

    (

    GameObject.

    FindGameObjectsWithTag

    (

    "Object"

    )

    )

    ;
  38. distance =

    10f;
  39. if

    (

    GameController.

    instance

    !=

    null

    )

    {
  40. GameController.

    instance

    .

    score

    =

    0

    ;
  41. prevLevel =

    GameController.

    instance

    .

    currentLevel

    ;
  42. highscore.

    transform

    .

    GetChild

    (

    0

    )

    .

    transform

    .

    GetComponent

    <

    Text>

    (

    )

    .

    text

    =

    GameController.

    instance

    .

    highscore

    [

    GameController.

    instance

    .

    currentLevel

    -

    1

    ]

    .

    ToString

    (

    "N0"

    )

    ;

  43. if

    (

    GameController.

    instance

    .

    highscore

    [

    GameController.

    instance

    .

    currentLevel

    -

    1

    ]

    >

    0

    )

    {
  44. highscore.

    gameObject

    .

    SetActive

    (

    true

    )

    ;
  45. }

  46. }

  47. }

  48. void

    GameIsOnPlay(

    )

    {
  49. /*if (PlayerBullet () == 0) {
  50. timeAfterLastShot += Time.deltaTime;
  51. camera.isFollowing = false;
  52. if (timeAfterLastShot > 2f) {
  53. if (AllStopMoving () && AllEnemiesDestroyed ()) {
  54. if (!gameFinished) {
  55. gameFinished = true;
  56. Debug.Log ("Hello World");
  57. }
  58. } else if (AllStopMoving () && !AllEnemiesDestroyed ()) {
  59. if (!gameFinished) {
  60. gameFinished = true;
  61. Debug.Log ("Hi World");
  62. }
  63. }
  64. }

  65. }*/

  66. if

    (

    checkGameStatus)

    {
  67. timeAfterLastShot +=

    Time.

    deltaTime

    ;
  68. if

    (

    timeAfterLastShot >

    2f)

    {
  69. if

    (

    AllStopMoving (

    )

    ||

    Time.

    time

    -

    timeSinceStartedShot >

    8f)

    {
  70. if

    (

    AllEnemiesDestroyed (

    )

    )

    {
  71. if

    (

    !

    gameFinished)

    {
  72. gameFinished =

    true

    ;
  73. GameWin (

    )

    ;
  74. timeAfterLastShot =

    0

    ;
  75. checkGameStatus =

    false

    ;
  76. }
  77. }

    else

    {
  78. if

    (

    PlayerBullet (

    )

    ==

    0

    )

    {
  79. if

    (

    !

    gameFinished)

    {
  80. gameFinished =

    true

    ;
  81. timeAfterLastShot =

    0

    ;
  82. checkGameStatus =

    false

    ;
  83. GameLost (

    )

    ;
  84. }
  85. }

    else

    {
  86. checkGameStatus =

    false

    ;
  87. camera.

    isFollowing

    =

    false

    ;
  88. timeAfterLastShot =

    0

    ;
  89. }
  90. }
  91. }
  92. }

  93. }

  94. }

  95. void

    GameWin(

    )

    {
  96. if

    (

    GameController.

    instance

    !=

    null

    &&

    MusicController.

    instance

    !=

    null

    )

    {
  97. if

    (

    GameController.

    instance

    .

    isMusicOn

    )

    {
  98. AudioSource.

    PlayClipAtPoint

    (

    MusicController.

    instance

    .

    winSound

    , Camera.

    main

    .

    transform

    .

    position

    )

    ;
  99. }

  100. if

    (

    GameController.

    instance

    .

    score

    >

    GameController.

    instance

    .

    highscore

    [

    GameController.

    instance

    .

    currentLevel

    -

    1

    ]

    )

    {
  101. GameController.

    instance

    .

    highscore

    [

    GameController.

    instance

    .

    currentLevel

    -

    1

    ]

    =

    GameController.

    instance

    .

    score

    ;
  102. }

  103. highscore.

    text

    =

    GameController.

    instance

    .

    highscore

    [

    GameController.

    instance

    .

    currentLevel

    ]

    .

    ToString

    (

    "N0"

    )

    ;

  104. int

    level =

    GameController.

    instance

    .

    currentLevel

    ;
  105. level++;
  106. if

    (

    !

    (

    level-

    1

    >=

    GameController.

    instance

    .

    levels

    .

    Length

    )

    )

    {
  107. GameController.

    instance

    .

    levels

    [

    level -

    1

    ]

    =

    true

    ;
  108. }

  109. GameController.

    instance

    .

    Save

    (

    )

    ;
  110. GameController.

    instance

    .

    currentLevel

    =

    level;
  111. }
  112. gameWinPanel.

    SetActive

    (

    true

    )

    ;

  113. }

  114. void

    GameLost(

    )

    {
  115. if

    (

    GameController.

    instance

    !=

    null

    &&

    MusicController.

    instance

    !=

    null

    )

    {
  116. if

    (

    GameController.

    instance

    .

    isMusicOn

    )

    {
  117. AudioSource.

    PlayClipAtPoint

    (

    MusicController.

    instance

    .

    loseSound

    , Camera.

    main

    .

    transform

    .

    position

    )

    ;
  118. }
  119. }
  120. gameOverPanel.

    SetActive

    (

    true

    )

    ;
  121. }


  122. public

    int

    PlayerBullet(

    )

    {
  123. int

    playerBullet =

    GameObject.

    FindGameObjectWithTag

    (

    "Player"

    )

    .

    transform

    .

    GetChild

    (

    0

    )

    .

    transform

    .

    GetComponent

    <

    Cannon>

    (

    )

    .

    shot

    ;
  124. return

    playerBullet;
  125. }



  126. private

    bool

    AllEnemiesDestroyed(

    )

    {
  127. return

    enemies.

    All

    (

    x =>

    x ==

    null

    )

    ;
  128. }


  129. private

    bool

    AllStopMoving(

    )

    {
  130. foreach

    (

    var

    item in

    objects.

    Union

    (

    enemies)

    )

    {
  131. if

    (

    item !=

    null

    &&

    item.

    GetComponent

    <

    Rigidbody2D>

    (

    )

    .

    velocity

    .

    sqrMagnitude

    >

    0

    )

    {
  132. return

    false

    ;
  133. }

  134. }

  135. return

    true

    ;
  136. }

  137. void

    DistanceBetweenCannonAndBullet(

    )

    {
  138. GameObject[

    ]

    bullet =

    GameObject.

    FindGameObjectsWithTag

    (

    "Player Bullet"

    )

    ;
  139. foreach

    (

    GameObject distanceToBullet in

    bullet)

    {
  140. if

    (

    !

    distanceToBullet.

    transform

    .

    GetComponent

    <

    CannonBullet>

    (

    )

    .

    isIdle

    )

    {
  141. if

    (

    distanceToBullet.

    transform

    .

    position

    .

    x

    -

    player.

    position

    .

    x

    >

    distance)

    {
  142. camera.

    isFollowing

    =

    true

    ;
  143. checkGameStatus =

    true

    ;
  144. timeSinceStartedShot =

    Time.

    time

    ;
  145. TimeSinceShot (

    )

    ;
  146. camera.

    target

    =

    distanceToBullet.

    transform

    ;
  147. }

    else

    {
  148. if

    (

    PlayerBullet(

    )

    ==

    0

    )

    {
  149. camera.

    isFollowing

    =

    true

    ;
  150. checkGameStatus =

    true

    ;
  151. timeSinceStartedShot =

    Time.

    time

    ;
  152. TimeSinceShot (

    )

    ;
  153. camera.

    target

    =

    distanceToBullet.

    transform

    ;
  154. }
  155. }
  156. }
  157. }
  158. /*if (GameObject.FindGameObjectWithTag ("Player Bullet") != null) {
  159. if (!GameObject.FindGameObjectWithTag ("Player Bullet").transform.GetComponent<CannonBullet> ().isIdle) {
  160. Transform distanceToBullet = GameObject.FindGameObjectWithTag ("Player Bullet").transform;
  161. if (distanceToBullet.position.x - player.position.x > distance) {
  162. camera.isFollowing = true;
  163. checkGameStatus = true;
  164. TimeSinceShot ();
  165. camera.target = distanceToBullet;
  166. }
  167. }

  168. }*/
  169. }

  170. void

    TimeSinceShot(

    )

    {
  171. time +=

    Time.

    deltaTime

    ;
  172. if

    (

    time >

    3f)

    {
  173. time =

    0f;
  174. GameObject.

    FindGameObjectWithTag

    (

    "Player Bullet"

    )

    .

    transform

    .

    GetComponent

    <

    CannonBullet>

    (

    )

    .

    isIdle

    =

    true

    ;
  175. }

  176. }

  177. public

    void

    RestartGame(

    )

    {
  178. SceneManager.

    LoadScene

    (

    SceneManager.

    GetActiveScene

    (

    )

    .

    buildIndex

    )

    ;
  179. if

    (

    GameController.

    instance

    !=

    null

    )

    {
  180. GameController.

    instance

    .

    currentLevel

    =

    prevLevel;
  181. }
  182. }

  183. public

    void

    BackToLevelMenu(

    )

    {
  184. if

    (

    GameController.

    instance

    !=

    null

    &&

    MusicController.

    instance

    !=

    null

    )

    {
  185. if

    (

    GameController.

    instance

    .

    isMusicOn

    )

    {
  186. MusicController.

    instance

    .

    PlayBgMusic

    (

    )

    ;
  187. }

    else

    {
  188. MusicController.

    instance

    .

    StopAllSounds

    (

    )

    ;
  189. }
  190. }
  191. SceneManager.

    LoadScene

    (

    "Level Menu"

    )

    ;
  192. Time.

    timeScale

    =

    1

    ;
  193. }

  194. public

    void

    ContinueGame(

    )

    {
  195. if

    (

    GameController.

    instance

    !=

    null

    )

    {
  196. SceneManager.

    LoadScene

    (

    "Level "

    +

    GameController.

    instance

    .

    currentLevel

    )

    ;
  197. }
  198. }

  199. public

    void

    PauseGame(

    )

    {
  200. if

    (

    gameInProgress)

    {
  201. gameInProgress =

    false

    ;
  202. Time.

    timeScale

    =

    0

    ;
  203. pausePanel.

    SetActive

    (

    true

    )

    ;
  204. }
  205. }

  206. public

    void

    ResumeGame(

    )

    {
  207. Time.

    timeScale

    =

    1

    ;
  208. gameInProgress =

    true

    ;
  209. pausePanel.

    SetActive

    (

    false

    )

    ;
  210. }

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


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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

437,153

314,794

314,803

Top