A Simple Wave System
In our game, we decided that a Wave system would be a good way to give the Player a since of progression.
So instead of the endless onslaught of enemies that we have had up to this point, the Player will experience clusters of enemies.
Now let’s see how we can setup a simple Wave system that is based on an amount of enemies to be spawned. Later we we can modify the system to allow for Boss fights and possibly even Mini Boss fights.
First, We will add some new variables to the SpawnManager Class to accommodate the new system. All three are fairly simple INTs. _waveCounts is a Serialized Array and will hold the intended amount of enemies for each wave. _spawnedEnemiesInWave will keep track of how many enemies get spawned so that we know when to stop spawning for any particular wave. _currentWave will be, as expected, our Wave count and by default starts at 0 since we will use it directly with the _waveCounts Array.
WaveStart() will be how we start each new Wave and end the game when the waves have finished. This is where we will handle the _spawning Bool that was previously set in Start(). As well, We reset the spawned Enemy Counter and Display the Wave Number at the start of each new wave. Note that we have to add 1 to _currentWave so that when Display shows it is “Wave 1” instead of “Wave 0”. Us coders will understand that 0 is a starting point for most things we do, but everyone else starts from 1.
In the StartSpawning(), we want to remove the EnemySpawnRoutine() and replace it with the WaveStart() since it is now controlling the EnemySpawnRoutine() itself.
Lastly, we have to modify our EnemySpawnRoutine() to work with the Wave spawning. We wrap the original routing in a second While statement that makes sure it doesn’t spawn more Enemy ships than is called for in the Wave. After the while loop, we want to check that the EnemyContainer Pool, were we have been storing our active enemies, is empty before we cycle to the next wave. This way we can insure that that the Player has killed all the Enemy ships before they are hit with another wave of them. We also add a Yield statement outside the IF so that the game doesn’t get stuck in a endless loop when the last enemy is spawned.
And With a single line added to the SpawnEnemy(), we are able to count when an Enemy is spawned into the Wave.