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

pAge = Allocate(1) ; -- Allocate a TReal age member
pSpin = Allocate(1); -- Allocate a TReal spin member
pOriginalRadius = Allocate(1); -- Allocate a TReal radius member
pRadius = Allocate(1); -- Allocate a TReal radius member
pSpinSpeed = Allocate(1); -- Allocate a TReal spin member
pAngleX = Allocate(1);
pAngleY = Allocate(1);
pX = Allocate(1);
pY = Allocate(1);
pOriginalX = Allocate(1);
pOriginalY = Allocate(1);
pRandomAngle = Allocate(1);
pScaleOrigin = Allocate(1);

-- ------------------------------------------------------------
-- Phase 2 & 3 : Setting 
---------------------------------------------------------------
-- Set the particle texture
SetTexture("fx/images/feu_folet_big_yellow.png");
SetBlendMode(kBlendNormal);
SetNumParticles(50);


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

pOriginalRadius:Init( 0 ); -- rayon en pixel
pRadius:Init( 110 ); -- rayon en pixel
pColor:Init( Color(1,1,1,1) );
pScaleOrigin:Init(fRange(0.2,0.7));
pScale:Init( pScaleOrigin );
pAge:Init(0);
pRandomAngle:Init(fRange(0,2*3.1459));
--pRandomAngle:Init(fRange(0,3.1459));
pAngleX:Init(f2dRotation(fAngleCircle(pRandomAngle,30))[1]);
pAngleY:Init(f2dRotation(fAngleCircle(pRandomAngle,30))[2]);

pOriginalX:Init(pAngleX*pRadius + dLocus[1]);
pOriginalY:Init(pAngleY*pRadius + dLocus[2]);

pX:Init(pOriginalX);
pY:Init(pOriginalY);
pPosition:Init(Vec2(pOriginalX,pOriginalY));  -- position around dlocus

pSpin:Init( fRange( 0, 2*3.1415927 ));
pSpinSpeed:Init(5);


-- ------------------------------------------------------------
-- Phase 5 : Particle Parameter Animation Functions
---------------------------------------------------------------
--pPosition:Anim(pPosition); -- f2dRotation(NextAngle(nb part));
 
 

--pRadius:Anim(fFade(pAge,pRadius,800,0,5000,500)); effet marrant
pRadius:Anim(fFade(pAge,pRadius,800,pRadius,5000,pRadius+40));
--pX:Anim(fFade(pAge,pOriginalX,3000,pOriginalX,3001,pOriginalX + (pRadius * fCos(pAngleX))));
--pY:Anim(fFade(pAge,pOriginalY,3000,pOriginalY,3001,pOriginalY + (pRadius * fSin(pAngleY))));

pScale:Anim(fFade(pAge,pScale,800,pScaleOrigin + 0.3 ,800, pScaleOrigin + 0.4)); 


 pX:Anim((pAngleX*pRadius)+ dLocus[1]);
 pY:Anim((pAngleY*pRadius)+ dLocus[2]);

pPosition:Anim(Vec2(pX+fRange(-1,1),pY));
pColor:Anim( fFade(pAge,Color(1,1,1,1),800,Color(1,1,1,1),600,Color(1,1,1,0)) );

pSpin:Anim( pSpin + fTimeScale(pSpinSpeed)  );


pUp:Anim(  f2dRotation( pSpin )  ); --fClosedCycle(5,0,900) + 
-- Add time to the age
pAge:Anim( pAge+fAge() );

-- Expire particles over 1500 ms old
Anim( fExpire( fGreater(pAge,1400) ) );

-- Create an initial particle
CreateParticles(0);

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


