--------------------------------------------------------------------------------
-- 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(1, 1, 1, 1); 
end

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


acceleration = Vec2(0, 700)


lifetime = 1000

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

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

--[[ ACTION PHASE ]]

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


-- Update the particles velocity and position
--pVelocity:Anim(pVelocity + fTimeScale(fRange(Vec2(-20, -1), Vec2(20, -35))));
pVelocity:Anim(pVelocity + fTimeScale(acceleration));
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, initialColor, lifetime, finalColor));

-- Make the particle bigger
pScale:Anim(pScale + fTimeScale(0.4));

-- 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
createTimer = true

-- Define the Update function
function Update(seconds)

   if gActive then                   
      CreateParticles(seconds*600);
      if createTimer then 
         creationTimer = NewTimeout(0.5) 
         createTimer = false
      end   
      end                           
      
      if creationTimer then
         if creationTimer(seconds) then
            gActive = false;
         end
      end

end