-- Define our particle characteristics

pVelocity = Allocate(2) ; -- Allocate a Vec2 velocity member
pAge = Allocate(1) ; -- Allocate a TReal age member
pSpin = Allocate(1); -- Allocate a TReal spin member
pSpinSpeed = Allocate(1); -- Allocate a TReal spin member

SetTexture("spark_welding.png");
SetBlendMode(kBlendAdditiveAlpha);
SetNumParticles(700);

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

-- Pick a position randomly, within a range of values.
-- dLocus is defined in swarm.cpp and added to the random
-- range
pPosition:Init( fRange( Vec2(-3,-15), Vec2(3,15) )+dLocus );

-- Pick a velocity within a range
pVelocity:Init( fRange( Vec2(-120,-130), Vec2(120,170) ) );

-- Start scale at 0.5
pScale:Init( 0.3 );

-- Start age as 0 milliseconds
pAge:Init(0);

-- Start initial rotation as a random angle
--pSpin:Init( fRange( 0, 2*3.1415927 ) );

-- Start spin velocity random from -10 to 10
--pSpinSpeed:Init( fRange( -3, 3 ) );

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

-- Add velocity scaled by time to position
pPosition:Anim( pPosition + fTimeScale(pVelocity) );

-- Add 1/second to scale
pScale:Anim( pScale - 0.003 );

-- Add gravity scaled by time to velocity
pVelocity:Anim( pVelocity + fTimeScale(Vec2(0,500)) );

-- Fade (using age) from white to yellow to black. Assumes
-- the first parameter is a number of elapsed milliseconds,
-- though can work to linearly interpolate using first 
-- scalar parameter between any value types.
pColor:Anim( 
   fFade( pAge,
      Color(1,1,1,1),300,
      Color(255/255,228/255,0/255,0.8),100,
      Color(255/255,46/255,12/255,0.4)) -- to final color
   );

-- Spin by pSpinSpeed scaled by time
--pSpin:Anim( pSpin + fTimeScale( pSpinSpeed ) );

-- Set the particle's "Up" vector to the current user variable
-- "pSpin"
--pUp:Anim( f2dRotation( pSpin ) );

-- Age the particle using age function
pAge:Anim( pAge+fAge() );

-- General Anim functions
-- Expire particles over 700 ms old
Anim( fExpire( fGreater(pAge,fRange(400,900)) ) );

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

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

-- A function to run as we're executing
-- seconds - how many seconds have elapsed
function Update(seconds)
   CreateParticles( seconds * 300 );
end
