Advertisement
Guest User

GameState

a guest
Aug 17th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6.  
  7. public class GameState : MonoBehaviour
  8. {
  9. //script reference
  10.  
  11. [SerializeField] GameObject spawner;
  12. [SerializeField] MovePlayer movePlayer;
  13. [SerializeField] AudioClip winningSFX;
  14.  
  15. // cache references
  16.  
  17. [SerializeField] GameObject spikes;
  18. [SerializeField] int ballsLeft;
  19. [SerializeField] int livesLeft;
  20. [SerializeField] GameObject livesImg;
  21. [SerializeField] GameObject livesImg2;
  22. [SerializeField] GameObject livesImg3;
  23. [SerializeField] GameObject livesImg4;
  24. [SerializeField] GameObject livesImg5;
  25. [SerializeField] GameObject winScreen;
  26. [SerializeField] GameObject loseScreen;
  27. [SerializeField] TextMeshProUGUI ballsLeftText;
  28. [SerializeField] GameObject spawnerParent;
  29. [SerializeField] GameObject spawnerParent2;
  30. [SerializeField] GameObject spawnerParent3;
  31. [SerializeField] GameObject livesSpawnerParent;
  32.  
  33. // Start is called before the first frame update
  34. void Start()
  35. {
  36. livesLeft = 3;
  37. var movePlayer = FindObjectsOfType<MovePlayer>();
  38. loseScreen.SetActive(false);
  39. winScreen.SetActive(false);
  40. }
  41.  
  42. // Update is called once per frame
  43. void Update()
  44. {
  45. ballsLeftText.text = "Balls Left: " + ballsLeft;
  46. }
  47.  
  48. public void CatchBall()
  49. {
  50. ballsLeft -= 1;
  51. if(ballsLeft < 0)
  52. {
  53. ballsLeft = 0;
  54. }
  55.  
  56. if (ballsLeft == 0)
  57. {
  58. livesLeft = 3;
  59. livesImg.gameObject.SetActive(true);
  60. livesImg2.gameObject.SetActive(true);
  61. livesImg3.gameObject.SetActive(true);
  62. livesImg4.gameObject.SetActive(false);
  63. livesImg5.gameObject.SetActive(false);
  64. DestroySpawners();
  65. DisplayWinScreen();
  66. movePlayer.enabled = false;
  67. }
  68. }
  69.  
  70. public void DestroySpawners()
  71. {
  72. Destroy(spawnerParent);
  73. Destroy(spawnerParent2);
  74. Destroy(spawnerParent3);
  75. Destroy(livesSpawnerParent);
  76. }
  77.  
  78. public void LoseLife()
  79. {
  80. livesLeft -= 1;
  81.  
  82. /* if(spikes.gameObject.tag == "Spikes")
  83. {
  84. livesLeft -= 1;
  85. } */
  86.  
  87. if(livesLeft == 4)
  88. {
  89. livesImg5.gameObject.SetActive(false);
  90. }
  91.  
  92. if (livesLeft == 3)
  93. {
  94. livesImg4.gameObject.SetActive(false);
  95. }
  96.  
  97. if (livesLeft == 2)
  98. {
  99. livesImg3.gameObject.SetActive(false);
  100. }
  101.  
  102. else if(livesLeft == 1)
  103. {
  104. livesImg2.gameObject.SetActive(false);
  105. }
  106.  
  107. else if(livesLeft == 0)
  108. {
  109. Destroy(livesImg);
  110. Destroy(spawnerParent);
  111. Destroy(spawnerParent2);
  112. Destroy(spawnerParent3);
  113. movePlayer.enabled = false;
  114. DisplayLoseScreen();
  115. }
  116.  
  117. else if(livesLeft <= 0)
  118. {
  119. livesLeft = 0;
  120. }
  121. }
  122.  
  123. public void AddLife()
  124. {
  125. livesLeft += 1;
  126.  
  127. if(livesLeft > 5)
  128. {
  129. livesLeft = 5;
  130. }
  131.  
  132. if (livesLeft == 1)
  133. {
  134. livesImg.gameObject.SetActive(true);
  135. }
  136.  
  137. if (livesLeft == 2)
  138. {
  139. livesImg2.gameObject.SetActive(true);
  140. }
  141.  
  142. if (livesLeft == 3)
  143. {
  144. livesImg3.gameObject.SetActive(true);
  145. }
  146.  
  147. if (livesLeft == 4)
  148. {
  149. livesImg4.gameObject.SetActive(true);
  150. }
  151.  
  152. if (livesLeft == 5)
  153. {
  154. livesImg5.gameObject.SetActive(true);
  155. }
  156. }
  157.  
  158.  
  159.  
  160. private void DisplayLoseScreen()
  161. {
  162. loseScreen.SetActive(true);
  163. }
  164.  
  165. private void DisplayWinScreen()
  166. {
  167. AudioSource.PlayClipAtPoint(winningSFX, Camera.main.transform.position);
  168. winScreen.SetActive(true);
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement