Firing Shots

Many types of games have some form of projectile that is thrown or shot toward other players or enemies. How would you go about adding the ability to shoot a projectile of some sort in your game? Think about it at its most basic level, and you'll realize that a projectile flying through the air or through space is just like the moving camera and moving objects you've already implemented in previous chapters of this book. Essentially, your shots (or bullets) will each consist of a model, a position, and a direction, and potentially some form of rotation as well. Seems like an easy enough concept. Let's see how you can add it to your game.

Your SpinningEnemy class already has all the components that a shot requires, so you can use that class for the shots as well as the enemy ships. Isn't it great to have a generic, multipurpose class that can be reused for different things? Think of all the time you just saved! You're already done coding your shot class...nice job!

The next step to creating a shot is to add the shot model to your project. If you haven't already, download the source code for this chapter of the book. With the source code for this chapter is a model file called ammo.x in the 3D Game\Content\Models folder. In Visual Studio, add the ammo.x file to your project by right-clicking the Content\Models folder in Solution Explorer, selecting Add → Existing Item..., browsing to the location of the ammo.x file, and selecting it.

Once you've added the ammo.x file to ...

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.