// Called when minigame GUI shows up
function InsectMinigame::onWake(%this)
{
   %this.isPlaying = true;   
   
   exec("~/data/ui/minigame/swat/"@playMinigameDlg.currentLevel@"/SwatConfig.cs");
   
   // Set skip button
   InsectSkipButton.setBitmap("scriptsAndAssets/data/ui/minigame/quit.png");
   
   %this.gameCtrl = new GuiContainer() {
            canSaveDynamicFields = "1";
            Enabled = "1";
            className = "InsectGameCtrl";    
            isContainer = "1";
            HorizSizing = "relative";
            VertSizing = "relative";
            Position = "0 0";
            Extent = "800 600";
            MinExtent = "1 1";
            canSave = "1";
            Visible = "1";
            hovertime = "1000";
            alpha = "1";
            color = "255 255 255 255";
            unitClip = "0 0 1 1";
            lockMouse = "0";
            Margin = "0 0 0 0";
            Padding = "0 0 0 0";
            AnchorTop = "1";
            AnchorBottom = "0";
            AnchorLeft = "1";
            AnchorRight = "0";
            useMouseEvents = "1";
   };                           

   MinigameRefCenter.add(%this.gameCtrl);

   InsectGui.init();

   MinigameRefCenter.pushToBack(%this.gameCtrl);
   MinigameRefCenter.pushToBack(InsectSkipButton);
   
   %this.gameFinished = false;
   
   // Force resizing
   %this.setExtent(getWord(Canvas.extent,0), getWord(Canvas.extent,1));
   Canvas.setWidescreen(MinigameRefCenter, 0, false);
   %this.hitSfxIteration = 1;
}

// Linear interpolation helper function
//#define LERP(a,b,t)     (((b) - (a)) * (t) + (a))

function InsectMinigame::lerp(%this, %a, %b, %t)
{
   //return %start*(1-%t) + %end*%t;
   return (%b - %a) * %t + %a;
}

// Return true if the given two AABB rectangles do intersect
// %l1 = Left position of rectangle 1
// %t1 = Top position of rectangle 1
// %r1 = Right position of rectangle 1
// %b1 = Bottom position of rectangle 1
// %l2 = Left position of rectangle 2
// %t2 = Top position of rectangle 2
// %r2 = Right position of rectangle 2
// %b2 = Bottom position of rectangle 2
function InsectMinigame::intersectRect(%this, %l1, %t1, %r1, %b1, %l2, %t2, %r2, %b2)
{
   return !(%l2 > %r1 || %r2 < %l1 || %t2 > %b1 || %b2 < %t1);
}

// Called by miniGame.cs
function InsectMinigame::start(%this)
{  
   %this.swatterReverseAnimation = false;
   %this.swatterIsPlaying = false;
   %this.swatterCurrentImage = 1;
   %this.score = 0;   
   %this.scoreFontSize = 36;
   
   %this.spiderWebSpawnInterval = 1680; // 1400
   
   %this.isIdle = false;
   
   %this.newSpiderWebIdx = 0;
   %this.newAntCounter1Idx = 0;
   %this.newAntCounter2Idx = 0;
   %this.newAntGroundIdx = 0;   
   %this.spiderWebNum = 0;
   %this.antCounter1Num = 0;   
   %this.antCounter2Num = 0;
   %this.antGroundNum = 0;   
   %this.spawnSpiderWeb();
   %this.spawnAntCounter1();
   %this.spawnAntCounter2();
   %this.spawnAntGround();   
      
   Canvas.hideCursor();   
   //%this.updateScore();
   %this.timerCounter = 61;
//   %this.onTimer();
   %this.onIdle();
   %this.onTick(1);
      
   %this.isConfettiAnimating = false;
   
   sfxPlay(BackgroundSwatMinigame);
   sfxPlay(MusicSpyLevel);
      
   InsectGui.pushToBack(Swatter);
   InsectGui.pushToBack(InsectSkipButton);
}

// Exit the minigame
function InsectMinigame::quit(%this)
{
   sfxStopAll();
   
   if(%this.isEnding)
      return;
            
   // Avoid new schedules
   %this.isEnding = true;
   
   //lockMouse(false);
   Canvas.showCursor();     
   
   %this.isPlaying = false;   
   
   // Widescreen support
   if(isObject(MinigameCenter))
   {
      Minigame.bringToFront(%this);
      MinigameCenter.setVisible(true);   
      Minigame.pushToBack(MinigameCenter);   
   }
   
   // Number of hints won
   Minigame.hintsGained = InsectGui.meter.getHintsGained();   
   
   %this.meter.pause = true;
   %this.counter.pause = true;
   
   if(mFloor(Minigame.hintsGained) > 0 && %this.gameFinished/* && PlayMinigameDlg.isPlayingMinigame !$= true*/)
   {      
      if(!%this.ConfettiIsPlaying)
      {
         if(isObject(PlayMinigameDlg))
         {            
            PlayMinigameDlgCenter.pushToBack(MinigameCircles);
            PlayMinigameDlgCenter.pushToBack(MinigameParticleFx);
            PlayMinigameDlgCenter.pushToBack(WinText);
            PlayMinigameDlgCenter.pushToBack(HintNumber);
            PlayMinigameDlgCenter.pushToBack(PMClapper);
            PlayMinigameDlgCenter.pushToBack(PMBoard);
            PlayMinigameDlgCenter.pushToBack(PMClapperStart);   
                     
            PlayMinigameDlg.bringToFront(%this);
            
            PlayMinigameDlg.setVisible(true);            
            PlayMinigameDlgCenter.setVisible(true);
         }
            
         // Play confetti
         MinigameParticleFx.ConfettiFx(15); // Play confetti for 15 secs
         %this.ConfettiIsPlaying = true;

         if(PlayMinigameDlg.isPlayingMinigame $= true)
            scheduleObjLCA(playMinigameDlg, "Minigame", 14*1000, "quitMinigame", 0);
         else
            // Quit a bit earlier
            scheduleObjLCA(playMinigameDlg, "Minigame", 12*1000, "quitMinigame", 0);
      }
   }
   else
   {
      Minigame.hintsGained = 0;
      
      if(isObject(PlayMinigameDlg))
         PlayMinigameDlg.bringToFront(PlayMinigameDlg.curMinigame);
      
      playMinigameDlg.quitMinigame();
   }
}

function InsectGameCtrl::onMouseMove(%this, %modifier, %worldPosition, %mouseClicks)
{
//   %winScaleX = 1.0 / 800 * getWord(MinigameRefCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(MinigameRefCenter.getExtent(),1);   
   
   // Get mouse position
   %mouseX = getWord(%worldPosition,0);
   %mouseY = getWord(%worldPosition,1);
   
   echo(%mouseY);
   if(%mouseY > 530*%winScaleY)
   {
      Swatter.setVisible(false);
      Canvas.showCursor();      
   }
   else
   {
      Swatter.setVisible(true);      
      Canvas.hideCursor();
      InsectMinigame.updateSwat(%mouseX, %mouseY);      
   }   
}

function InsectGameCtrl::onMouseEnter(%this)
{
   //lockMouse(true); 
   Canvas.hideCursor();
}

function InsectGameCtrl::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)
{
   InsectMinigame.onClick(getWord(%worldPosition,0), getWord(%worldPosition,1));
}

// Update the swat GUI control according to current mouse cursor position
function InsectMinigame::updateSwat(%this, %cursorX, %cursorY)
{   
   // Compensate widescreen offset
   %rect2 = MinigameRefCenter.getPosition();
   
   %cursorX = %cursorX - getWord(Swatter.Extent,0)/2 - getWord(%rect2,0);
   %cursorY = %cursorY - getWord(Swatter.Extent,1)/4 - getWord(%rect2,1);
   
   Swatter.setPosition(%cursorX, %cursorY);
/*   Swatter.setVisible(true);
   
   %cursorPos = Canvas.getCursorPos();
   Swatter.setPosition(
      getWord(%cursorPos,0) - getWord(Swatter.Extent,0)/2 - getWord(%rect2,0),
      getWord(%cursorPos,1) - getWord(Swatter.Extent,1)/4 - getWord(%rect2,1)); */
//   Swatter.setVisible(true);   
}

// Callback used for making the spiders go up
function InsectMinigame::onSpiderWebGoUp(%this, %spiderIdx, %newSpeed)
{
   ("SpiderWeb"@%this.spiderWebIdx[%spiderIdx]).speed = %newSpeed*3;
   ("SpiderString"@%this.spiderWebIdx[%spiderIdx]).speed = %newSpeed*3;   
}
// Tick event called at 1 FPS
function InsectMinigame::onTick(%this, %i)
{
   sfxPlay("AudioTimerBeep"@ ((%i % 2) + 1));
   if (%i > 76)
   {/*do nothing :P*/}
   else if (%i > 64)
      scheduleObjLCA(%this, "Minigame", 250, "onTick", 1, %i+1 );   
   else if (%i > 50)
	  scheduleObjLCA(%this, "Minigame", 500, "onTick", 1, %i+1 );   
   else
      scheduleObjLCA(%this, "Minigame", 1000, "onTick", 1, %i+1 );   
}

// Idle event called at 30 FPS
function InsectMinigame::onIdle(%this)
{
   if(%this.isConfettiAnimating)
      return;
      
   if(%this.isEnding)
      return;
      
   %this.isIdle = true;
   
//   %this.updateSwat();
   
   %winScaleX = 1.0 / 800 * getWord(MinigameRefCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(MinigameRefCenter.getExtent(),1);   

   // Update position of each spiderWeb   
   for(%j=0; %j<%this.spiderWebNum; %j++)
   {
      if(%this.spiderWebIdx[%j] $= "")
         continue;
      
      %obj  = ("SpiderWeb"@%this.spiderWebIdx[%j]);
      if(%obj.hit > 1)
         continue;
         
      %obj2 = ("SpiderString"@%this.spiderWebIdx[%j]);
      %posX = getWord(%obj.Position,0);
      %posY = getWord(%obj.Position,1);
      %sizeX = getWord(%obj.Extent,0);
      %sizeY = getWord(%obj.Extent,1);
      
      %newPosY = %posY + %obj.speed*%winScaleY;
      %obj.setPosition(%posX, %newPosY);
      
      %width2 = 20*%obj.scale*%winScaleX;
      %height2 = 1024*%obj.scale*%winScaleY;
      %obj2.setPosition((%posX+%sizeX/2) - %width2/2.3, %newPosY-%height2+%sizeY/2);
      %obj2.setExtent(%width2, %height2);

      // Test if it is out of the screen
      if(%obj.goingUp)
      {
         if(%posY < -%sizeY)
            %this.killSpiderWeb(%j);
      }
      else
      {        
         // Test if reached point of return
         if(%newPosY / getWord(MinigameRefCenter.getExtent(),1) > %obj.t)
         {
            %obj.goingUp = true;
            scheduleObjLCA(%this, "Minigame", 1000, "onSpiderWebGoUp", 2, %j, -%obj.speed);
            %obj.speed = 0;
         }
      }
   }

   // Update position of each ant in counter1
   for(%j=0; %j<%this.antCounter1Num; %j++)
   {
      if(%this.antCounter1Idx[%j] $= "")
         continue;
         
      %obj  = ("AntCounter1"@%this.antCounter1Idx[%j]);
      if(%obj.isHit)
         continue;
      
      %posX = %this.lerp(-40*%winScaleX, 189*%winScaleX, %obj.t);
      %posY = %this.lerp((339-65)*%winScaleY, (317-65)*%winScaleY, %obj.t);
      %obj.setPosition(%posX, %posY);

      // Test if it is out of the screen
      if(%obj.goingRight)
      {
         %obj.t -= 0.006;
         if(%obj.t <= 0)
            %this.killAntCounter1(%j);
      }
      else
      {
         %obj.t += 0.006;
         
         // Test if reached point of return
         if(%obj.t >= 1)
         {
            %obj.goingRight = true;
//            %obj.rotation = 0;
            %obj.setDML("scriptsAndAssets/data/ui/minigame/swat/Ant_AliveL.png");
         }
      }
   }

   // Update position of each ant in counter2
   for(%j=0; %j<%this.antCounter2Num; %j++)
   {
      if(%this.antCounter2Idx[%j] $= "")
         continue;
         
      %obj  = ("AntCounter2"@%this.antCounter2Idx[%j]);
      if(%obj.isHit)
         continue;
      
      %posX = %this.lerp(265*%winScaleX, 447*%winScaleX, %obj.t);
      %posY = %this.lerp((419-75)*%winScaleY, (399-75)*%winScaleY, %obj.t);
      %obj.setPosition(%posX, %posY);

      // Test if it is out of the screen
      if(%obj.goingRight)
      {
         %obj.t -= 0.006;
         if(%obj.t <= 0)
            %this.killAntCounter2(%j);
      }
      else
      {
         %obj.t += 0.006;
         
         // Test if reached point of return
         if(%obj.t >= 1)
         {
            %obj.goingRight = true;
//            %obj.rotation = 0;
            %obj.setDML("scriptsAndAssets/data/ui/minigame/swat/Ant_AliveL.png");
         }
      }
   }

   // Update position of each ant on ground
   for(%j=0; %j<%this.antGroundNum; %j++)
   {
      if(%this.antGroundIdx[%j] $= "")
         continue;
         
      %obj  = ("AntGround"@%this.antGroundIdx[%j]);
      if(%obj.isHit)
         continue;
      
      %posX = %this.lerp(%obj.origX*%winScaleX, %obj.destX*%winScaleX, %obj.t);
      %posY = %this.lerp(%obj.origY*%winScaleY, %obj.destY*%winScaleY, %obj.t);
      %obj.setPosition(%posX, %posY);

      %obj.t += 0.004;
      if(%obj.t >= 1)
         %this.killAntGround(%j);
   }
   
   %this.isIdle = false;
   
   scheduleObjLCA(%this, "Minigame", 33, "onIdle", 0);   
}

// Kill a spiderweb
function InsectMinigame::killSpiderWeb(%this, %i)
{
   %obj  = ("SpiderWeb"@%this.spiderWebIdx[%i]);
   %obj2 = ("SpiderString"@%this.spiderWebIdx[%i]);   
   %this.spiderWebIdx[%i] = "";
   SpiderWebs.remove(%obj);
   SpiderWebs.remove(%obj2);
   %obj.delete();
   %obj2.delete();   
}

// Kill an antCounter1
function InsectMinigame::killAntCounter1(%this, %i)
{
   %obj  = ("AntCounter1"@%this.antCounter1Idx[%i]);
   %this.antCounter1Idx[%i] = "";
   AntsCounter1.remove(%obj);
   %obj.delete();
}

// Kill an antCounter2
function InsectMinigame::killAntCounter2(%this, %i)
{
   %obj  = ("AntCounter2"@%this.antCounter2Idx[%i]);
   %this.antCounter2Idx[%i] = "";
   AntsCounter2.remove(%obj);
   %obj.delete();
}

// Kill an antGround
function InsectMinigame::killAntGround(%this, %i)
{
   %obj  = ("AntGround"@%this.antGroundIdx[%i]);
   %this.antGroundIdx[%i] = "";
   AntsGround.remove(%obj);
   %obj.delete();
}

// Score feedback
function InsectMinigame::updateScore(%this, %scoreToAdd)
{
   %this.score += %scoreToAdd;
   if(%this.score >= 6)
   {
      %this.score -= 6;
      InsectGui.scoreMatch(1);
   }
//   %fontSize = %this.scoreFontSize * Canvas.scaleSize;
//   %text = %this.score;
//   InsectScoreWindowText.setText(%text);
}

// Update swatter state
//function InsectMinigame::updateSwatter(%this)
//{
//   %this.swatterWasUsed = false;
//   Swatter.setBitmap("scriptsAndAssets/data/ui/minigame/swat/swatter1.png");   
//}

// Swatter Animation
function InsectMinigame::swatterAnimation(%this)
{
   if(%this.swatterCurrentImage == 5){
      %this.swatterCurrentImage = 1;
      Swatter.setDML("scriptsAndAssets/data/ui/minigame/swat/swatter/swatter_01.png");
      %this.swatterIsPlaying = false;
      return;
   }
   %this.swatterCurrentImage++;

   Swatter.setDML("scriptsAndAssets/data/ui/minigame/swat/swatter/swatter_0"@%this.swatterCurrentImage@".png");

   scheduleObjLCA(%this, "Minigame", 40, "swatterAnimation", 0);
}

// Callback of left mouse button click
function InsectMinigame::onClick(%this, %mouseX, %mouseY)
{
   //animate swatter
   if(!%this.swatterIsPlaying)
   {
      %this.swatterIsPlaying = true;
      sfxPlay(SwingSoundSwatMinigame);
      scheduleObjLCA(%this, "Minigame", 0, "swatterAnimation", 0);      
   }
   
   %HitSomething = false;
   
   if(%this.isConfettiAnimating || %this.isEnding || %this.swatterWasUsed)
      return;
  
   // Get mouse coordinates
//   %curMousePos = canvas.getCursorPos();
//   %mouseX = getWord(%curMousePos,0);
//   %mouseY = getWord(%curMousePos,1);
   
   // Compensate widescreen offset
   %rect2 = MinigameRefCenter.getPosition();
   %mouseX -= getWord(%rect2,0);
   %mouseY -= getWord(%rect2,1);
   
   // First rectangle
   %l1 = %mouseX - getWord(Swatter.Extent,0)/2;
   %t1 = %mouseY - getWord(Swatter.Extent,1)/4;
   %r1 = %mouseX + getWord(Swatter.Extent,0)/2;
   %b1 = %mouseY + getWord(Swatter.Extent,1)/4;
   
   %l1 += getWord(Swatter.Extent,0)/5;
   %r1 -= getWord(Swatter.Extent,0)/5;
   %t1 += getWord(Swatter.Extent,1)/9;
   
   %winScaleX = 1.0 / 800 * getWord(MinigameRefCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(MinigameRefCenter.getExtent(),1);   
   
   // Check spiders in string
   for(%j=0; %j<%this.spiderWebNum; %j++)
   {
      if(%this.spiderWebIdx[%j] $= "")
         continue;

      %obj = ("SpiderWeb"@%this.spiderWebIdx[%j]);
      if(%obj.hit > 4)
         continue;
      
      %pos = %obj.Position;
      %dim = %obj.Extent;
      
      %marginX = getWord(%dim, 0) / 4;
      %marginY = getWord(%dim, 1) / 4;
      
      %l2 = getWord(%pos, 0) + %marginX;
      %t2 = getWord(%pos, 1) + %marginY;
      %r2 = %l2 + getWord(%dim, 0) - %marginX;
      %b2 = %t2 + getWord(%dim, 1) - %marginY;
      
      if(%this.intersectRect(%l1, %t1, %r1, %b1, %l2, %t2, %r2, %b2))
      {
         %obj.stop();
         %obj.hit++;
         %HitSomething = true;
         
         if(%obj.hit >= 5)
         {
            %obj.setDML("scriptsAndAssets/data/ui/minigame/swat/spider_dead.png");

            // Centerize splat
            %cx = %l2 + getWord(%dim, 0)/2;
            %cy = %t2 + getWord(%dim, 1)/2;
            %obj.setPosition(%cx-(224/2)*%winScaleX, %cy-(190/2)*%winScaleY);
            %obj.setExtent(224, 190);         

            %pos = %obj.getPosition();
            %dim = %obj.getExtent();
            
            %obj.animateCenter(getWord(%pos, 0)+(getWord(%dim, 0)/2), 600*%winScaleY, 0, 750, "1 2");
            
            %obj2 = ("SpiderString"@%this.spiderWebIdx[%j]);
            %obj2.animateAlpha(0, 0, 750, "1 2");
            
            //sfxPlay(HitSpiderSoundSwatMinigame);
            %this.playSfxHit();
			
            scheduleObjLCA(%this, "Minigame", 750, "killSpiderWeb", 1, %j);
            
            %this.updateScore(6);
         }
         else
         {
            %obj2 = ("SpiderString"@%this.spiderWebIdx[%j]);            
            //%obj.setAlpha(0.75);
            //%obj.setAlpha(0.75);
            %obj.setDML("scriptsAndAssets/data/ui/minigame/swat/spider_string_hit.png");
            
            //sfxPlay(FirstHitSpiderSoundSwatMinigame);
			%this.playSfxHit();
         }
      }
   }
   
   // Check ants in counter1
   for(%j=0; %j<%this.antCounter1Num; %j++)
   {
      if(%this.antCounter1Idx[%j] $= "")
         continue;

      %obj = ("AntCounter1"@%this.antCounter1Idx[%j]);
      if(%obj.isHit)
         continue;
      
      %pos = %obj.Position;
      %dim = %obj.Extent;
           
      %l2 = getWord(%pos, 0);
      %t2 = getWord(%pos, 1);
      %r2 = %l2 + getWord(%dim, 0);
      %b2 = %t2 + getWord(%dim, 1);
      
      if(%this.intersectRect(%l1, %t1, %r1, %b1, %l2, %t2, %r2, %b2))
      {
         %obj.stop();
         %obj.isHit = true;
         %obj.setDML("scriptsAndAssets/data/ui/minigame/swat/ant_dead.png");

         // Centerize splat
         %cx = %l2 + getWord(%dim, 0)/2;
         %cy = %t2 + getWord(%dim, 1)/2;
         %obj.setPosition(%cx-(96/2)*%winScaleX, %cy-(47/2)*%winScaleY);
         %obj.setExtent(96, 47);
         
         %obj.animateAlpha(0, 0, 750, "1 2");
         scheduleObjLCA(%this, "Minigame", 750, "killAntCounter1", 1, %j);
         
         //sfxPlay(HitSpiderSoundSwatMinigame);
		 %this.playSfxHit();
		 
         %HitSomething = true;
         
         %this.updateScore(2);
      }
   }

   // Check ants in counter2
   for(%j=0; %j<%this.antCounter2Num; %j++)
   {
      if(%this.antCounter2Idx[%j] $= "")
         continue;

      %obj = ("AntCounter2"@%this.antCounter2Idx[%j]);
      if(%obj.isHit)
         continue;
      
      %pos = %obj.Position;
      %dim = %obj.Extent;
           
      %l2 = getWord(%pos, 0);
      %t2 = getWord(%pos, 1);
      %r2 = %l2 + getWord(%dim, 0);
      %b2 = %t2 + getWord(%dim, 1);
      
      if(%this.intersectRect(%l1, %t1, %r1, %b1, %l2, %t2, %r2, %b2))
      {
         %obj.stop();
         %obj.isHit = true;
         %obj.setDML("scriptsAndAssets/data/ui/minigame/swat/ant_dead.png");
         
         // Centerize splat
         %cx = %l2 + getWord(%dim, 0)/2;
         %cy = %t2 + getWord(%dim, 1)/2;
         %obj.setPosition(%cx-(96/2)*%winScaleX, %cy-(47/2)*%winScaleY);
         %obj.setExtent(96, 47);         
         
         %obj.animateAlpha(0, 0, 750, "1 2");
         scheduleObjLCA(%this, "Minigame", 750, "killAntCounter2", 1, %j);
         
         //sfxPlay(HitSpiderSoundSwatMinigame); 
		 %this.playSfxHit();
		 
         %HitSomething = true;
         
         %this.updateScore(2);
      }
   }
   
   // Check ants on ground
   for(%j=0; %j<%this.antGroundNum; %j++)
   {
      if(%this.antGroundIdx[%j] $= "")
         continue;

      %obj = ("AntGround"@%this.antGroundIdx[%j]);
      if(%obj.isHit)
         continue;
      
      %pos = %obj.Position;
      %dim = %obj.Extent;
           
      %l2 = getWord(%pos, 0);
      %t2 = getWord(%pos, 1);
      %r2 = %l2 + getWord(%dim, 0);
      %b2 = %t2 + getWord(%dim, 1);
      
      if(%this.intersectRect(%l1, %t1, %r1, %b1, %l2, %t2, %r2, %b2))
      {
         %obj.stop();
         %obj.isHit = true;
         %obj.setDML("scriptsAndAssets/data/ui/minigame/swat/ant_dead.png");
         
         // Centerize splat
         %cx = %l2 + getWord(%dim, 0)/2;
         %cy = %t2 + getWord(%dim, 1)/2;
         %obj.setPosition(%cx-(96/2)*%winScaleX, %cy-(47/2)*%winScaleY);
         %obj.setExtent(96, 47);         
         
         %obj.animateAlpha(0, 0, 750, "1 2");
         scheduleObjLCA(%this, "Minigame", 750, "killAntGround", 1, %j);
         
         //sfxPlay(HitSpiderSoundSwatMinigame);
		 %this.playSfxHit();
		 
         %HitSomething = true;
         
         %this.updateScore(2);
      }
   }
   
   if(!%HitSomething){
      sfxPlay(MissSoundSwatMinigame);
   }
}

function InsectMinigame::playSfxHit(%this)
{
	sfxPlay(%this.HitSoundString @ %this.hitSfxIteration);
	%this.hitSfxIteration = (%this.hitSfxIteration % 4 )+ 1;
}

// Spawn spider web
function InsectMinigame::spawnSpiderWeb(%this)
{
   if(%this.isIdle)
      return;
      
   if(%this.isConfettiAnimating || %this.isEnding)
      return;
   
   %winScaleX = 1.0 / 800 * getWord(MinigameRefCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(MinigameRefCenter.getExtent(),1);
   
   // Width and height of the object to be spawned
   %objWidth = 200;
   %objHeight = 150;
   %scale = 0.5 + (getRandom(0, 1000) / 1000);
   %scale *= 0.7;

   %obj = new GuiPlasticBitmapCtrl("SpiderWeb"@%this.newSpiderWebIdx) {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      Profile = "GuiContentProfile";
      HorizSizing = "relative";
      VertSizing = "relative";
      MinExtent = "2 2";
      canSave = "1";
      Visible = "0";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      useVariable = "0";
      tile = "0";      
      rotation = "0";
      deltaRotation = "0";
      noClip = "0";
      DML = "scriptsAndAssets/data/ui/minigame/swat/spider_string.png";
   };
   
   %obj2 = new GuiPlasticBitmapCtrl("SpiderString"@%this.newSpiderWebIdx) {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      Profile = "GuiContentProfile";
      HorizSizing = "relative";
      VertSizing = "relative";
      MinExtent = "2 2";
      canSave = "1";
      Visible = "0";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      useVariable = "0";
      tile = "0";      
      rotation = "0";
      deltaRotation = "0";
      noClip = "0";
      DML = "scriptsAndAssets/data/ui/minigame/swat/string.png";
   };
   
   %addItem = true;
   
   for(%j=0; %j<%this.spiderWebNum; %j++)
   {
      if(%this.spiderWebIdx[%j] $= "")
      {
         %obj = ("SpiderWeb"@%this.newSpiderWebIdx);
         %this.spiderWebIdx[%j] = %this.newSpiderWebIdx;
         %addItem = false;
         break;
      }
   }
   
   %obj.hit = 0;
   
   SpiderWebs.add(%obj2);   
   SpiderWebs.add(%obj);
   
   // Choose initial position
   %width = %objWidth*%scale*%winScaleX;
   %height = %objHeight*%scale*%winScaleY;
   %posX = getRandom(0, (800 * %winScaleX)-%height);
   %posY = -%width;
   %obj.speed = getRandom(6.5,13); // 5,10
   %obj.t = getRandom(1000,5000) / 5000;
   %obj.t -= 0.2;
   %obj.scale = %scale;
   %obj.goingUp = false;
   
   %obj.setPosition(%posX, %posY);
   
   %obj.setExtent(%width, %height);   
   %obj.setVisible(true);
   
   %width2 = 20*%scale*%winScaleX;
   %height2 = 600*%scale*%winScaleY;
   %obj2.setPosition((%posX+%width/2) - %width2/2, %posY-%height2+%height/2);
   %obj2.setExtent(%width2, %height2);
   %obj2.setVisible(true);   
           
   if(%addItem)
   {
      %this.spiderWebIdx[%this.spiderWebNum] = %this.newSpiderWebIdx;
      %this.spiderWebNum++;
   }
      
   %this.newSpiderWebIdx++;
      
   // Respawn
   scheduleObjLCA(%this, "Minigame", %this.spiderWebSpawnInterval, "spawnSpiderWeb", 0);
}

// Spawn antsCounter1
function InsectMinigame::spawnAntCounter1(%this)
{
   if(%this.isIdle)
      return;
      
   if(%this.isConfettiAnimating || %this.isEnding)
      return;
   
   %winScaleX = 1.0 / 800 * getWord(MinigameRefCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(MinigameRefCenter.getExtent(),1);
   
   // Width and height of the object to be spawned
   %objWidth = 30;
   %objHeight = 30;

   %obj = new GuiPlasticBitmapCtrl("AntCounter1"@%this.newAntCounter1Idx) {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      Profile = "GuiContentProfile";
      HorizSizing = "relative";
      VertSizing = "relative";
      MinExtent = "2 2";
      canSave = "1";
      Visible = "0";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      useVariable = "0";
      tile = "0";      
      rotation = "0";
      deltaRotation = "0";
      noClip = "0";
      DML = "scriptsAndAssets/data/ui/minigame/swat/Ant_AliveR.png";
   };
      
   %addItem = true;
   
   for(%j=0; %j<%this.antCounter1Num; %j++)
   {
      if(%this.antCounter1Idx[%j] $= "")
      {
         %obj = ("AntCounter1"@%this.newAntCounter1Idx);
         %this.antCounter1Idx[%j] = %this.newAntCounter1Idx;
         %addItem = false;
         break;
      }
   }
   
   %obj.isHit = false;
   
   AntsCounter1.add(%obj);   
   
   // Choose initial position
   %width = %objWidth*%winScaleX;
   %height = %objHeight*%winScaleY;
   %posX = -40*%winScaleX;
   %posY = (339-100)*%winScaleY;
   
   %obj.t = 0;   
   %obj.goingRight = false;
   
   %obj.setPosition(%posX, %posY);
   %obj.setExtent(%width, %height);   
   %obj.setVisible(true);
             
   if(%addItem)
   {
      %this.antCounter1Idx[%this.antCounter1Num] = %this.newAntCounter1Idx;
      %this.antCounter1Num++;
   }
      
   %this.newAntCounter1Idx++;
      
   // Respawn
   scheduleObjLCA(%this, "Minigame", getRandom(1000, 2500), "spawnAntCounter1", 0);
}

// Spawn antsCounter2
function InsectMinigame::spawnAntCounter2(%this)
{
   if(%this.isIdle)
      return;
      
   if(%this.isConfettiAnimating || %this.isEnding)
      return;
   
   %winScaleX = 1.0 / 800 * getWord(MinigameRefCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(MinigameRefCenter.getExtent(),1);
   
   // Width and height of the object to be spawned
   %objWidth = 30;
   %objHeight = 30;

   %obj = new GuiPlasticBitmapCtrl("AntCounter2"@%this.newAntCounter2Idx) {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      Profile = "GuiContentProfile";
      HorizSizing = "relative";
      VertSizing = "relative";
      MinExtent = "2 2";
      canSave = "1";
      Visible = "0";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      useVariable = "0";
      tile = "0";      
      rotation = "0";
      deltaRotation = "0";
      noClip = "0";
      DML = "scriptsAndAssets/data/ui/minigame/swat/Ant_AliveR.png";
   };
      
   %addItem = true;
   
   for(%j=0; %j<%this.antCounter2Num; %j++)
   {
      if(%this.antCounter2Idx[%j] $= "")
      {
         %obj = ("AntCounter2"@%this.newAntCounter2Idx);
         %this.antCounter2Idx[%j] = %this.newAntCounter2Idx;
         %addItem = false;
         break;
      }
   }
   
   %obj.isHit = false;
   
   AntsCounter2.add(%obj);   
   
   // Choose initial position
   %width = %objWidth*%winScaleX;
   %height = %objHeight*%winScaleY;
   %posX = 265*%winScaleX;
   %posY = (419-100)*%winScaleY;
   
   %obj.t = 0;   
   %obj.goingRight = false;
   
   %obj.setPosition(%posX, %posY);
   %obj.setExtent(%width, %height);   
   %obj.setVisible(true);
             
   if(%addItem)
   {
      %this.antCounter2Idx[%this.antCounter2Num] = %this.newAntCounter2Idx;
      %this.antCounter2Num++;
   }
      
   %this.newAntCounter2Idx++;
      
   // Respawn
   scheduleObjLCA(%this, "Minigame", getRandom(2500, 5000), "spawnAntCounter2", 0);
}

// Spawn antsGround
function InsectMinigame::spawnAntGround(%this)
{
   if(%this.isIdle)
      return;
      
   if(%this.isConfettiAnimating || %this.isEnding)
      return;
   
   %winScaleX = 1.0 / 800 * getWord(MinigameRefCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(MinigameRefCenter.getExtent(),1);
   
   // Width and height of the object to be spawned
   %objWidth = 30;
   %objHeight = 30;

   %obj = new GuiPlasticBitmapCtrl("AntGround"@%this.newAntGroundIdx) {
      canSaveDynamicFields = "0";
      Enabled = "1";
      isContainer = "0";
      Profile = "GuiContentProfile";
      HorizSizing = "relative";
      VertSizing = "relative";
      MinExtent = "2 2";
      canSave = "1";
      Visible = "0";
      hovertime = "1000";
      alpha = "1";
      color = "255 255 255 255";
      unitClip = "0 0 1 1";
      useVariable = "0";
      tile = "0";      
      rotation = "0";
      deltaRotation = "0";
      noClip = "0";
      DML = "scriptsAndAssets/data/ui/minigame/swat/Ant_AliveR.png";
   };
      
   %addItem = true;
   
   for(%j=0; %j<%this.antGroundNum; %j++)
   {
      if(%this.antGroundIdx[%j] $= "")
      {
         %obj = ("AntGround"@%this.newAntGroundIdx);
         %this.antGroundIdx[%j] = %this.newAntGroundIdx;
         %addItem = false;
         break;
      }
   }
   
   %obj.isHit = false;
   
   AntsGround.add(%obj);   
   
   // Choose initial position
   %width = %objWidth*%winScaleX;
   %height = %objHeight*%winScaleY;

   if(getRandom(0,1))
   {
      %obj.origX = 206;
      %obj.origY = getRandom(560, 581)-90;
      %obj.destX = 840;
      %obj.destY = getRandom(467, 581)-90;
//      %obj.rotation = 180;
      %obj.setDML("scriptsAndAssets/data/ui/minigame/swat/Ant_AliveR.png");
   }
   else
   {
      %obj.origX = 840;
      %obj.origY = getRandom(467, 581)-90; 
      %obj.destX = 206;
      %obj.destY = getRandom(560, 581)-90;
//      %obj.rotation = 0;
      %obj.setDML("scriptsAndAssets/data/ui/minigame/swat/Ant_AliveL.png");
   }

   %obj.t = 0;
   %posX = %obj.origX*%winScaleX;
   %posY = %obj.origY*%winScaleY;  
   
   %obj.setPosition(%posX, %posY);
   %obj.setExtent(%width, %height);   
   %obj.setVisible(true);
             
   if(%addItem)
   {
      %this.antGroundIdx[%this.antGroundNum] = %this.newAntGroundIdx;
      %this.antGroundNum++;
   }
      
   %this.newAntGroundIdx++;
      
   // Respawn
   scheduleObjLCA(%this, "Minigame", getRandom(500, 1500), "spawnAntGround", 0);
}

function InsectMinigame::onTimeOut(%this)
{
   sfxPlay(VictorySoundSwatMinigame);
      
   %this.gameFinished = true;
   
   %this.quit();
}

// Callback for resizing events
function InsectMinigame::onResize(%this, %width, %height)
{
/*   // Force resizing
   %this.setExtent(getWord(Canvas.extent,0), getWord(Canvas.extent,1));
   //%this.updateScore();
   
   %fontSize = 36 * Canvas.scaleSize; if(%fontSize > 62) %fontSize = 62;   
   GuiCenteredBigTextProfile.fontSize = %fontSize;
   GuiLCAButtonProfile2.fontSize = %fontSize;
   
   InsectSkipButton.setProfile(GuiDefaultProfile);
   InsectSkipButton.setProfile(GuiLCAButtonProfile2);
*/   
   InsectGui.xScale = getWord(InsectGui.getExtent(), 0)/800;
   InsectGui.yScale =  getWord(InsectGui.getExtent(), 1)/600; 
   InsectGui.x0 = mRound(getWord(InsectGui.boardPos, 0) * InsectGui.xScale);
   InsectGui.y0 = mRound(getWord(InsectGui.boardPos, 1) * InsectGui.yScale);
   InsectGui.gemWidth = mRound(getWord(InsectGui.gemExtent, 0) * InsectGui.xScale);
   InsectGui.gemHeight = mRound(getWord(InsectGui.gemExtent, 1) * InsectGui.yScale);   
   
   Canvas.setWidescreen(MinigameRefCenter, 2000, true);
}

function InsectGui::init(%this)
{   
   %this.initSpecificGui();
}

function InsectSkipButton::onMouseEnter(%this){
   %this.setBitmap("scriptsAndAssets/data/ui/minigame/quit_rollover.png");
}

function InsectSkipButton::onMouseLeave(%this){
   %this.setBitmap("scriptsAndAssets/data/ui/minigame/quit.png");
}

function InsectSkipButton::onMouseDown(%this){
   %this.setBitmap("scriptsAndAssets/data/ui/minigame/quit_pressed.png");
}

