Hi! I’m back — 2 days off and my mind is refreshed. In case you missed my last article it was just about how to take a break and refresh. I had done 37 straight days of work on this game, and even though I could have easily kept going I thought I would give my mind a break. I’m glad I did, I killed it today.
Well, I think I killed it. See I had a lot of for loops and while loops and a lot of code I ended up deleting and just went with a basic approach. This may not be the most clean code but it works.
Today, I added waves to my enemies. Just 3 for now. This is how I did it:
I created 2 integer variables to hold the information for how many enemies I have spawned and what wave I am on.
Then in the start function I just set my wave to 1, enemy spawned at 0 and got connection to my UIManager script where I will be sending information on what wave we are on so it can print the correct wave to the screen.
Before I forget, don’t forget to create a new Wave_Text in the UI so I could manipulate what wave it printed. I’m not going to go into how to do that, if you are this far along you already know.
For now, testing purposes I just put the below into the Update function so we can get the game started. Later I will have a bit of a cutscene that will open up the gameplay.
So I just hit B, and it loads up the 2 spawning coroutines I have. Below you will see how I wrote the enemy spawn wave.
So breaking it down — this is spawnwave1. I also have a spawnwave2 and spawnwave3 that are basically the same thing with a few different parameters. You remember above when I said I had some For loops running and ended up deleting a lot of my work because it wasn’t working? This led me to this. It’s long winded and definitely not ideal but it works. I will be changing this once I get more implantation into my game, but for now I’m sticking with it.
Basically while my _enemySpawned total is less than 10 it will run the while loop. I set the second wave to 20 and the third wave to 30. Once inside it’s the same instantiate method as before.. getting a random position to spawn, getting a random number to see if it will spawn an asteroid or enemy and then doing the actual spawn. Then it adds one to my _enemySpawned. The first wave will spawn every 5 seconds.
Once the enemy has spawned 10 it adds one to the wave, sends the wave total to my UI manager which will call the UpdateWave function and print wave 2. Resets the _enemyspawned to 0 so it can start counting again. Then I stop the coroutine so it won’t run wave 1 and 2 together. I set 2 seconds to wait and then call the wave2 routine.
Last, back in my UIManager this is the code to have it display on screen:
Basically just created an IEnumerator that will print the _currentwave on screen. I call this coroutine inside of the UpdateWave I created.
See — pretty basic and long winded and I will definitely update this to show you how you can do this cleaner with a for loop. It will be a good addition to this article to show you you can do things differently.
Let’s see it in action.
Just a highlight how it jumps to wave 2.
So that’s it, now on to another item — we are going to get evil on the player again muhahaha.. a negative powerup that will do something nasty when the player collects.