SHARE
TWEET

Wave_Spawner_Rand_Enemy

a guest Nov 8th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class WaveSpawner : MonoBehaviour
  5. {
  6.     //public Enemy enemyInstance;
  7.     //public int health;
  8.  
  9.     public enum SpawnState { SPAWNING, WAITING, COUNTING };
  10.  
  11.     [System.Serializable]
  12.     public class Wave
  13.     {
  14.         public string name;
  15.         public Transform[] enemies;
  16.         public int count;
  17.         public float rate;
  18.     }
  19.  
  20.     public Wave[] waves;
  21.     private int nextWave = 0;
  22.     public int NextWave
  23.     {
  24.         get { return nextWave + 1; }
  25.     }
  26.  
  27.     public Transform[] spawnPoints;
  28.  
  29.     public float timeBetweenWaves = 5f;
  30.     private float waveCountdown;
  31.     public float WaveCountdown
  32.     {
  33.         get { return waveCountdown; }
  34.     }
  35.  
  36.     private float searchCountdown = 1f;
  37.  
  38.     private SpawnState state = SpawnState.COUNTING;
  39.     public SpawnState State
  40.     {
  41.         get { return state; }
  42.     }
  43.  
  44.     void Start()
  45.     {
  46.         if (spawnPoints.Length == 0)
  47.         {
  48.             Debug.LogError("No spawn points referenced.");
  49.         }
  50.  
  51.         waveCountdown = timeBetweenWaves;
  52.     }
  53.  
  54.     void Update()
  55.     {
  56.         if (state == SpawnState.WAITING)
  57.         {
  58.             if (!EnemyIsAlive())
  59.             {
  60.                 WaveCompleted();
  61.             }
  62.             else
  63.             {
  64.                 return;
  65.             }
  66.         }
  67.  
  68.         if (waveCountdown <= 0)
  69.         {
  70.             if (state != SpawnState.SPAWNING)
  71.             {
  72.                 StartCoroutine(SpawnWave(waves[nextWave]));
  73.             }
  74.         }
  75.         else
  76.         {
  77.             waveCountdown -= Time.deltaTime;
  78.         }
  79.     }
  80.  
  81.     void WaveCompleted()
  82.     {
  83.         Debug.Log("Wave Completed!");
  84.  
  85.         state = SpawnState.COUNTING;
  86.         waveCountdown = timeBetweenWaves;
  87.  
  88.         if (nextWave + 1 > waves.Length - 1)
  89.         {
  90.             nextWave = 0;
  91.             Debug.Log("ALL WAVES COMPLETE! Looping...");
  92.         }
  93.         else
  94.         {
  95.             nextWave++;
  96.             //enemyInstance.health += 2;
  97.         }
  98.     }
  99.  
  100.     bool EnemyIsAlive()
  101.     {
  102.         searchCountdown -= Time.deltaTime;
  103.         if (searchCountdown <= 0f)
  104.         {
  105.             searchCountdown = 1f;
  106.             if (GameObject.FindGameObjectWithTag("Enemy") == null)
  107.             {
  108.                 return false;
  109.             }
  110.         }
  111.         return true;
  112.     }
  113.  
  114.     IEnumerator SpawnWave(Wave _wave)
  115.     {
  116.         Debug.Log("Spawning Wave: " + _wave.name);
  117.         state = SpawnState.SPAWNING;
  118.  
  119.         for (int i = 0; i < _wave.count; i++)
  120.         {
  121.             SpawnEnemy(_wave.enemies);
  122.             yield return new WaitForSeconds(1f / _wave.rate);
  123.         }
  124.  
  125.         state = SpawnState.WAITING;
  126.  
  127.         yield break;
  128.     }
  129.  
  130.     void SpawnEnemy(Transform[] enemies)
  131.     {
  132.         /*Debug.Log("Spawning Enemy: " + _enemy.name);
  133.         var health = enemyInstance.GetComponent<Enemy>().health;*/
  134.  
  135.         int randEnemy = Random.Range(0, 2);
  136.         Transform _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
  137.         Instantiate(enemies[randEnemy], _sp.position, _sp.rotation);
  138.     }
  139.  
  140. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top