Showing posts with label shooting. Show all posts
Showing posts with label shooting. Show all posts

Friday, January 25, 2013

XNA Tutorial:2D Shooting Part 2

A while ago I created a tutorial for shooting bullets out of a rocket in nay direction. The method used, while functional, is not very re-usable. In this tutorial we are going to re-factor the previous shooting tutorial. It is recommended that you finish this previous tutorial as we will be using that project.

Firstly you need to create a new game component. Call it ProjectileManager. This will be the place where we will manage our bullets. You need to ensure that instead of just inheriting from Microsoft.Xna.Framework.GameComponent change it to DrawableGameComponent as this will allow us to add a draw method to this game component.

You must then add a draw method to the game component just like the following. In here we will draw our bullets.

Next we will add a few variables and instantiate them in the constructor. As you can see, the list of bullets and the texture are both static. This is because we will be referencing them later is a static method. I'll explain further later.

Now we need to add an enum for bullet types. Enums are types that allow you to assign names to integer values. Add the following line to Bullet.cs file. Add is so that it is outside the bullet class. Next we will need to add a new property to the bullet class. This will be of type BulletType. We will need this if at some point in the future we create enemies that are capable of shooting back at the player. With this new property we will need to modify the constructor for the bullet class. We need to add a new parameter for the bullet type. Modify the constructor so it looks like this: Go back to the ProjectileManager. The next thing we need to do is add a public static method to add a new bullet to the bullet list. A public static method allows us to be able to access this method anywhere in the code. In this case by just typing ProjectileManager.AddBullet(methodParameters). Create a new method and make sure it looks like the following code. Next we need to be able to update the bullets in the projectile manager. We have pretty much already written this code in the rocket class all you really need to do is cut and paste the bullet update loop code over to the update loop of the projectile manager. Within the update for the projectile manager you should have to following code. Make sure you have also removed this code from the rocket class.

The exact same needs to be done for the drawing loop. Cut and paste the bullet drawing loop from the rocket class to the projectile manager draw method. The following code should now be in your draw method for the projectile manager. Before we continue make sure you no longer have code in the rocket class for updating and drawing bullets. You may also remove the list of bullets and bulletTex from the rocket class. You must also remove the bulletTex parameter from the rocket constructor and any other bulletTex code from the constructor. The constructor should now look like the following. next we need to use the new method we created in the ProjectileManager class. The rocket update method should now look like this: You may have notices that if you build your project you are still getting a few errors. We will work on fixing these next.

Go to the Game1 class. Go to the load content method to where we are instantiating the rocket. Remove the parameter for the bullet texture. It should now look like this. Next just above this line of code, still within the load content method, Create a new instance of the projectile manager. Now go down to the draw method in the Game1 class and move the spritebatch.end so that is appears after the base.Draw(). We do this to allow our projectile manager to draw with the same spritebatch. The base.Draw() updated the draw method of all the game components.

You should now be able to run the game. You may have noticed that the bullets are being drawn on top of the rocket. To fix this go back to your code. We must change the draw order. Go to the bullet class and to the draw method. change the last parameter to be less that 1.0f. I put it to 0.8f. Then go to the Game1 draw method and modify the spritebatch.begin method to look like the following. Now run the game and it should work perfectly.

Now the code is set up in such a way as to allow us to add enemies at some stage in the future. If you see any typos or mistakes please let me know.

Here is the source for the finished project.

Sunday, February 19, 2012

XNA Tutorial: 2D Shooting

In this tutorial we are going to build on what we did in the last one and make a shooting rocket. I have refactored the code we did the last tutorial into classes and methods. Before we start you are going to have to download the project file here.

After you have downloaded the project file and opened it the first thing to do is add a new class and call it Bullet. We are going to model this class on our rocket class. Add your using directives.
Now we need to add these properties.
Now lets create our constructor and the update method. It's relatively similar to the rocket constructor and update. The only thing that's missing is the rotation. The draw method will also be nearly identical to the draw method of the rocket class. The fact that the bullet class and the rocket class are so similar would mean that we could have used inheritance. Where we would create a base class let's say a "Sprite" class and have both the rocket and bullet classes inherit from the sprite class. Perhaps in a future tutorial we will do that but for now lest stick with this method.

Now that the boring part is complete let's think about how we will handle the bullets. We want the player to be able to shoot a bullet every time they press the left mouse button and have it continuously shoot if they have it pressed down.

Next we need to modify the rocket class the rocket class will manage the bullets. Add these at the top below the properties and before the constructor. Now modify the constructor so if looks like the following Next thing to modify is the update method. In here we want to check if IsShooting is true and if it is increase the shot timer. If the shot timer is greater than the time between shots then we create a new bullet and give it the the same position and direction as the rocket and add it to the list of bullets. After we do that were going to have to loop through the list of bullets and update each one. We also need to check if the ActiveTime(the max time it can be drawn for) is greater than the TotalActiveTime(the current amount of time the bullet had been drawn). If it is we remove it from the list. That's most of it done. Now all we need to do is draw them and check if the left mouse button has been clicked. To draw the bullets go to the draw method in the rocket class and add the following just above where your drawing your rocket. One more thing we need to change before checking for the mouse button press. Go to the Game1.cs file and up in the LoadContent method you will see where we instantiated the rocket. Modify it so it looks like this. Now Finally we can check for the mouse button press. Go to the update method of the Game1 class and you should see that we are already checking for our mousestate we will use this variable now. Just after the line that updates the rotation of the rocket at the following code. There we have it, press f5 and run the game to see what you have created. When you press and hold the left mouse button the rocket will shoot bullets in the direction of the mouse cursor. Here is a video of what you should see.

I hope you found it interesting. If you find a typo or a mistake anywhere or have any questions please leave a comment below.

Next go to part 2, where I show you how to improve this system.