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

pRadius = Allocate(1); -- Allocate a TReal radius member

pMyCloudLocus = Allocate(1);

pX = Allocate(1);
pY = Allocate(1);

pX0 = Allocate(1);
pX1 = Allocate(1);
pY0 = Allocate(1);
pY1 = Allocate(1);
pMX0 = Allocate(1);
pMX1 = Allocate(1);
pMY0 = Allocate(1);
pMY1 = Allocate(1);
pT  = Allocate(1);

-- 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("FeuFolet");

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

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


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

--pVelocity:Init(fCloud(1,0));

-- Range : select a value for x between [-200,200] & for y [-300,120]
--                           X     Y           X      Y


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

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

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

pX0:Init(750); -- particle position x
pY0:Init(250); -- particle position y 
pX1:Init(50);  -- score position x
pY1:Init(100); -- score position y
pMX0:Init(pX0 + fCos(3*(3.14/4)) * 750);
pMY0:Init(pY0 + fSin(-3*(3.14/4)) * 750);
pMX1:Init(pX1 + fCos((3.14/4)) * 750);
pMY1:Init(pY1 + fSin((-3.14/4)) * 750);
pT:Init(0.0);

pX:Init(pX0);
pY:Init(pY0);

pPosition:Init(Vec2(pX0,pY0));


CreateParticles(1);

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


pAge:Anim(pAge+fAge()); -- + fCloud(2,0,pPosition[1],pPosition[2])
Anim( fExpire( fGreater(pAge,10000) ) );

-- fTimeScale(Vec2(0,400) --> add a fractionnal velocity position 
-- which is stored in pixels/second


pX:Anim(fSpline(pT,pX0,pX1,pMX0,pMX1));
pY:Anim(fSpline(pT,pY0,pY1,pMY0,pMY1));
pT:Anim(fClosedCycle(0,1,250));
 
pPosition:Anim(Vec2(pX,pY));

--pVelocity:Anim(fCloud(2,0,pPosition[1],pPosition[2])); -- by default value 0 means i am the center and all cloud gravite around me



--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(Vec2(1.1,1.1)) );

-- adjust particle age


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



-- ------------------------------------------------------------
-- 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 * 0 );
   end
   
end


