move to github
This commit is contained in:
46
SpacePew/ParticleSystem/ExplosionParticleSystem.cs
Normal file
46
SpacePew/ParticleSystem/ExplosionParticleSystem.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using SpacePew.Camera;
|
||||
|
||||
namespace SpacePew.ParticleSystem
|
||||
{
|
||||
public class ExplosionParticleSystem : ParticleSystem
|
||||
{
|
||||
public ExplosionParticleSystem(MainGame game, int howManyEffects, ICamera2D camera) : base(game, howManyEffects, camera) { }
|
||||
|
||||
protected override void InitializeConstants()
|
||||
{
|
||||
_textureFilename = "ParticleTextures\\explosion";
|
||||
|
||||
_minInitialSpeed = 40;
|
||||
_maxInitialSpeed = 500;
|
||||
|
||||
_minAcceleration = 0;
|
||||
_maxAcceleration = 0;
|
||||
|
||||
_minLifetime = .5f;
|
||||
_maxLifetime = 1.0f;
|
||||
|
||||
_minScale = .3f;
|
||||
_maxScale = 1.0f;
|
||||
|
||||
_minNumParticles = 20;
|
||||
_maxNumParticles = 25;
|
||||
|
||||
_minRotationSpeed = -MathHelper.PiOver4;
|
||||
_maxRotationSpeed = MathHelper.PiOver4;
|
||||
|
||||
_blendState = BlendState.Additive;
|
||||
|
||||
DrawOrder = AdditiveDrawOrder;
|
||||
}
|
||||
|
||||
protected override void InitializeParticle(Particle p, Vector2 where)
|
||||
{
|
||||
base.InitializeParticle(p, where);
|
||||
|
||||
p.Acceleration = -p.Velocity / p.Lifetime;
|
||||
}
|
||||
}
|
||||
}
|
||||
41
SpacePew/ParticleSystem/ExplosionSmokeParticleSystem.cs
Normal file
41
SpacePew/ParticleSystem/ExplosionSmokeParticleSystem.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using SpacePew.Camera;
|
||||
|
||||
namespace SpacePew.ParticleSystem
|
||||
{
|
||||
public class ExplosionSmokeParticleSystem : ParticleSystem
|
||||
{
|
||||
public ExplosionSmokeParticleSystem(MainGame game, int howManyEffects, ICamera2D camera)
|
||||
: base(game, howManyEffects, camera)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void InitializeConstants()
|
||||
{
|
||||
_textureFilename = "ParticleTextures\\smoke";
|
||||
|
||||
_minInitialSpeed = 20;
|
||||
_maxInitialSpeed = 200;
|
||||
|
||||
_minAcceleration = -10;
|
||||
_maxAcceleration = -50;
|
||||
|
||||
_minLifetime = 1.0f;
|
||||
_maxLifetime = 2.5f;
|
||||
|
||||
_minScale = 1.0f;
|
||||
_maxScale = 2.0f;
|
||||
|
||||
_minNumParticles = 10;
|
||||
_maxNumParticles = 20;
|
||||
|
||||
_minRotationSpeed = -MathHelper.PiOver4;
|
||||
_maxRotationSpeed = MathHelper.PiOver4;
|
||||
|
||||
_blendState = BlendState.AlphaBlend;
|
||||
|
||||
DrawOrder = AlphaBlendDrawOrder;
|
||||
}
|
||||
}
|
||||
}
|
||||
55
SpacePew/ParticleSystem/Particle.cs
Normal file
55
SpacePew/ParticleSystem/Particle.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace SpacePew.ParticleSystem
|
||||
{
|
||||
public class Particle
|
||||
{
|
||||
private static readonly Random _random = new Random();
|
||||
private float RandomBetween(float min, float max)
|
||||
{
|
||||
return min + (float)_random.NextDouble() * (max - min);
|
||||
}
|
||||
|
||||
public Vector2 Position;
|
||||
public Vector2 Velocity;
|
||||
public Vector2 Acceleration;
|
||||
|
||||
public float Scale { get; set; }
|
||||
|
||||
public float Lifetime { get; private set; }
|
||||
public float TimeSinceStart { get; private set; }
|
||||
public float Rotation { get; private set; }
|
||||
public float RotationSpeed { get; private set; }
|
||||
|
||||
public bool Active
|
||||
{
|
||||
get { return TimeSinceStart < Lifetime; }
|
||||
}
|
||||
|
||||
public void Initialize(Vector2 position, Vector2 velocity, Vector2 acceleration, float lifetime, float scale, float rotationSpeed)
|
||||
{
|
||||
this.Position = position;
|
||||
this.Velocity = velocity;// *this.Scale;
|
||||
this.Acceleration = acceleration;
|
||||
this.Lifetime = lifetime * this.Scale;
|
||||
this.Scale = scale * this.Scale;
|
||||
this.RotationSpeed = rotationSpeed;
|
||||
|
||||
this.TimeSinceStart = 0.0f;
|
||||
|
||||
this.Rotation = RandomBetween(0, MathHelper.TwoPi);
|
||||
}
|
||||
|
||||
public void Update(float dt)
|
||||
{
|
||||
Velocity += Acceleration * dt;
|
||||
Position += Velocity * dt;
|
||||
|
||||
Rotation += RotationSpeed * dt;
|
||||
|
||||
TimeSinceStart += dt;
|
||||
}
|
||||
}
|
||||
}
|
||||
194
SpacePew/ParticleSystem/ParticleSystem.cs
Normal file
194
SpacePew/ParticleSystem/ParticleSystem.cs
Normal file
@@ -0,0 +1,194 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework;
|
||||
using SpacePew.Camera;
|
||||
|
||||
namespace SpacePew.ParticleSystem
|
||||
{
|
||||
public abstract class ParticleSystem : DrawableGameComponent
|
||||
{
|
||||
private static readonly Random _random = new Random();
|
||||
protected Random Random
|
||||
{
|
||||
get { return _random; }
|
||||
}
|
||||
|
||||
protected float RandomBetween(float min, float max)
|
||||
{
|
||||
return min + (float)_random.NextDouble() * (max - min);
|
||||
}
|
||||
|
||||
public const int AlphaBlendDrawOrder = 100;
|
||||
public const int AdditiveDrawOrder = 200;
|
||||
|
||||
private readonly MainGame _game;
|
||||
private readonly int _howManyEffects;
|
||||
|
||||
private Texture2D _texture;
|
||||
private Vector2 _origin;
|
||||
|
||||
private Particle[] _particles;
|
||||
|
||||
private Queue<Particle> _freeParticles;
|
||||
|
||||
public int FreeParticleCount
|
||||
{
|
||||
get { return _freeParticles.Count; }
|
||||
}
|
||||
|
||||
protected int _minNumParticles;
|
||||
protected int _maxNumParticles;
|
||||
|
||||
protected string _textureFilename;
|
||||
|
||||
protected float _minInitialSpeed;
|
||||
protected float _maxInitialSpeed;
|
||||
|
||||
protected float _minAcceleration;
|
||||
protected float _maxAcceleration;
|
||||
|
||||
protected float _minRotationSpeed;
|
||||
protected float _maxRotationSpeed;
|
||||
|
||||
protected float _minLifetime;
|
||||
protected float _maxLifetime;
|
||||
|
||||
protected float _minScale;
|
||||
protected float _maxScale;
|
||||
|
||||
protected BlendState _blendState;
|
||||
|
||||
protected ICamera2D _camera;
|
||||
|
||||
protected ParticleSystem(MainGame game, int howManyEffects, ICamera2D camera)
|
||||
: base(game)
|
||||
{
|
||||
this._game = game;
|
||||
this._howManyEffects = howManyEffects;
|
||||
this._camera = camera;
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
InitializeConstants();
|
||||
|
||||
_particles = new Particle[_howManyEffects * _maxNumParticles];
|
||||
_freeParticles = new Queue<Particle>(_howManyEffects * _maxNumParticles);
|
||||
|
||||
for (int i = 0; i < _particles.Length; i++)
|
||||
{
|
||||
_particles[i] = new Particle();
|
||||
_freeParticles.Enqueue(_particles[i]);
|
||||
}
|
||||
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
protected abstract void InitializeConstants();
|
||||
|
||||
protected override void LoadContent()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_textureFilename))
|
||||
{
|
||||
throw new ArgumentNullException("textureFilename");
|
||||
}
|
||||
|
||||
_texture = _game.Content.Load<Texture2D>(_textureFilename);
|
||||
|
||||
_origin.X = _texture.Width / 2;
|
||||
_origin.Y = _texture.Height / 2;
|
||||
|
||||
base.LoadContent();
|
||||
}
|
||||
|
||||
public void AddParticles(Vector2 where, float scale)
|
||||
{
|
||||
int numParticles = Random.Next(_minNumParticles, _maxNumParticles);
|
||||
|
||||
for (int i = 0; i < numParticles && _freeParticles.Count > 0; i++)
|
||||
{
|
||||
Particle p = _freeParticles.Dequeue();
|
||||
p.Scale = scale;
|
||||
InitializeParticle(p, where);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void InitializeParticle(Particle p, Vector2 where)
|
||||
{
|
||||
Vector2 direction = PickRandomDirection();
|
||||
|
||||
float velocity =
|
||||
RandomBetween(_minInitialSpeed, _maxInitialSpeed);
|
||||
float acceleration =
|
||||
RandomBetween(_minAcceleration, _maxAcceleration);
|
||||
float lifetime =
|
||||
RandomBetween(_minLifetime, _maxLifetime);
|
||||
float scale =
|
||||
RandomBetween(_minScale, _maxScale);
|
||||
float rotationSpeed =
|
||||
RandomBetween(_minRotationSpeed, _maxRotationSpeed);
|
||||
|
||||
p.Initialize(
|
||||
where, velocity * direction, acceleration * direction,
|
||||
lifetime, scale, rotationSpeed);
|
||||
}
|
||||
|
||||
protected virtual Vector2 PickRandomDirection()
|
||||
{
|
||||
float angle = RandomBetween(0, MathHelper.TwoPi);
|
||||
return new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||
|
||||
foreach (var p in _particles.Where(p => p.Active))
|
||||
{
|
||||
p.Update(dt);
|
||||
|
||||
if (!p.Active)
|
||||
{
|
||||
_freeParticles.Enqueue(p);
|
||||
}
|
||||
}
|
||||
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
public override void Draw(GameTime gameTime)
|
||||
{
|
||||
_game.SpriteBatch.Begin(SpriteSortMode.Deferred, _blendState, null, null, null, null, _camera.Transform);
|
||||
|
||||
foreach (Particle p in _particles)
|
||||
{
|
||||
if (!p.Active)
|
||||
continue;
|
||||
|
||||
float normalizedLifetime = p.TimeSinceStart / p.Lifetime;
|
||||
|
||||
float alpha = 4 * normalizedLifetime * (1 - normalizedLifetime);
|
||||
var color = new Color(new Vector4(1, 1, 1, alpha));
|
||||
|
||||
float scale = p.Scale * (.75f + .25f * normalizedLifetime);
|
||||
|
||||
_game.SpriteBatch.Draw(_texture,
|
||||
p.Position,
|
||||
null,
|
||||
color,
|
||||
p.Rotation,
|
||||
_origin,
|
||||
scale,
|
||||
SpriteEffects.None,
|
||||
0.0f);
|
||||
}
|
||||
|
||||
_game.SpriteBatch.End();
|
||||
|
||||
base.Draw(gameTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
61
SpacePew/ParticleSystem/SmokePlumeParticleSystem.cs
Normal file
61
SpacePew/ParticleSystem/SmokePlumeParticleSystem.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using SpacePew.Camera;
|
||||
|
||||
namespace SpacePew.ParticleSystem
|
||||
{
|
||||
public class SmokePlumeParticleSystem : ParticleSystem
|
||||
{
|
||||
public SmokePlumeParticleSystem(MainGame game, int howManyEffects, ICamera2D camera)
|
||||
: base(game, howManyEffects, camera)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void InitializeConstants()
|
||||
{
|
||||
_textureFilename = "ParticleTextures\\smoke";
|
||||
|
||||
_minInitialSpeed = 20;
|
||||
_maxInitialSpeed = 100;
|
||||
|
||||
_minAcceleration = 0;
|
||||
_maxAcceleration = 0;
|
||||
|
||||
_minLifetime = 5.0f;
|
||||
_maxLifetime = 7.0f;
|
||||
|
||||
_minScale = .5f;
|
||||
_maxScale = 1.0f;
|
||||
|
||||
_minNumParticles = 7;
|
||||
_maxNumParticles = 15;
|
||||
|
||||
_minRotationSpeed = -MathHelper.PiOver4 / 2.0f;
|
||||
_maxRotationSpeed = MathHelper.PiOver4 / 2.0f;
|
||||
|
||||
_blendState = BlendState.AlphaBlend;
|
||||
|
||||
DrawOrder = AlphaBlendDrawOrder;
|
||||
}
|
||||
|
||||
protected override Vector2 PickRandomDirection()
|
||||
{
|
||||
float radians = RandomBetween(
|
||||
MathHelper.ToRadians(80), MathHelper.ToRadians(100));
|
||||
|
||||
Vector2 direction = Vector2.Zero;
|
||||
|
||||
direction.X = (float)Math.Cos(radians);
|
||||
direction.Y = -(float)Math.Sin(radians);
|
||||
return direction;
|
||||
}
|
||||
|
||||
protected override void InitializeParticle(Particle p, Vector2 where)
|
||||
{
|
||||
base.InitializeParticle(p, where);
|
||||
|
||||
p.Acceleration.X += RandomBetween(10, 50);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user