juaniisuar

Untitled

Jun 25th, 2015
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace Completed
  5. {
  6. using System.Collections.Generic; //Allows us to use Lists.
  7. using UnityEngine.UI; //Allows us to use UI.
  8.  
  9. public class GameManager : MonoBehaviour
  10. {
  11. public float levelStartDelay = 2f; //Time to wait before starting level, in seconds.
  12. public float turnDelay = 0.1f; //Delay between each Player turn.
  13. public int playerFoodPoints = 100; //Starting value for Player food points.
  14. public static GameManager instance = null; //Static instance of GameManager which allows it to be accessed by any other script.
  15. [HideInInspector] public bool playersTurn = true; //Boolean to check if it's players turn, hidden in inspector but public.
  16.  
  17.  
  18. private Text levelText; //Text to display current level number.
  19. private GameObject levelImage; //Image to block out level as levels are being set up, background for levelText.
  20. private BoardManager boardScript; //Store a reference to our BoardManager which will set up the level.
  21. private int level = 1; //Current level number, expressed in game as "Day 1".
  22. private List<Enemy> enemies; //List of all Enemy units, used to issue them move commands.
  23. private bool enemiesMoving; //Boolean to check if enemies are moving.
  24. private bool doingSetup = true; //Boolean to check if we're setting up board, prevent Player from moving during setup.
  25.  
  26.  
  27.  
  28. //Awake is always called before any Start functions
  29. void Awake()
  30. {
  31. //Check if instance already exists
  32. if (instance == null)
  33.  
  34. //if not, set instance to this
  35. instance = this;
  36.  
  37. //If instance already exists and it's not this:
  38. else if (instance != this)
  39.  
  40. //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
  41. Destroy(gameObject);
  42.  
  43. //Sets this to not be destroyed when reloading scene
  44. DontDestroyOnLoad(gameObject);
  45.  
  46. //Assign enemies to a new List of Enemy objects.
  47. enemies = new List<Enemy>();
  48.  
  49. //Get a component reference to the attached BoardManager script
  50. boardScript = GetComponent<BoardManager>();
  51.  
  52. //Call the InitGame function to initialize the first level
  53. InitGame();
  54. }
  55.  
  56. //This is called each time a scene is loaded.
  57. void OnLevelWasLoaded(int index)
  58. {
  59. //Add one to our level number.
  60. level++;
  61. //Call InitGame to initialize our level.
  62. InitGame();
  63. }
  64.  
  65. //Initializes the game for each level.
  66. void InitGame()
  67. {
  68. //While doingSetup is true the player can't move, prevent player from moving while title card is up.
  69. doingSetup = true;
  70.  
  71. //Get a reference to our image LevelImage by finding it by name.
  72. levelImage = GameObject.Find("LevelImage");
  73.  
  74. //Get a reference to our text LevelText's text component by finding it by name and calling GetComponent.
  75. levelText = GameObject.Find("LevelText").GetComponent<Text>();
  76.  
  77. //Set the text of levelText to the string "Day" and append the current level number.
  78. levelText.text = "Day " + level;
  79.  
  80. //Set levelImage to active blocking player's view of the game board during setup.
  81. levelImage.SetActive(true);
  82.  
  83. //Call the HideLevelImage function with a delay in seconds of levelStartDelay.
  84. Invoke("HideLevelImage", levelStartDelay);
  85.  
  86. //Clear any Enemy objects in our List to prepare for next level.
  87. enemies.Clear();
  88.  
  89. //Call the SetupScene function of the BoardManager script, pass it current level number.
  90. boardScript.SetupScene(level);
  91.  
  92. }
  93.  
  94.  
  95. //Hides black image used between levels
  96. void HideLevelImage()
  97. {
  98. //Disable the levelImage gameObject.
  99. levelImage.SetActive(false);
  100.  
  101. //Set doingSetup to false allowing player to move again.
  102. doingSetup = false;
  103. }
  104.  
  105. //Update is called every frame.
  106. void Update()
  107. {
  108. //Check that playersTurn or enemiesMoving or doingSetup are not currently true.
  109. if(playersTurn || enemiesMoving || doingSetup)
  110.  
  111. //If any of these are true, return and do not start MoveEnemies.
  112. return;
  113.  
  114. //Start moving enemies.
  115. StartCoroutine (MoveEnemies ());
  116. }
  117.  
  118. //Call this to add the passed in Enemy to the List of Enemy objects.
  119. public void AddEnemyToList(Enemy script)
  120. {
  121. //Add Enemy to List enemies.
  122. enemies.Add(script);
  123. }
  124.  
  125.  
  126. //GameOver is called when the player reaches 0 food points
  127. public void GameOver()
  128. {
  129. //Set levelText to display number of levels passed and game over message
  130. levelText.text = "After " + level + " days, you starved.";
  131.  
  132. //Enable black background image gameObject.
  133. levelImage.SetActive(true);
  134.  
  135. //Disable this GameManager.
  136. enabled = false;
  137. }
  138.  
  139. //Coroutine to move enemies in sequence.
  140. IEnumerator MoveEnemies()
  141. {
  142. //While enemiesMoving is true player is unable to move.
  143. enemiesMoving = true;
  144.  
  145. //Wait for turnDelay seconds, defaults to .1 (100 ms).
  146. yield return new WaitForSeconds(turnDelay);
  147.  
  148. //If there are no enemies spawned (IE in first level):
  149. if (enemies.Count == 0)
  150. {
  151. //Wait for turnDelay seconds between moves, replaces delay caused by enemies moving when there are none.
  152. yield return new WaitForSeconds(turnDelay);
  153. }
  154.  
  155. //Loop through List of Enemy objects.
  156. for (int i = 0; i < enemies.Count; i++)
  157. {
  158. //Call the MoveEnemy function of Enemy at index i in the enemies List.
  159. enemies[i].MoveEnemy ();
  160.  
  161. //Wait for Enemy's moveTime before moving next Enemy,
  162. yield return new WaitForSeconds(enemies[i].moveTime);
  163. }
  164. //Once Enemies are done moving, set playersTurn to true so player can move.
  165. playersTurn = true;
  166.  
  167. //Enemies are done moving, set enemiesMoving to false.
  168. enemiesMoving = false;
  169. }
  170. }
  171. }
Add Comment
Please, Sign In to add comment