/* * PointEmitter.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 "burst" effect from a single emitter point /// public class PointEmitter : Emitter { #region Fields // Temporary vector used in setting the velocity of the next particle 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; #endregion #region Properties /// /// Sets the direction of the emitter, does nothing for this emitter /// public override Vector3 Direction { get { return Vector3.Zero; } set { } } #endregion #region Form Properties /// /// Gets/Sets the current position of the emitter /// public override Vector3 EmitterPosition { get { return mEmitterPosition; } set { mEmitterPosition = value; } } #endregion #region Creation /// /// Default constructor for use in the Particle Editor /// public PointEmitter() : 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; } /// /// Constructor /// /// Initial position to start at /// 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 PointEmitter(Vector3 position, 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; } /// /// Returns a copy of the point emitter /// /// A cloned copy of this point emitter public override object Clone() { PointEmitter retVal = new PointEmitter(Vector3.Zero, 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 point 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 the next particle will go in /// private void CalcDirection() { mTempVel.X = Random.NextFloat(-1, 1); mTempVel.Y = Random.NextFloat(-1, 1); mTempVel.Z = Random.NextFloat(-1, 1); mTempVel.Normalize(); } #endregion } }