/Arkanoid/Assets/Scripts/GameStateManager.cs

https://bitbucket.org/adam19/breakout_unity · C# · 336 lines · 270 code · 58 blank · 8 comment · 30 complexity · 132d7c5af018fe77626e4051b9401bd4 MD5 · raw file

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameStateManager : MonoBehaviour {
  5. public enum GameStateEnum
  6. {
  7. MAINMENU,
  8. HOWTOPLAYMENU,
  9. STARTING,
  10. PLAYING,
  11. GAMEOVER_LOST,
  12. GAMEOVER_WON
  13. }
  14. public int TimeUntilGameStart = 3;
  15. public int LivesToStartWith = 3;
  16. public float ScoreMultiplierIncrement = 0.1f;
  17. public float ScoreMultiplierCap = 0.0f;
  18. public BlockManager BlockLayoutToSpawn;
  19. public Camera MainCameraRef;
  20. public Transform CameraTransformMainMenu;
  21. public Transform CameraTransformHowToPlayMenu;
  22. public Transform CameraTransformGame;
  23. public float CameraSlerpTime = 1.0f;
  24. public TextMesh UIText;
  25. public TextMesh NumBallsText;
  26. public TextMesh ScoreText;
  27. public TextMesh ScoreMultiplierText;
  28. public TextButton MenuButton;
  29. public static GameStateEnum GameState
  30. {
  31. get { return _GameState; }
  32. }
  33. private static GameStateEnum _GameState;
  34. private static int _NumBalls;
  35. private static int _Score;
  36. private float _ScoreMultiplier = 1.0f;
  37. private PlayerController _PlayerControllerComp;
  38. private BlockManager _BlockManagerRef;
  39. private float _CountDownTime = -1.0f;
  40. // Camera-specific
  41. private Transform _TargetCameraTransform;
  42. private Transform _StartCameraTransform;
  43. private bool _IsCameraSlerping = false;
  44. private float _CameraSlerpStart = 0.0f;
  45. private bool IsDebugKeyDown = false;
  46. // Use this for initialization
  47. void Start () {
  48. _PlayerControllerComp = GetComponent<PlayerController>();
  49. SetCameraTargetTransform(CameraTransformMainMenu, true);
  50. OpenMainMenu();
  51. SetMenuButtonActive(false);
  52. if (MenuButton)
  53. {
  54. MenuButton.OnClickDelegate = OpenMainMenu;
  55. }
  56. }
  57. // Update is called once per frame
  58. void Update () {
  59. // Update the camera transform
  60. if (_IsCameraSlerping && MainCameraRef != null)
  61. {
  62. float t = Time.timeSinceLevelLoad - _CameraSlerpStart;
  63. if (CameraSlerpTime > 0.0f)
  64. {
  65. t /= CameraSlerpTime;
  66. }
  67. else
  68. {
  69. t = Mathf.Min(t, 1.0f);
  70. }
  71. // Slerp the rotation
  72. Quaternion CameraRotation = MainCameraRef.transform.rotation;
  73. CameraRotation = Quaternion.Slerp(_StartCameraTransform.rotation, _TargetCameraTransform.rotation, t);
  74. MainCameraRef.transform.rotation = CameraRotation;
  75. // Lerp the position
  76. Vector3 CameraPosition = MainCameraRef.transform.position;
  77. CameraPosition = Vector3.Lerp(_StartCameraTransform.position, _TargetCameraTransform.position, t);
  78. MainCameraRef.transform.position = CameraPosition;
  79. }
  80. // Display the countdown timer
  81. if (_CountDownTime > 0.0f)
  82. {
  83. int WholeSeconds = Mathf.CeilToInt(_CountDownTime);
  84. UIText.text = WholeSeconds.ToString();
  85. _CountDownTime -= Time.deltaTime;
  86. }
  87. else if (GameState == GameStateEnum.STARTING)
  88. {
  89. StartGame();
  90. }
  91. }
  92. private void SetCameraTargetTransform(Transform NewTargetTransform, bool InstantSnapToRotation = false)
  93. {
  94. if (NewTargetTransform != null)
  95. {
  96. _StartCameraTransform = MainCameraRef.transform;
  97. _TargetCameraTransform = NewTargetTransform;
  98. _IsCameraSlerping = !InstantSnapToRotation;
  99. if (MainCameraRef)
  100. {
  101. if (InstantSnapToRotation)
  102. {
  103. // Set all of the rotations to the new rotation
  104. _StartCameraTransform = NewTargetTransform;
  105. MainCameraRef.transform.SetPositionAndRotation(_StartCameraTransform.position, _StartCameraTransform.rotation);
  106. }
  107. else
  108. {
  109. _CameraSlerpStart = Time.timeSinceLevelLoad;
  110. }
  111. }
  112. }
  113. }
  114. public void OpenMainMenu()
  115. {
  116. _GameState = GameStateEnum.MAINMENU;
  117. Cursor.visible = true;
  118. SetTextActive(UIText, false);
  119. SetTextActive(NumBallsText, false);
  120. SetTextActive(ScoreText, false);
  121. SetTextActive(ScoreMultiplierText, false);
  122. SetMenuButtonActive(false);
  123. SetCameraTargetTransform(CameraTransformMainMenu);
  124. }
  125. public void OpenHowToPlayMenu()
  126. {
  127. _GameState = GameStateEnum.HOWTOPLAYMENU;
  128. Cursor.visible = true;
  129. SetTextActive(UIText, false);
  130. SetTextActive(NumBallsText, false);
  131. SetTextActive(ScoreText, false);
  132. SetTextActive(ScoreMultiplierText, false);
  133. SetCameraTargetTransform(CameraTransformHowToPlayMenu);
  134. }
  135. public void InitGame()
  136. {
  137. Debug.Log("InitGame()");
  138. _GameState = GameStateEnum.STARTING;
  139. Cursor.visible = false;
  140. if (_BlockManagerRef != null)
  141. {
  142. Destroy(_BlockManagerRef);
  143. }
  144. _BlockManagerRef = Instantiate(BlockLayoutToSpawn);
  145. if (_BlockManagerRef)
  146. {
  147. _BlockManagerRef.GameStateManagerRef = this;
  148. }
  149. _NumBalls = LivesToStartWith;
  150. _Score = 0;
  151. SetMenuButtonActive(false);
  152. SetTextActive(UIText, true);
  153. SetTextActive(NumBallsText, true);
  154. SetTextActive(ScoreText, true);
  155. SetTextActive(ScoreMultiplierText, true);
  156. UpdateBallsText();
  157. UpdateScoreText();
  158. UpdateMultiplierText();
  159. SetCameraTargetTransform(CameraTransformGame);
  160. StartGameAfterDelay(TimeUntilGameStart);
  161. }
  162. public void StartGame()
  163. {
  164. Debug.Log("StartGame()");
  165. _GameState = GameStateEnum.PLAYING;
  166. Cursor.visible = false;
  167. if (UIText)
  168. {
  169. UIText.text = "Go!";
  170. StartCoroutine(HideTextAfterDelay());
  171. }
  172. }
  173. public void QuitGame()
  174. {
  175. Application.Quit();
  176. }
  177. public void BlockDestroyed(Block DeadBlock)
  178. {
  179. if (DeadBlock != null)
  180. {
  181. _Score += (int)(DeadBlock.DestructionPoints * _ScoreMultiplier);
  182. UpdateScoreText();
  183. IncrementScoreMultiplier();
  184. }
  185. }
  186. public void AllBlocksDestroyed()
  187. {
  188. _GameState = GameStateEnum.GAMEOVER_WON;
  189. UIText.text = "You Won!";
  190. SetTextActive(UIText, true);
  191. SetMenuButtonActive(true);
  192. }
  193. public void ResetScoreMultiplier()
  194. {
  195. _ScoreMultiplier = 1.0f;
  196. UpdateMultiplierText();
  197. }
  198. public void IncrementScoreMultiplier()
  199. {
  200. _ScoreMultiplier += ScoreMultiplierIncrement;
  201. _ScoreMultiplier = Mathf.Max(_ScoreMultiplier, ScoreMultiplierCap);
  202. UpdateMultiplierText();
  203. }
  204. public void OnPlayerDeath()
  205. {
  206. if (_GameState != GameStateEnum.GAMEOVER_WON)
  207. {
  208. if (_NumBalls == 0)
  209. {
  210. _GameState = GameStateEnum.GAMEOVER_LOST;
  211. UIText.text = "Game Over!";
  212. SetMenuButtonActive(true);
  213. }
  214. else
  215. {
  216. _NumBalls--;
  217. UIText.text = "Ouch!";
  218. UpdateBallsText();
  219. StartGameAfterDelay(TimeUntilGameStart);
  220. }
  221. ResetScoreMultiplier();
  222. SetTextActive(UIText, true);
  223. }
  224. }
  225. private void StartGameAfterDelay(int Seconds)
  226. {
  227. _GameState = GameStateEnum.STARTING;
  228. _CountDownTime = Seconds;
  229. if (_PlayerControllerComp)
  230. {
  231. _PlayerControllerComp.SpawnBall();
  232. }
  233. }
  234. private IEnumerator HideTextAfterDelay()
  235. {
  236. yield return new WaitForSeconds(2);
  237. SetTextActive(UIText, false);
  238. }
  239. private void SetMenuButtonActive(bool IsActive)
  240. {
  241. if (IsActive)
  242. {
  243. Cursor.visible = true;
  244. }
  245. if (MenuButton)
  246. {
  247. MenuButton.gameObject.SetActive(IsActive);
  248. }
  249. }
  250. private void SetTextActive(TextMesh TextObj, bool IsActive)
  251. {
  252. if (TextObj)
  253. {
  254. TextObj.gameObject.SetActive(IsActive);
  255. }
  256. }
  257. private void UpdateBallsText()
  258. {
  259. if (NumBallsText)
  260. {
  261. NumBallsText.text = "Balls: " + _NumBalls;
  262. }
  263. }
  264. private void UpdateScoreText()
  265. {
  266. if (ScoreText)
  267. {
  268. ScoreText.text = "Score: " + _Score;
  269. }
  270. }
  271. private void UpdateMultiplierText()
  272. {
  273. if (ScoreMultiplierText)
  274. {
  275. ScoreMultiplierText.text = string.Format("x{0:N1}", _ScoreMultiplier);
  276. }
  277. }
  278. }