Putting the Pieces Together


Now that you have the special effects class implemented, you can go back to the game engine code file and implement the code there to make the game engine use these newly created special effects. First, you need to declare the class as a variable to the game engine:

 // The special effects class private SpecialEffects particleEffects = null; 

Then, you create the special effects class. You should do this part in the LoadWorld method, directly after loading the level:

 // Create the special effects class particleEffects = new SpecialEffects(sampleFramework.Device); 

Next, you want to ensure that the particles are updated every frame, so find the OnFrameMove method and add this code directly before updating the local player:

 // Update the special effects if they exist if (particleEffects != null) {     particleEffects.Update(elapsedTime); } 

It goes without saying that you want to actually render the particles as well. Add this code to the end of the RenderGameScene method:

 particleEffects.Draw(this); 

Of course, the particle system is useless unless you add some particles into it at some point. Because this system is used to add a spark to the firing of the bullets, add this code to the end of the OnPlayerFire method:

 // Fire effect if (particleEffects != null) {     // Get the barrel position and bullet velocity     Vector3 start, vel;     p.GetBulletStartLocation(out start, out vel);     // Now add a firing effect to that     particleEffects.AddFiringEffect(50, start, vel); } 

Finally, all you need to do is ensure that the particle system is cleaned up at the appropriate time. Add the cleanup code to the OnDestroyDevice method:

 // Clean up the special effects if (particleEffects != null) {     particleEffects.Dispose(); } 

With that, you now have the special effects implemented and hooked into the game. See Figure 18.1 for an example of the particle system in action. I'm sure you can think of even better effects to add.

Figure 18.1. Tankerswith special effects.




Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net