A Better Particle Effects Object
One year and one day ago, I wrote the widely popular article A Simple Particle Effects Object. Since that time, I've used this object in two different games and have modified it to be slightly better.
The improvements to this object include:
- Particles fade to transparent.
- Easier to implement different types of particles.
- Ability to spread particles out rather than having them all generate from the exact same point (SpreadXDistance, SpreadYDistance).
In order to use this object, you will need to add Particle and Particles classes to your project. Particle is an individual particle and all associated properties / methods. Particles is a collection of Particle objects along with methods to create, draw, update, and fire your Particle objects.
I have included one example particle in the Particle class (Orange)... you can create new ones by adding a value to the ParticleType enum and then setting the properties for that ParticleType in the Particle constructor.
public class Particle
{
private Texture2D particleTexture;
public enum ParticleType
{
Orange = 1
}
public Vector2 Position
{
get;
set;
}
public Vector2 Direction
{
get;
set;
}
public Vector2 Speed
{
get;
set;
}
public int SpreadXDistance
{
get;
set;
}
public int SpreadYDistance
{
get;
set;
}
public ParticleType currentParticleType
{
get;
set;
}
// The most opaque a particle can be.
public float MaxAlphaAmount
{
get;
set;
}
public int Width
{
get;
set;
}
public int Height
{
get;
set;
}
public bool Active
{
get;
set;
}
public float ParticleTimerStartValue
{
get;
set;
}
public Particle(ParticleType initialParticleType, ContentManager contentManager)
{
currentParticleType = initialParticleType;
this.SpreadXDistance = 0;
this.SpreadYDistance = 0;
this.Active = false;
switch (currentParticleType)
{
case ParticleType.Orange:
{
Speed = new Vector2(350, 350);
Direction = new Vector2(0, 0);
particleTexture = contentManager.Load<Texture2D>("Particles\\Orange");
this.ParticleTimerStartValue = .5f;
this.SpreadXDistance = 15;
This article has been view 2847 times.
|