Friday, 29 October 2010

Tutorial 9 : Multiple Waves

In this tutorial we are going to create the class that will handle the creation of our waves, update our waves, and then move the player onto the next wave when the previous one has finished.

This will be one of the most important classes in our game. We are going to store all our waves in one big queue, and just move through them one by one with a short period of time in-between waves so the player has time to breath and make new towers. This class will also determine how many enemies we create per wave, so we can make the waves harder as time progresses.

So, let’s get started, start off by creating a new class called “WaveManager.cs” and add the following fields and properties :

privateint numberOfWaves; // How many waves the game will have

privatefloat timeSinceLastWave; // How long since the last wave ended

private Queue<Wave> waves = new Queue<Wave>(); // A queue of all our waves

private Texture2D enemyTexture; // The texture used to draw the enemies

privatebool waveFinished = false; // Is the current wave over?

private Level level; // A reference to our level class

public Wave CurrentWave // Get the wave at the front of the queue

{

get { return waves.Peek(); }

}

public List<Enemy> Enemies // Get a list of the current enemeies

{

get { return CurrentWave.Enemies; }

}

publicint Round // Returns the wave number

{

get { return CurrentWave.RoundNumber + 1; }

}

As I explained earlier we are going to store our waves in a queue so we can access our waves in the order they were added. Next we are going to add in a constructor for the class :

This is where we are going to create all of the waves for the game. As you can see we progressively add wave after wave with the use of a for loop. The only interesting part of this method are the first two lines in the for loop.

These two lines alter the number of enemies each wave has, basically what these two lines do is every six waves, six extra enemies are produced. So for example the 1st, 2nd, 3rd, 4th and 5th waves will have 6 enemies, the 6th, 7th, 8th, 9th, 10th and 11th waves will have 12 enemies, and the 12th 13th… wave will have 18 enemies.

Of course the mechanism is extremely boring and in your own games you are probably going to want to add in boss levels, or fast levels etc, but for simplicity, this is all we are going to do.

Now we are going to add a method that will start the next wave :

privatevoid StartNextWave()

{

if (waves.Count > 0) // If there are still waves left

{

waves.Peek().Start(); // Start the next one

timeSinceLastWave = 0; // Reset timer

waveFinished = false;

}

}

Here we just check if there is a wave to start, and if there is we start it, and reset our timer. We need to add a call to this method in our constructor otherwise our waves will never start! At the bottom of the constructor add this :

StartNextWave();

All that is left to do now is to update our waves and then draw them, let’s start by updating them :

ah, ok thanks. Also, how do you draw the towers so amazingly? They are awesome and I want to learn how to do that. And another thing too, if I want to make the bullet turn to face the enemy. My bullet is an arrow, not a ball and I want it to turn to the enemy when shot at it.Just as a note: I'm not following the tutorial exactly, because I am actually going to add some stuff and put it on the Xbox Live Indie Games Marketplace, because I don't want to infringe on copyright or anything. But still, this tutorial is helping a lot. I will be sure to give you credit and a free download code FireFly!

I am glad you like my towers!!! I just started by drawing a square body for the base of the tower, and then kept tweaking the shape / symmetry of it until I was satisfied with it. Then to finish them off I added a radial gradient colour!

As for the rotating bullets, they should already work with the code I posted in Tutorial 7. As long as your arrow points up, it should get rotated to the right direction!

It would be awesome to see some Tower Defences on the Xbox! It will be interesting to see what kind of control scheme you have created. I can't wait to see what you have come up with!

How would I go about making it so that you had to press a "ready" button before the next round started? I've almost got it now but when I press the button nothing happens. Its like the system is only checking if its ready instantly after the round is over, and after that never checking again.

Sorry but I need help again :( Now it is working and sending new wave on time but when it start new wave a tower stop shooting at last wave And shoot only on new. I think a bug is in CurrentWave. Tower shooting only on CurrentWave and that is changing when we change CurrentWaveIndex.