mLifetime = GUI_Slider(
	3.00,
      "Particle Lifetime",
      0.1,30.0);

mEmitSpeed = GUI_Slider(
	20.00,
      "Emitter Speed",
      0,1000);

mSpreadRadius = GUI_Slider(
	50.00,
      "Position Spread Radius",
      0,400);

mRadiusShrink = GUI_Slider(
	2.06,
      "Horizontal Shrink",
      0,3);

mMinSpeed = GUI_Slider(
   8.50,
      "Min Speed",
      0,500);

mMaxSpeed = GUI_Slider(
   17.00,
      "Max Speed",
      0,500);

mMinAngle = GUI_Slider(
   180.00,
      "Min Angle",
      0,360.0);

mMaxAngle = GUI_Slider(
   223.20,
      "Max Angle",
      0,360.0);

mMinScale = GUI_Slider(
	1.20,
      "Min Scale",
      0,3.0);

mMaxScale = GUI_Slider(
	1.50,
      "Max Scale",
      0,3.0);

mMaxAlpha = GUI_Slider(
	120.00,
      "Max Alpha",
      0,255);

mTextureName = GUI_PickTexture("fx/fnt_water_wave","Particle Texture");

mBlendMode = GUI_Select(
   kBlendAdditiveAlpha,
      "Blend Mode",
      {
         { description="Normal", value=kBlendNormal},
         { description="Additive", value=kBlendAdditiveAlpha}
      }
);

-- ------------------------------------------------------------
-- Globals

gActive = true;

PI = 3.1415926536;
TWO_PI = 2 * PI;

gMinAngle = mMinAngle * PI / 180;
gMaxAngle = mMaxAngle * PI / 180;

gMaxAlpha = mMaxAlpha / 255;


-- ------------------------------------------------------------
-- Define our particle characteristics

pTemp = Allocate(1) ; -- Allocate a Vec2 velocity member
pVelocity = Allocate(2) ; -- Allocate a Vec2 velocity member
pAge = Allocate(1) ; -- Allocate a TReal age member
pAlpha = Allocate(1);
pTempPosition = Allocate(2);


-- Set the particle texture
SetTexture(mTextureName);

-- Set the blend mode
SetBlendMode(mBlendMode);

-- LAST, we set the total number of particles in the particle
-- system. This doesn't 'create' the particles, but rather
-- allocates a block of 'dead' particles that can be created
-- below.
SetNumParticles(mEmitSpeed * (mLifetime + 1));
--SetNumParticles(2048);

-- ------------------------------------------------------------
-- Initialization rules.
-- 
-- The following rules define how particles are created.
--
-- Any parameter below in initialization or animation rules that 
-- is a scalar, Color(), or Vec2() can alternaly take a function 
-- that returns the same type--and most functions can take any type
-- and will return that same type. The exception is any function
-- that's expecting a scalar in certain parameter positions.

-- Define dLocus for FluidFX; in the game it's defined in 
-- swarm.cpp as a data source, and it gets the mouse position.
if not dLocus then
   dLocus = Vec2(0, 0);
end

if not dSystemAlpha then
   dSystemAlpha = 1;
end


--pPosition:Init(Vec2(fRange(-20, 20), fRange(-15, 15)) + dLocus);
pTemp:Init(fRange(0, 1));
pTempPosition:Init(f2dRotation(fRange(0, TWO_PI)) * pTemp * mSpreadRadius);
pPosition:Init(dLocus + Vec2(pTempPosition[1]*mRadiusShrink,pTempPosition[2]));

--pPosition:Init(dLocus +
--  f2dRotation(fRange(0, TWO_PI)) *
--  (f2dRotation(fRange(0, PI / 2))[1] + 1) / 2 * mSpreadRadiusH);

--pPosition:Init(dLocus + f2dRotation(fRange(0, TWO_PI)) * fRange(0, mSpreadRadiusH));

pVelocity:Init(f2dRotation(fRange(gMinAngle, gMaxAngle)) * fRange(mMinSpeed, mMaxSpeed));

pScale:Init(fRange(mMinScale, mMaxScale));

pAlpha:Init(0);
pColor:Init(Color(1, 1, 1, pAlpha * dSystemAlpha));

pAge:Init(0);

-- ------------------------------------------------------------
-- Particle Parameter Animation Functions
--
-- The following rules define how particles animate.
-- Each rule is applied on each frame of animation.

pPosition:Anim(pPosition + fTimeScale(pVelocity));

--pVelocity:Anim(pVelocity + fTimeScale(Vec2(0, mGravity)));

--pScale:Anim(pScale - fTimeScale(pStartScale / mLifetime));

pAlpha:Anim(fFade(pAge / mLifetime, 0, 250, 0.25, 250, 1, 250, 0.25, 250, 0) * gMaxAlpha);
pColor:Anim(fAlpha(pColor, pAlpha * dSystemAlpha));

pAge:Anim( pAge+fAge() );

-- ------------------------------------------------------------
-- General Anim functions
-- Expire particles over 700 ms old

Anim(fExpire(fGreater(pAge, mLifetime * 1000)));

-- ------------------------------------------------------------
-- Create initial particles (optional)
--CreateParticles( 10 );

-- ------------------------------------------------------------

-- A function to run as we're executing
-- seconds - how many seconds have elapsed

function Update(seconds)
   if gActive then
      CreateParticles(seconds * mEmitSpeed);
   end -- gActive
end
