Time for action - the Particle class

  1. Add a new class file to the Robot Rampage project called Particle.cs.
  2. Add the following using directives to the Particle class:
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    
  3. Update the class declaration for the Particle class to derive it from the Sprite class:
    class Particle : Sprite
    
  4. Add declarations to the Particle class:
    #region Declarations
    private Vector2 acceleration;
    private float maxSpeed;
    private int initialDuration;
    private int remainingDuration;
    private Color initialColor;
    private Color finalColor;
    #endregion
    
  5. Add properties to the Particle class:
    #region Properties
    public int ElapsedDuration
    {
    get
    {
    return initialDuration - remainingDuration;
    }
    }
    public float DurationProgress
    {
    get
    {
    return (float)ElapsedDuration /
    (float)initialDuration;
    }
    }
    public bool IsActive
    {
    get
    {
    return (remainingDuration > 0);
    }
    }
    #endregion
    
  6. Add a constructor to the Particle class:
    #region Constructor
    public Particle(
    Vector2 location,
    Texture2D texture,
    Rectangle initialFrame,
    Vector2 velocity,
    Vector2 acceleration,
    float maxSpeed,
    int duration,
    Color initialColor,
    Color finalColor)
    : base(location, texture, initialFrame, velocity)
    {
    initialDuration = duration;
    remainingDuration = duration;
    this.acceleration = acceleration;
    this.initialColor = initialColor;
    this.maxSpeed = maxSpeed;
    this.finalColor = finalColor;
    }
    #endregion
    
  7. Add the Update() and Draw() methods to the Particle class:
    #region Update and Draw
    public override void Update(GameTime gameTime)
    {
    if (remainingDuration <= 0)
    {
    Expired = true;
    }
    if (!Expired)
    {
    Velocity += acceleration;
    if (Velocity.Length() > maxSpeed)
    {
    Vector2 vel = Velocity;
    vel.Normalize();
    Velocity = vel * maxSpeed;
    }
    TintColor = Color.Lerp(
    initialColor,
    finalColor,
    DurationProgress);
    remainingDuration--;
    }
    base.Update(gameTime);
    }
    public override void Draw(SpriteBatch spriteBatch)
    {
    if (IsActive)
    {
    base.Draw(spriteBatch);
    }
    }
    #endregion
    

What just happened?

Just as we did in Asteroid Belt Assault, we have constructed our Particle class as an extension of the Sprite class. Just as before, we will use the Particle class to generate large volumes of sprites that will need to automatically expire after a given period of time.

The EffectsManager class

In Asteroid Belt Assault, we used a class called ExplosionManager to handle all of the game's explosive effects. We will create a similar class for Robot Rampage, but call it EffectsManager since it will handle our sparks effect as well as explosions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.153.38