// Initialize particle effects manager
function MenuParticleFx::onWake(%this)
{
   %this.particleMax = 5000;
   %this.particleInUse = -1;
   
   %this.particleIdxTable = 0;   
   
   // Create particles
   for(%i=0; %i<%this.particleMax; %i++)
   {
      %obj = ("P"@%i);
      if(!isObject(%obj))      
         %newP = new GuiPlasticBitmapCtrl(%obj);
      else
         %newP = %obj;
      %newP.useAddBlending = "1";         
      %newP.setVisible(false);
      MenuParticleFx.add(%newP);
   }
}

// Returns the ID of a particle
function MenuParticleFx::getAvailableParticle(%this)
{     
   %this.particleInUse++;
   
   return %this.particleInUse % %this.particleMax;
}

// Produces a fireworks effect
// %x and %y are the screen location of the particle generation
// %aniTime is the total animation time, in seconds
// %scale is a scale factor of the particle
function MenuParticleFx::FireworksFx(%this, %x, %y, %aniTime, %scale)
{
   
   
   %scalex = %scale * (1.0 / 800 * getWord(canvas.getExtent(),0));
   %scaley = %scale * (1.0 / 600 * getWord(canvas.getExtent(),1));
   
   %this.particleIdxTable++;
   %this.particleIdxNum[%this.particleIdxTable] = 0;

   for(%i=0; %i<100; %i++) // 15
   {
      %idx = %this.getAvailableParticle();
         
      %this.particleTable[%this.particleIdxTable, %this.particleIdxNum[%this.particleIdxTable]] = %idx;
      %this.particleIdxNum[%this.particleIdxTable]++;
      
      ("P"@%idx).particleFireworks(%x, %y, %aniTime, %scalex, %scaley);      
   }
     
   %particleIdx = %this.particleIdxTable;
   %particleNum = %this.particleIdxNum[%this.particleIdxTable];
   scheduleObjLCA(%this, "Menu", %aniTime*1000, "onDeathGroup", 2, %particleIdx, %particleNum);   
}

// Hide sparkle when end of animation is reached
function MenuParticleFx::onDeathGroup(%this, /*%idxFirst, %idxNum*/%idxTable, %idxNum)  
{
   for(%i=0; %i<%idxNum; %i++)
      ("P"@%this.particleTable[%idxTable, %i]).setVisible(false);
}

// Produces a comet effect going from (%x,%y) to the (%xDest,%yDest)
// %x and %y are the screen location of the particle fx start position
// %aniTime is the total animation time, in seconds
// %scale is a scale factor of the particle
// %fileType: 0 for star, 1 for cheese
function MenuParticleFx::CometFx(%this, %x, %y, %xDest, %yDest, %aniTime, %scale, %fileType)
{
   %winScaleX = 1.0 / 800 * getWord(canvas.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(canvas.getExtent(),1);
   
   %scale2 = %scale;
   if(%fileType > 0)
      %scale2 = 1.25;
   
   %direcX = %xDest;   
   %direcY = %yDest;
   
   //%direcX *= %winScaleX;
   //%direcY *= %winScaleY;
   
   %vecX = %direcX - %x;
   %vecY = %direcY - %y;
   
   %vecLen = mSqrt(%vecX*%vecX + %vecY*%vecY);
   
   %steps = %vecLen / 40;
   %stepsize = 1 / %steps;
   for(%t=0; %t<=1;)
   {
      %curX = (%vecX*%t)+%x;
      %curY = (%vecY*%t)+%y;
      
      %time = %aniTime*1000*%t;
      scheduleObjLCA(%this, "Menu", %time, 
         "CometDirectorFxHelper", 6, 
         %vecX, %vecY, %curX, %curY, %scale2, %fileType);   

      %stepsize = 1 / (%steps + getRandom(50));
      %t+=%stepsize;
   }
   
   %idx = %this.getAvailableParticle();
   %obj = ("P"@%idx);      
 
   %sizeX = 19 * %scale2 * %winScaleX;   
   %sizeY = 18 * %scale2 * %winScaleY;
   
   %origX = %x - (%sizeX / 2);
   %origY = %y - (%sizeY / 2);
   
   %obj.rewind();
   %obj.setExtent(%sizeX, %sizeY);
   %obj.setPosition(%origX, %origY);
   
	switch(%fileType)
	{
	case 0: %obj.setDML("scriptsAndAssets/data/ui/ho/particles/spark/small_spark.png");
	case 1: %obj.setDML("scriptsAndAssets/data/videos/sparkle/particle_cheese.png");
	case 2: %obj.setDML("scriptsAndAssets/data/ui/ho/particles/spark/spark.png");
	}

   %obj.setVisible(true);
   %obj.alpha = "1";
   %obj.aniTime = %aniTime;

   %obj.animateCenter(%direcX+%sizeX/2, %direcY+%sizeY/2, 0, %aniTime*1000, "0 0");
   scheduleObjLCA(%this, "Menu", %aniTime*1000, "onDeath", 1, %idx);
   
   scheduleObjLCA(%this, "Menu", %aniTime*1000, "CometHitMeterFx", 5, 
      %direcX, %direcY, %aniTime*2, %scale);   
}

// Produces a cascading effect from (%x,%y) to the right side of the screen
// %x and %y are the screen location of the particle fx start position
// %aniTime is the total animation time, in seconds
// %scale is a scale factor of the particle
function MenuParticleFx::CometHitMeterFx(%this, %x, %y, %aniTime, %scale)
{   
   %scalex = %scale * (1.0 / 800 * getWord(canvas.getExtent(),0));
   %scaley = %scale * (1.0 / 600 * getWord(canvas.getExtent(),1));

   setRandomSeed(getRealTime());   
   
   %this.particleIdxTable++;
   %this.particleIdxNum[%this.particleIdxTable] = 0;   
     
   for(%i=0; %i<70; %i++)
   {
      %idx = %this.getAvailableParticle();
         
      %this.particleTable[%this.particleIdxTable, %this.particleIdxNum[%this.particleIdxTable]] = %idx;
      %this.particleIdxNum[%this.particleIdxTable]++;         
         
      ("P"@%idx).particleHitMeter(%x, %y, %aniTime, %scalex, %scaley);
   }
   
   %particleIdx = %this.particleIdxTable;
   %particleNum = %this.particleIdxNum[%this.particleIdxTable];

   scheduleObjLCA(%this, "Menu", %aniTime*1000, "onDeathGroup", 2, %particleIdx, %particleNum);
}

// Helper function of CometDirectorFx
// Creates a new particle and trail at %x and %y
// %vecX and %vecY define the direction vector of the comet
function MenuParticleFx::CometDirectorFxHelper(%this, %vecX, %vecY, %x, %y, %scale, %fileType)
{
   %scalex = %scale * (1.0 / 800 * getWord(canvas.getExtent(),0));
   %scaley = %scale * (1.0 / 600 * getWord(canvas.getExtent(),1));

   %this.particleIdxTable++;
   %this.particleIdxNum[%this.particleIdxTable] = 0;
   
   %aniTime = 1;
     
   for(%i=0; %i<5; %i++)
   {
      %idx = %this.getAvailableParticle();
         
      %this.particleTable[%this.particleIdxTable, %this.particleIdxNum[%this.particleIdxTable]] = %idx;
      %this.particleIdxNum[%this.particleIdxTable]++;         
         
      ("P"@%idx).particleComet(%vecX, %vecY, %x, %y, %scalex, %scaley, %fileType, %aniTime);
   }

   %particleIdx = %this.particleIdxTable;
   %particleNum = %this.particleIdxNum[%this.particleIdxTable];
   scheduleObjLCA(%this, "Menu", %aniTime*1000, "onDeathGroup", 2, 
      %particleIdx, %particleNum);   
   
   return;
}

// Hide particle when end of animation is reached
function MenuParticleFx::onDeath(%this, %idx)  
{
   ("P"@%idx).setVisible(false);
}