Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.22 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class LevelControl : MonoBehaviour
  6. {
  7.         public int      wave;           //the wave we're on
  8.         public int      maxEnemies;     //max ammount of enemies on the map at any given time
  9.         public int      currentEnemies = 1;  //amount of enemies currently on the map
  10.         public float    startTimer; //countdown until timer ends
  11.         public bool     isCountingDown;
  12.         public bool     isSpawning;
  13.         public bool     roundIsOver;
  14.         public Text     watDo;
  15.    
  16.         public GameObject[] enemies;     //array of enemies
  17.         public GameObject[] spawnPoints;
  18.         public int[]        spawnAmmounts;
  19.    
  20.    
  21.         public void CountDown ()  //countsdown  1 every 1 second
  22.         {
  23.                 if (startTimer != 0) {
  24.                         startTimer -= 1;
  25.                         isCountingDown = true;
  26.                 } else {
  27.                        
  28.                         isCountingDown = false;
  29.                 }
  30.        
  31.         }
  32.    
  33.         // Use this for initialization
  34.         void Start ()
  35.         {
  36.                 watDo.text = " ";
  37.                 wave = 0;
  38.                 maxEnemies = 5;
  39.                 roundIsOver = true;
  40.                 startTimer = 5;
  41.                 spawnPoints = GameObject.FindGameObjectsWithTag ("spawner");
  42.         }
  43.    
  44.         // Update is called once per frame
  45.         void Update ()
  46.         {
  47.                 if (currentEnemies > 0) {
  48.                         roundIsOver = false;
  49.                 } else {
  50.                         roundIsOver = true;
  51.                 }
  52.                 if (startTimer <= 0) {
  53.                         startTimer = 0;
  54.                         isCountingDown = false;
  55.                         roundIsOver = false;
  56.                 }
  57.                 if (startTimer != 0 && startTimer != 5) {
  58.                         isCountingDown = true;
  59.                         watDo.text = startTimer.ToString ();
  60.                         roundIsOver = true;
  61.                        
  62.                 }
  63.                 if (startTimer == 0 && roundIsOver == false) {
  64.                         watDo.text = " ";
  65.                 }
  66.                
  67.                 if (roundIsOver && isCountingDown == false) {
  68.                         watDo.text = "Hit Space to Start the Round";
  69.                 }
  70.                
  71.                 if (isSpawning) {
  72.                         watDo.text = " ";
  73.                 }
  74.                
  75.                
  76.        
  77.                 if (startTimer > 0) {          //if we're either counting down or still waiting
  78.                         roundIsOver = true;    //round is still in over state
  79.                 } else {                       //otherwise
  80.                         roundIsOver = false;   //the round is over and we need to
  81.                         NextWave ();           //start the next wave
  82.                 }
  83.        
  84.                 if (roundIsOver) {                                          //if the round is in over state
  85.                         if (Input.GetKeyDown ("space")) {                   //"press space to start the countdown timer
  86.                                 if (startTimer != 0) {                      // but only if its not 0
  87.                                         if (isCountingDown == false) {
  88.                                                 InvokeRepeating ("CountDown", 1, 1);
  89.                                                
  90.                                         } else {
  91.                                                 Debug.Log ("Already counting down you retard!");
  92.                                         }
  93.                                 }  
  94.                         }
  95.                 }      
  96.         }
  97.    
  98.    
  99.         void NextWave ()   //resets the spawn timer, adds 1 to the wave #, and then spawns based on wave
  100.         {
  101.                 CancelInvoke ("CountDown");
  102.                 roundIsOver = false;
  103.                 startTimer = 5;
  104.                 SetWave (wave + 1);
  105.                 roundIsOver = false;
  106.                 Debug.Log ("spawning?");
  107.                 StartCoroutine (Spawn (0, 5, 1));
  108.        
  109.         }
  110.    
  111.         void SetWave (int waveToSet) //sets the wave number
  112.         {
  113.                 wave = waveToSet;
  114.         }
  115.    
  116.         IEnumerator randomSpawn (int arrayIndex, int amount) //spawns in random spawnPoint
  117.         { //call with StartCoroutine (randomSpawn (enemy array index, number to spawn));
  118.                 for (int i = 0; i <= amount; i++) {
  119.                         if (currentEnemies < maxEnemies) {
  120.                                 GameObject randomSpawn = spawnPoints [(Random.Range (0, spawnPoints.Length))];
  121.                                 Instantiate (enemies [arrayIndex],
  122.                              randomSpawn.transform.position,
  123.                              randomSpawn.transform.rotation);
  124.                
  125.                                 yield return new WaitForSeconds (1);
  126.                                 isSpawning = true;
  127.                                 currentEnemies ++;
  128.                         } else {
  129.                                 isSpawning = false;
  130.                         }
  131.            
  132.                 }
  133.         }
  134.        
  135.         IEnumerator Spawn (int arrayIndex, int amount, int spawnArrayIndex)  //spawns in specific spawnPoint
  136.         { //call with StartCoroutine (Spawn (enemy array index, number to spawn, spawnpoint array index));
  137.                 for (int i = 0; i <= amount; i++) {
  138.                         if (currentEnemies < maxEnemies) {
  139.                                 Instantiate (enemies [arrayIndex],
  140.                              spawnPoints [spawnArrayIndex].transform.position,
  141.                              spawnPoints [spawnArrayIndex].transform.rotation);
  142.                
  143.                                 yield return new WaitForSeconds (1);
  144.                                 currentEnemies ++;
  145.                                 isSpawning = true;
  146.                         } else {
  147.                                 isSpawning = false;
  148.                         }
  149.            
  150.                 }
  151.         }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement