Firstly you need to create a new game component. Call it ProjectileManager. This will be the place where we will manage our bullets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProjectileManager : DrawableGameComponent |
You must then add a draw method to the game component just like the following.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public override void Draw(GameTime gameTime) | |
{ | |
base.Draw(gameTime); | |
} |
Next we will add a few variables and instantiate them in the constructor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static List<Bullet> bullets = new List<Bullet>(); | |
static Texture2D playerBulletTexture; | |
SpriteBatch spriteBatch; | |
public ProjectileManager(Game game, SpriteBatch spriteBatch) | |
: base(game) | |
{ | |
game.Components.Add(this); | |
playerBulletTexture = game.Content.Load<Texture2D>("Bullet"); | |
this.spriteBatch = spriteBatch; | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum BulletType { Player, Enemy } | |
public class Bullet | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public BulletType Type { get; set; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Bullet(Texture2D texture, Vector2 position, Vector2 direction, float speed, int activeTime, BulletType type) | |
{ | |
this.DrawTexture = texture; | |
this.Position = position; | |
this.Direction = direction; | |
this.Speed = speed; | |
this.ActiveTime = activeTime; | |
this.Type = type; | |
this.TotalActiveTime = 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void AddBullet(Vector2 position, Vector2 direction, float speed, int activeTime, BulletType type) | |
{ | |
bullets.Add(new Bullet(playerBulletTexture, position, direction, speed, activeTime, type)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(int i = 0; i < bullets.Count; i++) | |
{ | |
bullets[i].Update(gameTime); | |
if (bullets[i].TotalActiveTime > bullets[i].ActiveTime) | |
bullets.RemoveAt(i); | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foreach (Bullet b in bullets) | |
{ | |
b.Draw(spriteBatch); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Rocket(Texture2D texture, Vector2 position, Vector2 direction, float rotation, float Speed) | |
{ | |
this.DrawTexture = texture; | |
this.Position = position; | |
this.Direction = direction; | |
this.Rotation = rotation; | |
this.Speed = Speed; | |
this.IsShooting = false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Update(GameTime gameTime) | |
{ | |
this.Position += Direction * Speed; | |
if (IsShooting) | |
{ | |
shotTimer += gameTime.ElapsedGameTime.Milliseconds; | |
if (shotTimer > timeBetweenShots) | |
{ | |
shotTimer = 0; | |
ProjectileManager.AddBullet(this.Position, this.Direction, 12, 2000, BulletType.Player); | |
} | |
} | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rocket = new Rocket( | |
Content.Load<Texture2D>("rocket"), | |
new Vector2(200, 200), | |
Vector2.Zero, | |
0f, | |
0f); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ProjectileManager pj = new ProjectileManager(this, spriteBatch); |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spriteBatch.Draw( | |
DrawTexture, | |
Position, | |
null, | |
Color.White, | |
0f, | |
new Vector2( | |
DrawTexture.Width / 2, | |
DrawTexture.Height / 2), | |
1.0f, | |
SpriteEffects.None, | |
0.8f); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); |
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.
can i get the source code??
ReplyDeleteLink to the source should work now.
Delete