Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class WaveSpawner : MonoBehaviour
  6. {
  7.     public static int EnemyAlive = 0;
  8.  
  9.     public Transform enemyPrefab;
  10.  
  11.     public Wave[] waves;
  12.  
  13.     public Transform spawnPoint;
  14.  
  15.     public float timeBetweenWaves = 5f;
  16.     private float countdown = 2f;
  17.  
  18.     public Text waveCountdownText;
  19.  
  20.     private int waveIndex = 0;
  21.  
  22.     private float timeToNextSpawn = 0f;
  23.  
  24.     public GameObject[] enemies;
  25.     int spawnedEnemies = 0;
  26.  
  27.  
  28.     void Update()
  29.     {
  30.         if (EnemyAlive > 0)
  31.         {
  32.             return;
  33.         }
  34.         if (countdown <= 0f)
  35.         {
  36.             if (waveIndex > waves.Length)
  37.             {
  38.                 SpawnRandomWave();
  39.             }
  40.             else
  41.             {
  42.             StartCoroutine(SpawnWave());
  43.             countdown = timeBetweenWaves;
  44.             return;
  45.             }
  46.         }
  47.  
  48.         countdown -= Time.deltaTime;
  49.  
  50.         countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
  51.  
  52.        // waveCountdownText.text = string.Format("{0:00.00}", countdown);
  53.     }
  54.  
  55.  
  56.     IEnumerator SpawnWave ()
  57.     {
  58.         PlayerStats.Rounds++;
  59.  
  60.         Wave wave = waves[waveIndex];
  61.  
  62.         for (int i = 0; i < wave.count; i++)
  63.         {
  64.             SpawnEnemy(wave.enemy);
  65.             yield return new WaitForSeconds(1f / wave.rate);
  66.         }
  67.         waveIndex++;
  68.        
  69.       /*  if (waveIndex == waves.Length)
  70.         {
  71.             Debug.Log("Won");
  72.             this.enabled = false;
  73.         }*/
  74.        
  75.     }
  76.     void SpawnEnemy(GameObject enemy)
  77.     {
  78.         Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
  79.         EnemyAlive++;
  80.     }
  81.  
  82.     void SpawnRandomWave()
  83.     {
  84.         timeToNextSpawn += Time.deltaTime;
  85.         if(timeToNextSpawn > 1.0f / waveIndex)
  86.         {
  87.             SpawnEnemy(enemies[Random.Range(0, 3)]);
  88.             timeToNextSpawn = 0f;
  89.             spawnedEnemies++;
  90.         }
  91.         // checked spawnedEnemies against #2 spawn then start new wave.
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement