WaveSpawner: Part 3
It has been a while since I have work on my game since I went on 2 1/2 week hiatus to see friends and families I was unable to see since the pandemic shutdown, but I am back and ready to get back to the wonderful world of game dev.
I should have left off at a better point and not in the middle of developing a WaveSpawner, but I thought I could get it done before I left…wrong. Anyways, lesson learned to focus on smaller tasks or polishes when you know you are going to step away for some time because it takes time to remember where you left off and what you line of thinking was at the moment.
I left off where the SpawnState.Spawning is looping through the wave count to find the current wave and spawning the amount of enemies for that wave with a spawn rate. Once the spawning has been complete the state is set to Waiting where the player must destroy all of the enemies before moving on to the next wave.
In the Update, we want to check to see if we are in the SpawnState.Waiting and if we are then check to see if any of the enemies are still alive.
In checking to see if there are any enemies alive, we will create a bool method that will use either FindGameObjectWithTag or FindGameObjectsWithTag and if it is null or the array length is 0 then we will return a false value if not return a true value.
This can be very taxing since this is looping through all of the GameObjects on the screen in order to find the ones with the tag “Enemy”, especially if you have multiple enemies with large environments and any particle systems. In reduce this we will run this check on a fixed time.
We will declared a private float that will be store the amount of time we want to wait before performing an enemy “search” and we will make this 1 second.
Back in the EnemyIsAlive method we are going to subtract Time.deltaTime and when the searchCountdown gets to 0, we perform that enemy alive check. Again, this helps with the performance of the game because it is only performing this check every second instead of every frame and is only doing this when it is in the SpawnState of Waiting.
Now that we have our EnemyIsAlive method lets put it to use by checking to see if it is FALSE when the SpawnState is Waiting in the Update. If it is false, meaning there are no enemies left, we will want to begin the next wave and if not, we want to return so if doesn’t continue on to the next lines of code.
One thing before moving on is we need to reset the searchCount back to 1 when it gets to 0 so it starts the cycle over.
Right now, we have the different SpawnSate setup (Waiting, Spawning, & Counting) and in part 4 we will finish the Wave Spawner by putting all of these states into action.