Day 53: Wave System Part 1
Objective: Create a wave system and when max wave hit spawn boss!
To start this journey, we will need to be in the spawn manager. We will need to create a few new variables. One to track the current wave and one for max waves.
Next we will need to create a function that’ll start spawning the enemies.
In this function we’ll check to see if the current wave is equal to the max wave. If it is spawn the boss if not spawn regular enemies. We will also increase the current wave by one. Next will be to refactor the IEnumerator to take in the enemies to spawn. We will take the amount of enemies we specified in the inspector and multiply by the current wave. This will allow more enemies to spawn each wave.
We will also have the while loop stop if enemiesToSpawn is equal to zero or less. We will also subtract one from enemiesToSpawn each time a enemy spawns. We do this to make the routine stop.
Now to call the start wave function. We need to adjust our old StartSpawning function. We’ll call the StartWave and start the coroutine for the powerups. For calculating the total weights for the enemies and powerups will be moved to the start function.
Now we will need to know when to call the asteroid to be spawned again so we can move on to the next wave. We’ll move to our Game Manager script. In the GameManager we will create a few new variables. We need a bool for a while loop to keep searching while it’s true. Next is an array for the alive enemies to be tracked.
Next We’ll create a public function to start searching for enemies. This function will turn the bool to true and start the routine to search for enimes.
Inside our IEnumerator, we’ll have a loop that’ll run as long as the bool is true, while there are enemies in the scene. We do this by finding any gameobject with the tag “Enemy” and assign it to the array. If the array becomes zero, we’ll call a function from the spawn manager, spawn the asteroid, and turn the bool false again.
In the Spawn manager we call the StartCheckingForenemies function after the coroutine breaks from its loop.
The function we called in the Game Manager to stop all coroutines is in the spawn manager and all it does is stop all the coroutines in the script. So for us it’ll stop spawning enemies and power ups while the asteroid is up.
Now we have a working wave system! Tomorrow we’ll discuss how to get the UI implemented and get the asteroid to have a fun effect when it spawns.
Thank you for reading! Have a wonderful day!