Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class LevelControl : MonoBehaviour
- {
- public int wave; //the wave we're on
- public int maxEnemies; //max ammount of enemies on the map at any given time
- public int currentEnemies = 1; //amount of enemies currently on the map
- public float startTimer; //countdown until timer ends
- public bool isCountingDown;
- public bool isSpawning;
- public bool roundIsOver;
- public Text watDo;
- public GameObject[] enemies; //array of enemies
- public GameObject[] spawnPoints;
- public int[] spawnAmmounts;
- public void CountDown () //countsdown 1 every 1 second
- {
- if (startTimer != 0) {
- startTimer -= 1;
- isCountingDown = true;
- } else {
- isCountingDown = false;
- }
- }
- // Use this for initialization
- void Start ()
- {
- watDo.text = " ";
- wave = 0;
- maxEnemies = 5;
- roundIsOver = true;
- startTimer = 5;
- spawnPoints = GameObject.FindGameObjectsWithTag ("spawner");
- }
- // Update is called once per frame
- void Update ()
- {
- if (currentEnemies > 0) {
- roundIsOver = false;
- } else {
- roundIsOver = true;
- }
- if (startTimer <= 0) {
- startTimer = 0;
- isCountingDown = false;
- roundIsOver = false;
- }
- if (startTimer != 0 && startTimer != 5) {
- isCountingDown = true;
- watDo.text = startTimer.ToString ();
- roundIsOver = true;
- }
- if (startTimer == 0 && roundIsOver == false) {
- watDo.text = " ";
- }
- if (roundIsOver && isCountingDown == false) {
- watDo.text = "Hit Space to Start the Round";
- }
- if (isSpawning) {
- watDo.text = " ";
- }
- if (startTimer > 0) { //if we're either counting down or still waiting
- roundIsOver = true; //round is still in over state
- } else { //otherwise
- roundIsOver = false; //the round is over and we need to
- NextWave (); //start the next wave
- }
- if (roundIsOver) { //if the round is in over state
- if (Input.GetKeyDown ("space")) { //"press space to start the countdown timer
- if (startTimer != 0) { // but only if its not 0
- if (isCountingDown == false) {
- InvokeRepeating ("CountDown", 1, 1);
- } else {
- Debug.Log ("Already counting down you retard!");
- }
- }
- }
- }
- }
- void NextWave () //resets the spawn timer, adds 1 to the wave #, and then spawns based on wave
- {
- CancelInvoke ("CountDown");
- roundIsOver = false;
- startTimer = 5;
- SetWave (wave + 1);
- roundIsOver = false;
- Debug.Log ("spawning?");
- StartCoroutine (Spawn (0, 5, 1));
- }
- void SetWave (int waveToSet) //sets the wave number
- {
- wave = waveToSet;
- }
- IEnumerator randomSpawn (int arrayIndex, int amount) //spawns in random spawnPoint
- { //call with StartCoroutine (randomSpawn (enemy array index, number to spawn));
- for (int i = 0; i <= amount; i++) {
- if (currentEnemies < maxEnemies) {
- GameObject randomSpawn = spawnPoints [(Random.Range (0, spawnPoints.Length))];
- Instantiate (enemies [arrayIndex],
- randomSpawn.transform.position,
- randomSpawn.transform.rotation);
- yield return new WaitForSeconds (1);
- isSpawning = true;
- currentEnemies ++;
- } else {
- isSpawning = false;
- }
- }
- }
- IEnumerator Spawn (int arrayIndex, int amount, int spawnArrayIndex) //spawns in specific spawnPoint
- { //call with StartCoroutine (Spawn (enemy array index, number to spawn, spawnpoint array index));
- for (int i = 0; i <= amount; i++) {
- if (currentEnemies < maxEnemies) {
- Instantiate (enemies [arrayIndex],
- spawnPoints [spawnArrayIndex].transform.position,
- spawnPoints [spawnArrayIndex].transform.rotation);
- yield return new WaitForSeconds (1);
- currentEnemies ++;
- isSpawning = true;
- } else {
- isSpawning = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement