Advertisement
Guest User

Untitled

a guest
May 26th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. using UnityEngine.SceneManagement;
  2. using UnityEngine;
  3.  
  4. public class StateManager : MonoBehaviour
  5. {
  6. enum GameState {Play, Death, Win, Title};
  7. public static int numEnemiesAlive;
  8.  
  9. bool isEnemiesLoaded = false;
  10. public string inputNameSubmit = "Submit";
  11.  
  12. GameState currentState;
  13. // Start is called before the first frame update
  14. void Start()
  15. {
  16. this.EnsureBootstrapSceneIsLoaded();
  17. numEnemiesAlive = 0;
  18. this.EnterTitle();
  19. }
  20.  
  21. void EnsureBootstrapSceneIsLoaded()
  22. {
  23. LoadSceneKeepBootstrapUnloadOthers("Bootstrap", "Bootstrap");
  24. }
  25.  
  26. private void SetGameObjectsActive(GameObject[] objects, bool active)
  27. {
  28. for (int i = 0; i < objects.Length; i++)
  29. {
  30. objects[i].gameObject.SetActive(active);
  31. }
  32. }
  33.  
  34. public void LoadSceneKeepBootstrapUnloadOthers(string sceneToLoadName, string sceneToKeepName)
  35. {
  36. bool isSceneFound = false;
  37.  
  38. for (int sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++)
  39. {
  40. var scene = SceneManager.GetSceneAt(sceneIndex);
  41. if (!scene.IsValid()) { continue; }
  42. if (scene.name.Equals(sceneToKeepName))
  43. {
  44. SetGameObjectsActive(scene.GetRootGameObjects(), true);
  45. if (sceneToLoadName.Equals(sceneToKeepName))
  46. {
  47. return;
  48. }
  49. continue;
  50. }
  51.  
  52. this.SetGameObjectsActive(scene.GetRootGameObjects(), false);
  53.  
  54. if (scene.name.Equals(sceneToLoadName))
  55. {
  56. SetGameObjectsActive(scene.GetRootGameObjects(), true);
  57. isSceneFound = true;
  58. }
  59. }
  60.  
  61. if (!isSceneFound)
  62. {
  63. SceneManager.LoadScene(sceneToLoadName, LoadSceneMode.Additive);
  64. }
  65. }
  66.  
  67.  
  68. void EnterTitle()
  69. {
  70. Debug.Log("Entering title");
  71. currentState = GameState.Title;
  72. this.LoadSceneKeepBootstrapUnloadOthers("Title Screen", "Bootstrap");
  73. }
  74.  
  75. void Title()
  76. {
  77. if (Input.GetButtonDown(inputNameSubmit))
  78. {
  79. this.EnterPlay();
  80. }
  81. }
  82.  
  83. void EnterPlay()
  84. {
  85. currentState = GameState.Play;
  86. print("Starting Game");
  87.  
  88. this.LoadSceneKeepBootstrapUnloadOthers("ShipLevel", "Bootstrap");
  89. isEnemiesLoaded = false;
  90. // play music looping?
  91. }
  92.  
  93. void Play()
  94. {
  95. if (GameplayController.isDead) {
  96.  
  97. EnterDeath();
  98. }
  99.  
  100. if (Input.GetButton("Escape"))
  101. {
  102. this.EnterTitle();
  103. }
  104.  
  105. if (!isEnemiesLoaded && numEnemiesAlive > 0)
  106. {
  107. isEnemiesLoaded = true;
  108. }
  109.  
  110.  
  111. if (isEnemiesLoaded && numEnemiesAlive <= 0)
  112. {
  113. this.EnterWin();
  114. }
  115.  
  116. }
  117.  
  118. void EnterWin()
  119. {
  120. currentState = GameState.Win;
  121. print("you win!!");
  122. this.LoadSceneKeepBootstrapUnloadOthers("Win", "Bootstrap");
  123. }
  124.  
  125. void Win()
  126. {
  127. // play victory theme
  128. if (Input.GetButtonDown(inputNameSubmit))
  129. {
  130. this.EnterTitle();
  131. }
  132. }
  133.  
  134. void EnterDeath()
  135. {
  136. currentState = GameState.Death;
  137.  
  138. // OpenScene unloads current scene and loads requested scene.
  139. this.LoadSceneKeepBootstrapUnloadOthers("Death", "Bootstrap");
  140. }
  141.  
  142. void Death()
  143. {
  144. if (Input.GetButtonDown(inputNameSubmit))
  145. {
  146. this.EnterTitle();
  147. }
  148. }
  149.  
  150.  
  151. // Update is called once per frame
  152. void Update()
  153. {
  154. switch (currentState)
  155. {
  156. case GameState.Title: this.Title(); return;
  157. case GameState.Play: this.Play(); return;
  158. case GameState.Death: this.Death(); return;
  159. case GameState.Win: this.Win(); return;
  160. default: Debug.Assert(false); return;
  161. }
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement