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

gExplosion = false ;


function myFunction()

	pAge:Anim( pAge+fAge() );
	pTAdvance:Anim(fClosedCycle(pTAdvanceFade,0.2,300));

	pT:Anim(fFade(pAge,0,pStart-1500,0,1,fClosedCycle(0,1,pStart+300)));
	pX:Anim(fSpline(pT+pTAdvance,pX0,pX1,pMX0,pMX1));
	pY:Anim(fSpline(pT+pTAdvance,pY0,pY1,pMY0,pMY1));
	pPosition:Anim(Vec2(pX,pY));
	
end


-- 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 particle characteristics
pAge = Allocate(1) ; -- Allocate a TReal age member
pSpin = Allocate(1); -- Allocate a TReal spin member
pRadius = Allocate(1); -- Allocate a TReal radius member
pSpinSpeed = Allocate(1); -- Allocate a TReal spin member


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);
pTAdvance = Allocate(1);
pTAdvanceFade = Allocate(1);
pHandicap = Allocate(1);

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

pD = Allocate(1);

--Timer
pStart = Allocate(1);
pSpline = Allocate(1);


-- ------------------------------------------------------------
-- Phase 2 & 3 : Setting 
---------------------------------------------------------------
-- Set the particle texture
SetTexture("fx/images/feu_folet_medium_bordeau.png");
SetBlendMode(kBlendNormal);
SetNumParticles(40);    -- Create some particles

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

pRadius:Init( fRange( 20,120 ) ); --20/80
pColor:Init( Color(1,1,1,1) );
pScale:Init(1);
pAge:Init(0);
pSpin:Init( fRange( 0, 2*3.1415927 ) );
pSpinSpeed:Init( fRange( -10, 10 ) );

--pPosition:Init( f2dRotation(pSpin)*pRadius + Vec2(800,500)); fx/images/
pD:Init(200);
pStart:Init(1900);
pSpline:Init(1000);

pX0:Init(dLocus[1]); -- center position registred into fCloud
pY0:Init(dLocus[2]);
pX1:Init(50); -- score
pY1:Init(230);

pMX0:Init(fCos((3.14/4)) *pD);
pMY0:Init(fSin((-3.14/4))* pD );
pMX1:Init(fCos((3.14/4)) * pD*3);
pMY1:Init(fSin((-3.14/4)) * pD*3);

pT:Init(0.0);
pTAdvanceFade:Init(fRange(-0.2,0.2));
pTAdvance:Init(pTAdvanceFade);


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

pSpin:Anim( pSpin + fTimeScale( pSpinSpeed ) );
pScale:Anim( fRange(0.9,2.0) );
pColor:Anim( fFade( pAge,Color(1,1,1,0), 500, Color(1,1,1,0),1, Color(1,1,1,1),500,Color(1,1,1,0))  );
pRadius:Anim(pRadius); --fFade( pAge,pRadius,pStart,pRadius - fTimeScale(1),1,pRadius));
Anim(myFunction());
Anim(fExpire(fGreater(fInScore(pX1-15,pY1-15,40,100,pX,pY),0)));

--pPosition:Anim( pPosition );


pUp:Anim( f2dRotation( pSpin ) );
-- Add time to the age


-- Expire particles over 1500 ms old

--Anim( fExpire(fGreater(pAge,2500) ));


-- Create an initial particle
--CreateParticles(30);

-- ------------------------------------------------------------
-- Phase 6 : Definition of an Update function that creates 
-- particles.
---------------------------------------------------------------
gUpdate = 0 ;




-- A function to run as we're executing
-- seconds - how many seconds have elapsed
function Update(seconds)
   -- Create 40 particles per second
   if gUpdate < 40 then
		gUpdate = gUpdate + 1 ;
		CreateParticles( seconds*40 );
   end
end

