Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.SceneManagement;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. public class HUDController : MonoBehaviour
  8. {
  9. public GameObject pauseMenu;
  10. public GameObject pointer;
  11. public GameObject gameOverMenu;
  12. public GameObject lvlCompletedScreen;
  13. private bool isShowing;
  14. private Button[] buttons;
  15. public int pointerLoc;
  16. public int maxLoc;
  17. public int lastLoc;
  18. private bool gameIsOver = false;
  19. private bool lvlComp = false;
  20. PlayerHealth health;
  21. public int score;
  22. public int displayScore;
  23. public Text scoreText;
  24. private int inhalers;
  25.  
  26. private void Start()
  27. {
  28. health = GameObject.Find("Player").GetComponent<PlayerHealth>();
  29. }
  30.  
  31. void Update()
  32. {
  33. if (Input.GetKeyDown(KeyCode.Escape) && !gameIsOver && !lvlComp)
  34. {
  35. isShowing = !isShowing;
  36. pauseMenu.SetActive(isShowing);
  37. pointer.SetActive(isShowing);
  38. if (buttons == null)
  39. {
  40. buttons = GetComponentsInChildren<Button>();
  41. maxLoc = buttons.Length;
  42. Debug.Log(buttons[0].transform.position.y);
  43. }
  44. buttons[0].image.rectTransform.sizeDelta = new Vector2(250, 48);
  45. if (isShowing)
  46. {
  47. Time.timeScale = 0;
  48. }
  49. else
  50. {
  51. buttons[pointerLoc].image.rectTransform.sizeDelta -= new Vector2(10, 0);
  52. pointerLoc = 0;
  53. Time.timeScale = 1;
  54. }
  55. pointer.transform.localPosition = buttons[0].transform.localPosition - new Vector3(160, 42, 0);
  56.  
  57. }
  58. if (isShowing || gameIsOver || lvlComp)
  59. {
  60. if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
  61. {
  62. lastLoc = pointerLoc;
  63. pointerLoc++;
  64. if (pointerLoc < maxLoc)
  65. {
  66. pointer.transform.localPosition -= new Vector3(0, 58, 0);
  67. Debug.Log("should move");
  68. }
  69. else
  70. {
  71. pointerLoc = 0;
  72. pointer.transform.localPosition += new Vector3(0, (maxLoc - 1) * 58, 0);
  73. }
  74. buttons[lastLoc].image.rectTransform.sizeDelta -= new Vector2(10, 0);
  75. buttons[pointerLoc].image.rectTransform.sizeDelta += new Vector2(10, 0);
  76. Debug.Log(buttons[pointerLoc].name);
  77. }
  78. if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
  79. {
  80. lastLoc = pointerLoc;
  81. pointerLoc--;
  82. if (pointerLoc < 0)
  83. {
  84. pointerLoc = maxLoc - 1;
  85. pointer.transform.localPosition -= new Vector3(0, (maxLoc - 1) * 58, 0);
  86. }
  87. else
  88. {
  89. pointer.transform.localPosition += new Vector3(0, 58, 0);
  90. }
  91. buttons[lastLoc].image.rectTransform.sizeDelta -= new Vector2(10, 0);
  92. buttons[pointerLoc].image.rectTransform.sizeDelta += new Vector2(10, 0);
  93. }
  94. if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.KeypadEnter))
  95. {
  96. switch (buttons[pointerLoc].name)
  97. {
  98. case "ButtonContinue": // Continue
  99. Continue();
  100. break;
  101. case "ButtonRestart": // Restart
  102. Restart();
  103. break;
  104. case "ButtonHowToPlay": // How To Play
  105. HowToPlay();
  106. break;
  107. case "ButtonQuit": // Quit
  108. Exit();
  109. break;
  110. case "ButtonNextLevel": // Next Level
  111. Continue();
  112. break;
  113. }
  114. }
  115. }
  116. }
  117. public void Continue()
  118. {
  119. pointer.transform.localPosition += new Vector3(0, (pointerLoc) * 58, 0);
  120. buttons[pointerLoc].image.rectTransform.sizeDelta -= new Vector2(10, 0);
  121. pointerLoc = 0;
  122. isShowing = !isShowing;
  123. pauseMenu.SetActive(isShowing);
  124. pointer.SetActive(isShowing);
  125. Time.timeScale = 1;
  126. }
  127. public void Restart()
  128. {
  129. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  130. Time.timeScale = 1;
  131. }
  132. public void HowToPlay()
  133. {
  134.  
  135. }
  136. public void Exit()
  137. {
  138. Application.Quit();
  139. }
  140. public void GameOver()
  141. {
  142. Debug.Log("U ded bro");
  143. gameIsOver = true;
  144. pointer.SetActive(true);
  145. Time.timeScale = 0;
  146. gameOverMenu.SetActive(true);
  147. buttons = null;
  148. buttons = GetComponentsInChildren<Button>();
  149. pointer.transform.localPosition = buttons[0].transform.localPosition - new Vector3(160, 42, 0);
  150. maxLoc = buttons.Length;
  151. }
  152. public void LevelCompleted()
  153. {
  154. Debug.Log("Level completed!");
  155. gameIsOver = true;
  156. pointer.SetActive(true);
  157. Time.timeScale = 0;
  158. lvlCompletedScreen.SetActive(true);
  159. buttons = null;
  160. buttons = GetComponentsInChildren<Button>();
  161. pointer.transform.localPosition = buttons[0].transform.localPosition - new Vector3(160, 42, 0);
  162. maxLoc = buttons.Length;
  163. score = Mathf.RoundToInt(health.currentHealth);
  164. health.lvlComp = true;
  165. inhalers = health.inhalers;
  166. StartCoroutine(ScoreUpdater());
  167. }
  168. private IEnumerator ScoreUpdater()
  169. {
  170. while (displayScore < score)
  171. {
  172. displayScore++;
  173. health.ChangeHealth(-1);
  174. scoreText.text = "SCORE: " + displayScore;
  175. yield return new WaitForSecondsRealtime(0.02f);
  176. }
  177. yield return new WaitForSecondsRealtime(0.5f);
  178. if (inhalers > 0)
  179. {
  180. for (int i = 0; i < inhalers; i++)
  181. {
  182. score += 20;
  183. health.addInhalers(-1);
  184. while (displayScore < score)
  185. {
  186. displayScore++;
  187. scoreText.text = "SCORE: " + displayScore;
  188. yield return new WaitForSecondsRealtime(0.02f);
  189. }
  190. yield return new WaitForSecondsRealtime(0.3f);
  191. }
  192. }
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement