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

-- Define our custom particle properties
pAge = Allocate( 1 ) ; -- Allocate a TReal age member
pAngle = Allocate( 1 ); -- Allocate a TReal angle member
pAngleChange = Allocate( 1 ); -- Allocate a angle change member
pPos = Allocate( 2 );
dPos = Allocate( 2 );

-- This controls number of particles used
particles = 1;

-- Set the particle texture
SetTexture("volume.png");

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

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

-- This is used to set the origin position
dPos:Init( fRange( Vec2( 0, -50 ), Vec2( 50, 50 ) ) );

-- This is used to set the main angle
if not dAngle then
   dAngle = 1.2;
end

-- This argument controls if we will change the angle
if not dAngleChange then
   dAngleChange = 0.05;
end

-- This is used to set the dispersion
if not dDisperse then
   dDisperse = 0.05;
end

-- This is used to define range
if not dRange then
   dRange = 100;
end

-- scale of particles
if not dScale then
   dScale = 1.0;
end

-- color of particles
if not dColor then
   dColor = Color(1.0,1.0,1.0,0.25);
end


-- Start initial rotation as a random angle
pAngle:Init( fRange( dAngle - dDisperse, dAngle + dDisperse) );

-- Start the change in rotation as a random
pAngleChange:Init( fRange( 0, dAngleChange ) );

-- Set the position
pPos:Init( f2dRotation( pAngle ) * fRange( -dRange, dRange ) );

pPosition:Init( dPos + pPos -  f2dRotation( pAngle - 1.47) * 250 * pScale );
--pPosition:Init( dPos );


-- Start color (tint) off as white (natural color of image)
pColor:Init( Color(0,0,0,0) );

-- Start scale out as 0.5
pScale:Init( fRange(dScale - dDisperse, dScale + dDisperse));

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

pUp:Init( f2dRotation( pAngle) );

-- 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
-- pPosition:Anim( pPosition +  fTimeScale( f2dRotation(pAngle) * dScale * 8) );

--pScale:Anim( pScale + fTimeScale(1.5) );
pAngle:Anim( pAngle + fTimeScale( pAngleChange ) );
pUp:Anim(  f2dRotation( pAngle) );
pColor:Anim( fFade( pAge, Color(1,1,1,0.01), 2000, dColor, 8000, Color(0,0,0,0) ) );
-- pPosition:Anim( dPos + f2dRotation( pAngle ) * 250   );
pPosition:Anim( dPos + pPos - f2dRotation( pAngle - 1.47) * 250 * pScale);


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

-- Expire particles over 1500 ms old
Anim(  fExpire(  fLess( pColor[ 4 ], 0.001 )  )  );

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

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

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

