--------------------------------------------------------------------------------
-- Jorge Eremiev - May 8, 2008
--
-- Particle system used in the stations
--------------------------------------------------------------------------------
--[[ INITIALIZATION PHASE ]] 

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

if not initialColor then
   initialColor = Color(0.95, 0.85, 0.04, 0.8); 
end

if not finalColor then
   finalColor = Color(0.95, 0.85, 0.04, 0);
end

if not acceleration then
   acceleration = Vec2(0, 0)
end

lifetime = 900

initialTime = 2 * (lifetime * 0.1);
fadeTime = 5 * (lifetime * 0.1);
deathTime = 3 * (lifetime * 0.1);

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

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

--[[ ACTION PHASE ]]

-- Give initial values to some of the variables
fScale = 0.8;
pScale:Init(0.8);
pPosition:Init(fRange(Vec2(-5, -5), Vec2(5, 5)));
pVelocity:Init(fRange(Vec2(-40, -40), Vec2(40, 40)));

-- 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, initialTime, initialColor, fadeTime, initialColor, deathTime, finalColor));

-- Make the particle bigger
--pScale:Anim(fFade(pAge, 0.2, initialTime , 1.0, fadeTime + deathTime, 0.6));
pScale:Anim(fFade(pAge, 1, fadeTime + deathTime, 0.6));
-- 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;

-- This variable must be changed from C++, when the worker is donde
continueWave = true;

-- Timer that will tells us for how long to create particles
local waveTimer;
createWaveTimer = true;

-- Timer that will keep track of the time we have to wait, before the new wave
-- of particles begin
local waitTimer;

-- Define the Update function
function Update(seconds)

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