--------------------------------------------------------------------------------
-- Pedro - June 20, 2008
--
-- Particle system used in the levels that have candels as decorations.
--------------------------------------------------------------------------------

--[[ INITIALIZATION PHASE ]] 

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

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

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

lifetime = 600;

fadeInTime = 2 * lifetime / 10;
fadeOutTime = 4 * lifetime / 10;
deathTime = 4 * lifetime / 10;

-- Set the particles texture
SetTexture("working_particle");
SetBlendMode(kBlendAdditiveAlpha);

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

--[[ ACTION PHASE ]]

-- Give initial values to some of the variables
pScale:Init(0.1);
pPosition:Init(fRange(Vec2(-2, -2), Vec2(2, 2)));
pVelocity:Init(fRange(Vec2(-3, 0), Vec2(3, -5)));

-- Update the particles velocity and position
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(pScale + fTimeScale(0.45));

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

-- Define the Update function
function Update(seconds)

       -- Start creating particles
      CreateParticles(seconds * 12);
end
