Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class WaveSpawner : MonoBehaviour
- {
- public static int EnemyAlive = 0;
- public Transform enemyPrefab;
- public Wave[] waves;
- public Transform spawnPoint;
- public float timeBetweenWaves = 5f;
- private float countdown = 2f;
- public Text waveCountdownText;
- private int waveIndex = 0;
- private float timeToNextSpawn = 0f;
- public GameObject[] enemies;
- int spawnedEnemies = 0;
- void Update()
- {
- if (EnemyAlive > 0)
- {
- return;
- }
- if (countdown <= 0f)
- {
- if (waveIndex > waves.Length)
- {
- SpawnRandomWave();
- }
- else
- {
- StartCoroutine(SpawnWave());
- countdown = timeBetweenWaves;
- return;
- }
- }
- countdown -= Time.deltaTime;
- countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
- // waveCountdownText.text = string.Format("{0:00.00}", countdown);
- }
- IEnumerator SpawnWave ()
- {
- PlayerStats.Rounds++;
- Wave wave = waves[waveIndex];
- for (int i = 0; i < wave.count; i++)
- {
- SpawnEnemy(wave.enemy);
- yield return new WaitForSeconds(1f / wave.rate);
- }
- waveIndex++;
- /* if (waveIndex == waves.Length)
- {
- Debug.Log("Won");
- this.enabled = false;
- }*/
- }
- void SpawnEnemy(GameObject enemy)
- {
- Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
- EnemyAlive++;
- }
- void SpawnRandomWave()
- {
- timeToNextSpawn += Time.deltaTime;
- if(timeToNextSpawn > 1.0f / waveIndex)
- {
- SpawnEnemy(enemies[Random.Range(0, 3)]);
- timeToNextSpawn = 0f;
- spawnedEnemies++;
- }
- // checked spawnedEnemies against #2 spawn then start new wave.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement