---------------------------------------
-- Creates the list of particle-systems
---------------------------------------
function newParticleSystems()
   local o = {}
   
   
   ----------------------------------------------
   -- Constructor of the list of particle-systems
   ----------------------------------------------
   function o:construct()
      self.spriteBonus = newSprite("images/sprites.png", 0,  114, 16, 16)
      self.spriteFire  = newSprite("images/sprites.png", 38, 114, 16, 16)
      
      self.particleSystems = {}
      return self
   end
   
   
   -----------------------------
   -- Adds sparkles for the menu
   -----------------------------
   function o:addMenuSparkles(x, y)
      local system = newParticleSystem(self.spriteBonus, 100)
      system:setPosition(x, y)
      system:setLifetime(0.2)
      system:setParticleLife(4.0)
      system:setEmissionRate(200)
      system:setSpeed(10, 70)
      system:start()
      table.insert(self.particleSystems, system)
      return system
   end
   
   
   ----------------------------
   -- Adds an explosion of fire
   ----------------------------
   function o:addFireExplosion(x, y)
      local system = newParticleSystem(self.spriteFire, 100)
      system:setPosition(x, y)
      system:setLifetime(0.1)
      system:setParticleLife(2.0)
      system:setEmissionRate(50)
      system:setSpeed(20, 40)
      system:start()
      table.insert(self.particleSystems, system)
      return system
   end
   
   
   -------------------------------------
   -- Adds an explosion used for bonuses
   -------------------------------------
   function o:addBonusExplosion(x, y)
      local system = newParticleSystem(self.spriteBonus, 100)
      system:setPosition(x, y)
      system:setLifetime(0.1)
      system:setParticleLife(2.0)
      system:setEmissionRate(50)
      system:setSpeed(20, 40)
      system:start()
      table.insert(self.particleSystems, system)
      return system
   end
   
   
   --------------------------------
   -- Adds a flame used for rockets
   --------------------------------
   function o:addRocketFlame(x, y)
      local system = newParticleSystem(self.spriteFire, 100)
      system:setPosition(x, y)
      system:setLifetime(0.5)
      system:setParticleLife(0.4)
      system:setSize(0.1, 1.0)
      system:setEmissionRate(10)
      system:setSpeed(0, 0)
      system:start()
      table.insert(self.particleSystems, system)
      return system
   end
   
   
   -------------------------------
   -- Updates the particle-systems
   -------------------------------
   function o:update(dt)
      local i = 1
      while (i <= #self.particleSystems) do
         local system = self.particleSystems[i]
         system:update(dt)
         if (not system:isRunning()) then
            table.remove(self.particleSystems, i)
         else
            i = i + 1
         end
      end
   end
   
   
   -----------------------------
   -- Draws the particle-systems
   -----------------------------
   function o:draw()
      for k, system in ipairs(self.particleSystems) do
         system:draw()
      end
   end
   

   return o:construct()
end
