Adding Your Particle Engine to Your Game

Now that you have an effect and a particle engine, you need to modify your code to create explosions when ships are hit.

The first thing that you'll need to do is add the texture image for the particles to use. This texture will give the particles their color—every particle will be assigned a random pixel from the texture and will be given the color at that pixel in the texture.

If you haven't already, download the source code for this chapter of the book. In the 3D Game\Content\Textures folder, you'll find an image file called Particle.png. Add it to your project by right-clicking the Content\Textures folder in Solution Explorer, selecting Add → Existing Item..., and then navigating to the Particle.png file and selecting it.

Now open the ModelManager class, and let's get to work on adding some cool explosions to your game. Add the following class-level variables:

List<ParticleExplosion> explosions = new List<ParticleExplosion>(  );
ParticleExplosionSettings particleExplosionSettings =
    new ParticleExplosionSettings(  );
ParticleSettings particleSettings = new ParticleSettings(  );
Effect explosionEffect;
Texture2D explosionTexture;

These variables are all pretty self-explanatory: you have a list of ParticleExplosions so your class can update and draw them, an instance of both settings classes for your particles, and Effect and Texture2D objects for drawing your particles.

Next, you'll need to load the resources for your particles, set the current ...

Get Learning XNA 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.