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

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

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

-- ------------------------------------------------------------
-- 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
if not dPos then
    dPos = Vec2( 0, 0 ) ;
end

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

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

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

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

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

-- color of particles
if not dColor then
   dColor = Color(1.0,1.0,1.0,0.35);
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( -dAngleChange, dAngleChange ) );

-- Set the position
pPosition:Init( dPos + (f2dRotation( pAngle ) * fRange( -dRange, dRange) ));
--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( pPosition + fTimeScale( f2dRotation( pAngle ) * fRange( -15, 15 ) ) );


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

