My Books

My Software

The ParticleEngine Class

We now want to repeat what we did when we made our Particle class to create a ParticleEngine class. Like before, right-click on your project in the Solution Explorer, and choose Add > New Item from the popup menu. The Add New Item dialog will appear. Select the Class template, and change the name (near the bottom) to ParticleEngine.cs. Click on the Add button when you have done this. The new class file will open up in the main editor window.

The first change we will want to make is to make the class public, so near the top of the file, find the line that says:

classParticleEngine

and change it to say:

publicclassParticleEngine

The ParticleEngine's Properties & Instance Variables

We now need to add some properties and instance variables to the class to keep track of important information about our particle. Add the following as instance variables:

The first variable is a random number generator that we will use when we are generating our particles. The EmitterLocation allows the user to change the location that the particles are originating at. particles is a variable that contains all of the particles that are currently active in the particle engine, and textures contains all of the textures available for use in the particle engine.

The Constructor

Once again, our constructor only needs to prepare the instance variables for use. In the code below, a couple of the parameters are assigned by the user, and a couple are created with default values. We want the user to tell us what textures we can use, as well as the default location of the emitter, but the list of particles is for the particle engine to manage, and the user should not have to worry about them. Also, random is given a default new random number generator. Add the following code to your ParticleEngine class:

Generating Particles

One of the important things this class will need to be able to do is to create particles with parameters that make the engine behave like you want it. We are going to create a default method for generating particles below. However, there is more than one way to do this. When your particle engine is complete and working, I would suggest immediately coming back to this method and changing things around to see what you can get. You can really do a lot with a particle engine, simply by changing the way it generates particles. Add the following code to your ParticleEngine class:

In this code, we start off by adding 10 new particles each time. We then go through the list of particles and tell them to update themselves. If any of them have "died" (their time to live has reached 0), the particle is removed.

The Draw() Method

The Draw() method is a very simple method of this class, since it is mostly just going to tell the paricles to draw themselves. Below is the code for the Draw() method:

using Statements

Also, don't forget to add the following two using statements so that your code will compile correctly to the top of your file:

usingMicrosoft.Xna.Framework.Graphics;
usingMicrosoft.Xna.Framework;

This should complete the entire ParticleEngine class, and we are now ready to use our particle engine in our game. We will do this in the final section of this tutorial. Below is the entire code for this class, all assembled: