-- Additional parameters

pVelocity = Allocate( 2 ) ;
pSpin = Allocate( 1 );
pSpinSpeed = Allocate( 1 );
pSpinSpeed2 = Allocate( 1 );
pFallDirection = Allocate( 1 );
pFallDistance = Allocate( 2 );
pTimes = Allocate( 3 );
pDirections = Allocate( 3);
pAge = Allocate( 1 );
pFadeOut = Allocate ( 2 );
pAge2 = Allocate( 1 );
-- Set the particle texture
SetTexture("leaves.xml");
SetBlendMode(kBlendNormal);

-- Create some particles
SetNumParticles(2);    -- Create some particles

-- Init Particles
pPosition:Init( fRange(Vec2(-267,-100), Vec2(267,00))); -- initial position
pColor:Init( Color(1,1,1,1) ); -- blending color
pScale:Init( fRange( 0.5, 1.0 ) ); -- initial size
pVelocity:Init( fRange( Vec2( 0, 3 ), Vec2( 0, 5 ) ) ) ; -- random velocity
pSpin:Init( fRange( 0, 2*3.1415927 ) ); -- random orientation
pSpinSpeed:Init( fRange( -3, 3 ) ); -- random spin speed
pSpinSpeed2:Init( fRange( 400, 600 ) ); -- random spin speed
pFallDirection:Init( 1.0 );
pUp:Init( f2dRadius( pSpin ) );   -- random radius
pAge:Init( 0 );
pTimes:Init( fRange( Vec3(1500,3500,5000), Vec3(2000,4000,5500) ) );
pDirections:Init( fRange( Vec3( 0.1, 2.5, 0.3), Vec3( 1.0, 3.0, 0.7) ) );
pAge2:Init( fRange( 0, 500) );



-- Move the particles
-- define the movement
pAge:Anim(  pAge + fAge()  );
pAge2:Anim(  fLess( pAge2, pSpinSpeed2 ) * pAge2 + fAge());
pFallDirection:Anim( fFade( pAge, pDirections[0], pTimes[0] , pDirections[1], pTimes[1] , pDirections[2], pTimes[2], pDirections[1], pTimes[2] + 3000, pDirections[2]));
pFallDistance:Anim( f2dRotation( pFallDirection) );
-- fade out particle
-- pColor:Anim( fFade( pPosition[1], Color(1,1,1,1), 500, Color(1,1,1,1),  590, Color(0.5,0.5,0.5,0.5), 595, Color(0.1,0.1,0.1,0) ) );
--pColor:Anim( Color( 1,1,1, - pPosition[2]*0.01) );
-- update movement
pPosition:Anim( pPosition + fTimeScale( pVelocity ) +  fTimeScale(pFallDistance * 30)  ); -- position update  40 scale the speed
pSpin:Anim( pSpin + fTimeScale( pSpinSpeed) ); -- spin update
pUp:Anim( f2dRotation( pSpin + pFallDirection ) ); -- particle orientation
pFrame:Anim( fFade( pAge2, 0, pSpinSpeed2, 10 ) );
-- Expire particles lower than 600
Anim( fExpire( fGreater(pPosition[2],  600) ) );

-- intial
CreateParticles( 1 );
-- A function to run as we're executing
-- seconds - how many seconds have elapsed


-- USE THIS TO CHANGE THE AMOUNT OF LEAVES
LeavesPerSecond = 0.05;

function Update(seconds)
   -- Create 10 particles per second
   CreateParticles( seconds * LeavesPerSecond );
end

