-- ------------------------------------------------------------
-- Initialization phase

-- Define our custom particle properties
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);
pPos = Allocate(2);
pScal = Allocate(1);
pCol = Allocate(4);
-- dLocus is a data source: An external connection to the particle
-- system. Here we define dLocus for FluidFX, since FluidFX doesn't know
-- to provide the data source; in the game it's defined in 
-- swarm.cpp as a data source, and it gets the mouse position.
if not dPos then
   dPos= Vec2(0,0) ;
end
if not dScale then
   dScale = 1;
end
if not dColor then
   dColor = Color(1.0,1.0,0.0,1);
end

-- Set the particle texture
SetTexture("fireworks");

-- Set the blend mode
SetBlendMode(kBlendAdditiveAlpha);

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

-- ------------------------------------------------------------
-- Action Phase

-- Particle 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.

-- Pick one of two positions randomly, and add dPos.
-- dPos is defined in swarm.cpp and added to the random
-- range

-- Start initial rotation as a random angle
pSpin:Init( fRange( -0.3, -2.84) );
pSpinSpeed:Init( fRange( -0.1,0.1) ); 

pPos:Init( dPos + f2dRotation(  pSpin ) * fRange(45,55) * dScale );

-- Pick a velocity from a range
pVelocity:Init( f2dRotation( pSpin) * 10 );

-- Start color (tint) off as white (natural color of image)
pCol:Init( fRange(dColor, Color(1,1,1,1) ) );

-- Start scale out as 0.5
pScal:Init( dScale );

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

pUp:Init( f2dRotation( pSpin ) );

-- Start spin velocity random from -10 to 10

-- ------------------------------------------------------------
-- 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
--pVelocity:Anim( pVelocity + fTimeScale(Vec2(0,10)) );
pColor:Anim( fFade( pAge,Color(0,0,0,0),500,pCol,3500,pCol, 4500, Color(0.1,0.1,0.1,0.1) ) );
pSpin:Anim( pSpin + fTimeScale( pSpinSpeed ) );
pPos:Anim( dPos + f2dRotation(  pSpin ) * fRange(50,55) * dScale);
pUp:Anim(  f2dRotation( pSpin)  );
pPosition:Anim( fFade(pAge, pPos, 1000, pPos+pVelocity, 2000, pPos, 3000, pPos+pVelocity, 4000, pPos));
pScale:Anim( fFade(pAge, pScal, 1000, pScal*1.2, 2000, pScal, 3000, pScal*1.2, 4000, pScal) );

-- Add time to the age
pAge:Anim( pAge+fAge() );

-- Expire particles over 1500 ms old
Anim( fExpire( fGreater(pAge,4600) ) );

-- Create an initial particle (optional)
-- CreateParticles( 1 );

-- A global variable that we can set from outside
gActive = false;

-- A function to run as we're executing
-- seconds - how many seconds have elapsed
function Update(seconds)
   if gActive then
      -- Create 10 particles per second
      CreateParticles( 150 );
   end
end

