---------------------------------------------------------------------
--	Author: Xavier Richter
--	Copyright (c) GIMAGIN 2009, All Rights Reserved.
---------------------------------------------------------------------


-- Phases to follow

-- 1) Create any custom particle properties.
-- 2) Set the particle texture and blend mode.
-- 3) Set the number of particles in the system.
-- 4) Set up the particle initializer functions.
-- 5) Set up the particle animation functions.
-- 6) Define an Update function that creates particles.

-- The order of the first three operations above is strict; 
-- the rest are just by convention

-- All functions accessible into this script are defined into
-- Lua Particle System Reference.

-- C++ is defined into <pf/luaparticlesystem.h> 

-- See the documentation for more information

-- ------------------------------------------------------------
-- Phase 1 : Initialization

-- Default particle property : 
   -- pPosition : A 2d Position TReal[2]
   -- pUp : A 2d Up-vector where 0,-1 make a particle "upright" TReal[2]
   -- pScale : A scale TReal[1]
   -- pColor : An RGBA color TReal[4] 
   -- pFrame : A frame for use with animated particles. TReal[1]
---------------------------------------------------------------
-- Define our custom particle properties



-- velocity is particle move speed
pVelocity = Allocate(2) ; -- Allocate a Vec2 velocity member

-- age is the life time
pAge = Allocate(1) ; -- Allocate a TReal age member

-- rotation angle
pSpin = Allocate(1); -- Allocate a TReal spin member

-- speed with the rotation is made
pSpinSpeed = Allocate(1); -- Allocate a TReal spin member

-- dLocus is a data source. The new FluidFX 2.0 defines dLocus for us,
-- but in case you want to load this particle system in a context
-- where you have no data source defined, this code will give dLocus
-- a default value.
-- dLocus represented a emission source
if not dLocus then
   dLocus= Vec2(0,0) ;
end



-- ------------------------------------------------------------
-- Phase 2 & 3 : Setting 
---------------------------------------------------------------
-- Set the particle texture
SetTexture("bulle");

-- Set the blend mode
--SetBlendMode(kBlendNormal);
--SetBlendMode(kBlendOpaque);
SetBlendMode(kBlendAdditiveAlpha);
--SetBlendMode(kBlendSubtractive);
--SetBlendMode(kBlendMultiplicative);

-- Set the size of the particle pool.
SetNumParticles(400);


-- ------------------------------------------------------------
-- Phase 4 : Action
---------------------------------------------------------------

-- Pick : select a point 10 pixels to the left(-10) or 10 pixels to the right(10) 
-- dLocus represented the emission source
pPosition:Init( fPick( Vec2(-10,0), Vec2(10,0) ) + dLocus  );


-- Range : select a value for x between [-200,200] & for y [-300,120]
--                           X     Y           X      Y
pVelocity:Init( fRange( Vec2(-100,-50), Vec2(100,-50) ) );

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

-- Start scale out as 0.5
pScale:Init( 0.5 );

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

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

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

-- ------------------------------------------------------------
-- Phase 5 : Particle Parameter Animation Functions
---------------------------------------------------------------

-- When you put a particle property in an equation, it returns an object 
-- that references the particle property that it represents.
-- Here it's pVelocity

-- fTimeScale(Vec2(0,400) --> add a fractionnal velocity position 
-- which is stored in pixels/second 
pVelocity:Anim( pVelocity + fTimeScale(Vec2(0,800)) );
pPosition:Anim( pPosition + fPick(Vec2(-5,-3),Vec2(5,-3)));
   

--pPosition:Anim( pPosition + fPick(Vec2(-5,-3), Vec2(5,-3)));
-- fTimeScale(1) add a fractionnal scale every frame
-- with a base scale equal to 0,5, scale will go from 0,5 to 1,5
-- in 1 second
pScale:Anim( pScale + fTimeScale(1) );

-- adjust particle age
pAge:Anim( pAge+fAge() );

-- fFade take as value one or many couple (Color,Time in ms) crescent in time 
-- Function calcul color transition as times go
pColor:Anim( fFade( pAge,Color(1,1,1,1), 500, Color(1,0,0,1), 1000, Color(1,1,1,0) ) );

-- Spin update
pSpin:Anim( pSpin + fTimeScale( pSpinSpeed ) );

-- pUp is a default particle property
-- f2dRotation produces a 2d vector based on a value in radians
-- which is what we've been calculating in pSpin 
-- (see Lua Particle System Reference in the documentation )
pUp:Anim( f2dRotation( pSpin ) );

-- if pAge > 2500 kill this
Anim( fExpire( fGreater(pAge,2500) ) );


-- ------------------------------------------------------------
-- Phase 6 : Definition of an Update function that creates 
-- particles.
---------------------------------------------------------------

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

-- A function to run as we're executing
-- seconds - how many seconds have elapsed
-- until until the value of SetNumParticles(); is reached
function Update(seconds)
   if gActive>0 then
      -- Create 10 particles per second
      CreateParticles( seconds * 10 );
   end
end
