--------------------------------------------------------------------------------
-- Jorge Eremiev - May 8, 2008
--
-- Particle system used in the stations
-- CASH REGISTER
--------------------------------------------------------------------------------

--[[ INITIALIZATION PHASE ]] 

-- Create our custom variables
pVelocity = Allocate(2); 
pAge = Allocate(1);
pSpin = Allocate(1); 
pSpinSpeed = Allocate(1); 

if not initialColor then
   initialColor = Color(1, 1, 1, 1); 
end

if not finalColor then
   finalColor = Color(1, 1, 1, 0);
end

lifetime = 800

fadeInTime = 3 * lifetime / 10;
fadeOutTime = 5 * lifetime / 10;
deathTime = 2 * lifetime / 10;

-- Set the particles texture
SetTexture("moneda_texture");
SetBlendMode(kBlendNormal);

-- The maximun number of particles available
SetNumParticles(15);

--[[ ACTION PHASE ]]
if not dLocus then
   dLocus= Vec2(0,0) ; 
end
pPosition:Init( fRange( Vec2(-0,-0), Vec2(0,0) )+dLocus );
-- Give initial values to some of the variables
fScale = 0.4;
pScale:Init(0.4);
pVelocity:Init(fRange(Vec2(-50, -100), Vec2(50, -200)));
pSpin:Init( fRange( 0, 2*3.1415927 ) );
pSpinSpeed:Init( fRange(-5, 5));

-- Update the particles velocity and position
pVelocity:Anim(pVelocity + fTimeScale(fRange(Vec2(0, 400), Vec2(0, 600))));
pPosition:Anim(pPosition + fTimeScale(pVelocity));

-- Update the particles age
pAge:Anim(pAge + fAge());

-- Set the particles color according to its age
pColor:Anim(fFade(pAge, finalColor, fadeInTime, initialColor, fadeOutTime, initialColor, deathTime, finalColor));

-- Make the particle bigger
pScale:Anim(fFade(pAge, 0.2, fadeInTime, 0.5, fadeOutTime, 0.5, deathTime, 0.5));

-- Make the particle rotate
pSpin:Anim(pSpin + fTimeScale(pSpinSpeed) );
pUp:Anim( f2dRotation(pSpin) );

-- Kill the particle if it's too old
Anim(fExpire(fGreater(pAge, lifetime)));

-- This variable must be changed from C++, when the worker starts working
gActive = true;

-- Define the Update function
function Update(seconds)

   if gActive then
       -- Start creating particles
      CreateParticles(seconds * 90); -- seconds * 15
   end
end
