mDuration = GUI_Slider(
   1.0,
      "Effect Duration",
      0.1,10.0);


mLifetime = GUI_Slider(
   1.50,
      "Particle Lifetime",
      0.1,3.0);

mEmitSpeed = GUI_Slider(
   50.00,
      "Emitter Speed",
      0,200);

mEmitRadius = GUI_Slider(
   120.00,
      "Emit Radius",
      0,500);

mMinSpeedY = GUI_Slider(
   -20.00,
      "Min Speed Y",
      -500,500);

mMaxSpeedY = GUI_Slider(
   -40.00,
      "Max Speed Y",
      -500,500);

mGravity = GUI_Slider(
   0.00,
      "Gravity",
      -500,500);

mMinStartScale = GUI_Slider(
   0.50,
      "Min Start Scale",
      0,3.0);

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

mMinStartAlpha = GUI_Slider(
   0.50,
      "Min Start Alpha",
      0,1.0);

mMaxStartAlpha = GUI_Slider(
   0.75,
      "Max Start Alpha",
      0,1.0);

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

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

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

PI = 3.1415926536;

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

pTemp = Allocate(1);
pTemp2 = Allocate(1);
pVelocity = Allocate(2) ; -- Allocate a Vec2 velocity member
pAge = Allocate(1) ; -- Allocate a TReal age member
pAlpha = Allocate(1);
pStartAlpha = Allocate(1);
pStartScale = Allocate(1);
pSpin = Allocate(1);
pSpinSpeed = Allocate(1);


-- 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(mMaxParticles);

-- ------------------------------------------------------------
-- External params.

if not dLocus then
   dLocus = Vec2(0, 0);
end

if not dStartColor then
   dStartColor = Color(0, 0, 1, 0.25);
end

-- ------------------------------------------------------------
-- Initialization

pTemp:Init(fRange(0, 1));
pTemp2:Init((pTemp - 1) * (pTemp - 1) - 1); -- to get the uniform circle distribution
pPosition:Init(f2dRadius(pTemp2 * mEmitRadius) + dLocus);
pVelocity:Init(fRange(Vec2(0, mMinSpeedY), Vec2(0, mMaxSpeedY)));

pStartScale:Init(fRange(mMinStartScale, mMaxStartScale));
pScale:Init(pStartScale);

pSpin:Init(fRange(0, 2 * PI));
pSpinSpeed:Init(fRange(-PI, PI));

pStartAlpha:Init(fRange(mMinStartAlpha, mMaxStartAlpha))
pColor:Init(fPick(
   Color(1.0, 0.5, 0.5, pStartAlpha),
   Color(0.5, 1.0, 0.5, pStartAlpha),
   Color(0.5, 0.5, 1.0, pStartAlpha)));

pAge:Init(0);

-- ------------------------------------------------------------
-- Animation

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

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

pSpin:Anim(pSpin + fTimeScale(pSpinSpeed));
pUp:Anim(f2dRotation(pSpin));

pColor:Anim(fAlpha(pColor, pColor[4] - fTimeScale(pStartAlpha / mLifetime)));

pAge:Anim( pAge+fAge() );
Anim(fExpire(fGreater(pAge, mLifetime * 1000)));

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

timeout = NewTimeout(mDuration);

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