/*
* DirectionalEmitter.cs
* Authors: Brian Murphy
* Copyright (c) 2007-2008 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Random=Util.Random;
namespace TACParticleEngine.Emitters
{
///
/// This class is an emitter that performs a "directed burst" effect from a single emitter point with a given angle of range
///
public class DirectionalEmitter : Emitter
{
#region Fields
// Temporary vector for determining where a particle moves in two-dimensions
private Vector3 mTempVel;
// Temporary int used for storing how many particles were created every cycle
private int mTempAmount;
// Temporary integer for use in iterating over the particle heap
private int mTempIndex;
// Current index of the next available spot in the head for adding a particle to
private int mCurrentIndex;
// Float used for determining
private float mTotalElapsedTime;
#endregion
#region Form Fields
// Current position of the emitter
private Vector3 mEmitterPosition;
// Vector range used for determining the general direction of all particles
private Vector3 mMinVel, mMaxVel;
#endregion
#region Properties
///
/// Sets the direction of the emitter
///
public override Vector3 Direction
{
get { return mMinVel; }
set
{
mMinVel = value;
mMaxVel = value;
}
}
#endregion
#region Form Properties
///
/// Gets/Sets the current position of the emitter
///
public override Vector3 EmitterPosition
{
get { return mEmitterPosition; }
set { mEmitterPosition = value; }
}
///
/// Gets/Sets the minimum velocity direction
///
public Vector3 MinVel
{
get { return mMinVel; }
set { mMinVel = value; }
}
///
/// Gets/Sets the maximum velocity direction
///
public Vector3 MaxVel
{
get { return mMaxVel; }
set { mMaxVel = value; }
}
#endregion
#region Creation
///
/// Default constructor for use in the Particle Editor
///
public DirectionalEmitter()
: base(SpriteBlendMode.AlphaBlend, "", 100, 1000, -1, 1, Vector2.One, Vector2.One, Vector2.One,
new Vector2(500, 500), 0, 0, 0, 0, Vector4.One, Vector4.One, Vector4.Zero, Vector4.Zero)
{
mCurrentIndex = 0;
mEmitterPosition = Vector3.Zero;
mTotalElapsedTime = 0;
mMinVel = Vector3.Zero;
mMaxVel = Vector3.Zero;
}
///
/// Constructor
///
/// Initial position to start at
/// Minimum values of the directional range at which particles can move
/// Maximum values of the directional range at which particles can move
/// SpriteBlendMode to use
/// Name of the texture to draw with
/// How many particles per second are made
/// The max amount of particles available at one time
/// How long the emitter stays active for
/// How long the patrticle stays active for
/// How small a particle can start
/// How large a particle can start
/// How small a particle can end
/// How large a particle can end
/// How slow a particle can start
/// How fast a particle can start
/// How slow a particle can end
/// How fast a particle can end
/// Minimum values for color to start with
/// Maximum values for color to start with
/// Minimum values for color to end with
/// Maximum values for color to end with
public DirectionalEmitter(Vector3 position, Vector3 minVel, Vector3 maxVel, SpriteBlendMode blendEffect,
string textureName, float particlesPerSec, int particleAmount, float emitLifetime,
float particleLifetime, Vector2 minStartSize, Vector2 maxStartSize, Vector2 minEndSize,
Vector2 maxEndSize, float minStartSpeed, float maxStartSpeed, float minEndSpeed, float maxEndSpeed,
Vector4 minStartColor, Vector4 maxStartColor, Vector4 minEndColor, Vector4 maxEndColor)
: base(blendEffect, textureName, particlesPerSec, particleAmount, emitLifetime, particleLifetime,
minStartSize, maxStartSize, minEndSize, maxEndSize, minStartSpeed, maxStartSpeed, minEndSpeed,
maxEndSpeed, minStartColor, maxStartColor, minEndColor, maxEndColor)
{
mCurrentIndex = 0;
mEmitterPosition = position;
mTotalElapsedTime = 0;
mMinVel = minVel;
mMaxVel = maxVel;
}
///
/// Returns a copy of the directional emitter
///
/// The cloned copy of this directional emitter
public override object Clone()
{
DirectionalEmitter retVal = new DirectionalEmitter(Vector3.Zero, mMinVel, mMaxVel, base.mBlendEffect,
base.mTextureName, base.mParticlesPerSec, base.mParticleAmount,
base.mEmitLifetime, base.mParticleLifetime, base.mMinStartSize, base.mMaxStartSize,
base.mMinEndSize, base.mMaxEndSize, base.mMinStartSpeed, base.mMaxStartSpeed,
base.mMinEndSpeed, base.mMaxEndSpeed, base.mMinStartColor, base.mMaxStartColor,
base.mMinEndColor, base.mMaxEndColor);
retVal.mTexture = mTexture;
return retVal;
}
#endregion
#region Update
///
/// Updates directional emitter
///
/// The time elapsed since last tick in milliseconds
public override void Update(float elapsedTime)
{
if (base.mParticles != null)
{
base.Update(elapsedTime);
if (base.Alive && base.IsActive)
{
mTotalElapsedTime += elapsedTime;
mTempAmount = (int)(mTotalElapsedTime * base.mParticlesPerSec);
if (mTempAmount > 0)
{
mTempIndex = (mCurrentIndex + mTempAmount) % base.mParticles.Length;
if (mCurrentIndex + mTempAmount < base.mParticles.Length)
{
for (; mCurrentIndex < mTempIndex; mCurrentIndex++)
{
CalcDirection();
base.InitializeParticle(mEmitterPosition, mTempVel, mCurrentIndex);
}
}
else
{
for (; mCurrentIndex < base.mParticles.Length; mCurrentIndex++)
{
CalcDirection();
base.InitializeParticle(mEmitterPosition, mTempVel, mCurrentIndex);
}
mCurrentIndex = 0;
for (; mCurrentIndex < mTempIndex; mCurrentIndex++)
{
CalcDirection();
base.InitializeParticle(mEmitterPosition, mTempVel, mCurrentIndex);
}
}
mTotalElapsedTime = 0;
}
}
}
}
#endregion
#region Util
///
/// Determines a random direction for the next particle to go in
///
private void CalcDirection()
{
if (mRandomDir)
{
double tempXRot = Random.NextFloat(MathHelper.TwoPi);
double tempYRot = Random.NextFloat(MathHelper.TwoPi);
mTempVel.X = (float)Math.Cos(tempYRot);
mTempVel.Y = (float)Math.Sin(tempXRot);
mTempVel.Z = (float)(Math.Sqrt(Math.Pow(Math.Sin(tempYRot), 2.0) + Math.Pow(Math.Cos(tempXRot), 2.0)));
}
else
{
mTempVel = Random.RandomVector3(mMinVel, mMaxVel);
mTempVel.Normalize();
}
}
#endregion
}
}