mLifetime = GUI_Slider(
   1.00,
      "Particle Lifetime",
      0.1,5.0);

mEmitInterval = GUI_Slider(
   1.20,
      "Emit Interval",
      0,10);

mOffsetX = GUI_Slider(
   0.00,
      "Offset X",
      -100,100);

mOffsetY = GUI_Slider(
   0.00,
      "Offset Y",
      -100,100);

mAppearZoneWidth = GUI_Slider(
   50.00,
      "Appear Zone Width",
      0,200);

mAppearZoneHeight = GUI_Slider(
   50.00,
      "Appear Zone Height",
      0,200);

mMaxSpinSpeed = GUI_Slider(
   1.00,
      "Max Spin Speed (rotations per second)",
      0,3);

mScale = GUI_Graph(
   {
      0.00,125,
      1.00,750,
      1.00,125,
      0.00
   },
   0.00,
   1.00,
   {
      {0.00,time=0.00},
      {1.00,time=125.00},
      {1.00,time=875.00},
      {0.00,time=1000.00}
   },
   "Scale");

mAlpha = GUI_Graph(
   {
      0.00,125,
      1.00,750,
      1.00,125,
      0.00
   },
   0.00,
   1.00,
   {
      {0.00,time=0.00},
      {1.00,time=125.00},
      {1.00,time=875.00},
      {0.00,time=1000.00}
   },
   "Alpha");

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

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

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

gActive = true;
gStop = 0;

PI = 3.1415926536;

mZoneLeft   = mOffsetX - mAppearZoneWidth / 2;
mZoneRight  = mOffsetX + mAppearZoneWidth / 2;
mZoneTop    = mOffsetY - mAppearZoneHeight / 2;
mZoneBottom = mOffsetY + mAppearZoneHeight / 2;

mMaxSpinSpeedRad = mMaxSpinSpeed * 2 * PI;

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

pVelocity = Allocate(2);
pAge = 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(4);

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

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

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

pPosition:Init(dLocus +
  Vec2(fRange(mZoneLeft, mZoneRight), fRange(mZoneTop, mZoneBottom)));

pScale:Init(0);

pSpin:Init(fRange(-PI, PI));
pSpinSpeed:Init(fRange(-mMaxSpinSpeedRad, mMaxSpinSpeedRad));

pColor:Init(Color(1, 1, 1, 0));

pAge:Init(0);

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

pScale:Anim(fFade(pAge / mLifetime, toparams(mScale)));

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

pColor:Anim(fAlpha(pColor, fFade(pAge / mLifetime, toparams(mAlpha))));

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 = 0;

function Update(seconds)
   if gActive then
      timeout = timeout - seconds;
      if timeout <= 0 then
         CreateParticles(1);
         timeout = mEmitInterval;
      end
   else
      timeout = 0;
   end -- gActive
end
