// Audio profiles for the dart minigame
new SFXProfile(AudioDartGunFire1)
{
   filename = "~/data/ui/minigame/spy/spy_fx_1_pistol_shot.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioDartGunFire2)
{
   filename = "~/data/ui/minigame/spy/muzzleloadshot.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioDartInsectHit1)
{
   filename = "~/data/ui/minigame/spy/SFX_bugdeaths1.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioDartInsectHit2)
{
   filename = "~/data/ui/minigame/spy/SFX_bugdeaths2.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioDartInsectHit3)
{
   filename = "~/data/ui/minigame/spy/SFX_bugdeaths3.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioDartInsectHit4)
{
   filename = "~/data/ui/minigame/spy/SFX_bugdeaths4.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioDartInsectHit5)
{
   filename = "~/data/ui/minigame/spy/SFX_bugdeaths5.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioDartFired)
{
   filename = "~/data/ui/minigame/spy/spy_sfx_dart1";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioDartMissed)
{
   filename = "~/data/ui/minigame/spy/bulletricochet.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioDartMissed)
{
   filename = "~/data/ui/minigame/spy/bulletricochet.wav";
   description = LCASoundEffects;
   preload = true;
};

new SFXProfile(AudioDartHit)
{
   filename = "~/data/ui/minigame/spy/explode.mov";
   description = LCASoundEffects;
   preload = true;
};

// Called when the dart minigame GUI awakens
function DartMinigame::onWake(%this)
{
   // Please move the code here to start()
   
   // Force resizing
   %this.setExtent(getWord(Canvas.extent,0), getWord(Canvas.extent,1));
}

// Called by miniGame.cs
function DartMinigame::start()
{
   $DartsNumber    = 5;
   
   $DartScore      = 0;
   $DartHitPlayer  = 0;
   $DartsCurThrown = 0;
   $DartsMaxThrown = 50;
   $DartsCurMoving = 0;
   
   $DartsIsEnding  = false;

   $DartCurInsect  = -1;   
   
   $DartTimerCounter = 60;
   $DartTimerEventID = scheduleLCA("DartMinigame", 500, 0, "DartMinigame::onGameTimer", 0); // Called each half sec 

   DartMinigame::UpdateScore();
  
   DartMinigame::DartStartMinigame();   
}

// Called when exiting minigame
function DartMinigame::onSleep(%this)
{
   cancelSchedulesLCA("DartMinigame");
}

// Exit the minigame
function DartMinigame::quit()
{
   if($DartsIsEnding)
      return;
      
   Canvas.showCursor();      
      
   // Avoid new schedules
   $DartsIsEnding = true;
   
   MiniGame.playClapperBoardScoreIn();
}

// Score feedback
function DartMinigame::UpdateScore()
{   
//   DartRemainingText.setText("Remaining:" SPC $DartsMaxThrown - $DartsCurThrown);
   DartScoreWindowText.setText($DartScore * 100);
//   DartMissedText.setText("Missed:" SPC $DartHitPlayer);
}

// Update the cross hair GUI control according to current mouse cursor position
function DartMinigame::UpdateCrossHair()
{
   %cursorPos = Canvas.getCursorPos();
   DartCrossHair.setPosition(
      getWord(%cursorPos,0) - getWord(DartCrossHair.Extent,0)/2,
      getWord(%cursorPos,1) - getWord(DartCrossHair.Extent,1)/2);
   DartCrossHair.Visible = true;   
   scheduleLCA("DartMinigame", 33, 0, "DartMinigame::UpdateCrossHair", 0);
}

// Start the dart minigame
function DartMinigame::DartStartMinigame()
{
   Canvas.hideCursor();   
   scheduleLCA("DartMinigame", 33, 0, "DartMinigame::UpdateCrossHair", 0);

   // Throw our darts
   for(%i=1; %i<=$DartsNumber; %i++)
   {
      $DartsCurMoving++;      
      scheduleLCA("DartMinigame", getRandom(200,5000), 0, "DartMinigame::DartCreateNew", 2, %i, getRandom(50,100));
   }
   
   // Create insect
   scheduleLCA("DartMinigame", getRandom(2000,10000), 0, "DartMinigame::DartCreateNewInsect", 1, getRandom(1,4));
}

// Called when a new insect is created and starts animating
function DartMinigame::DartCreateNewInsect(%idx)
{
   %obj = ("DartInsect"@%idx);
   if(!isObject(%obj))
      return;
      
   $DartCurInsect = %idx;
      
   %winScaleX = 1.0 / 800 * getWord(canvas.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(canvas.getExtent(),1);   
   
   %scale = 0.33;
   switch(%idx)
   {
      case 1: 
         %scale *= 1;
      case 2: 
         %scale *= 0.47;
      case 3: 
         %scale *= 0.32;
      case 4: 
         %scale *= 0.25;
   }
   
   %obj.DartIsASpider = getRandom(0,1);
   if(%obj.DartIsASpider $= 1)
   {
      %obj.TSpeed = 0.01;      
      %sizeX = 172 * %scale * %winScaleX;
      %sizeY = 126 * %scale * %winScaleY;
      %obj.setDML("scriptsAndAssets/data/ui/minigame/spy/spider.png");
   }
   else
   {
      %obj.TSpeed = 0.02;
      %sizeX = 249 * %scale * %winScaleX;
      %sizeY = 124 * %scale * %winScaleY;
      %obj.setDML("scriptsAndAssets/data/ui/minigame/spy/cockroach.png");
   }
   %obj.setExtent(%sizeX, %sizeY);
   DartInsect.setExtent(%sizeX, %sizeY);
     
   switch(%idx)
   {
      case 1: 
         %origX = 230 - %sizeX;
         %origY = 505 - %sizeY;
         %destX = 585;
      case 2: 
         %origX = 313 - %sizeX;
         %origY = 427 - %sizeY;
         %destX = 519;
      case 3: 
         %origX = 347 - %sizeX;
         %origY = 388 - %sizeY;
         %destX = 492;
      case 4: 
         %origX = 365 - %sizeX;
         %origY = 370 - %sizeY;
         %destX = 480;
   }
   %obj.setPosition(%origX, %origY);      
   DartInsect.setPosition(%origX, %origY);
   DartInsect.setVisible(true);
   
   %obj.alpha = "1";
   %obj.setVisible(true);
   %obj.DartInsectDuringHit = false;
   %obj.OrigX = %origX;
   %obj.OrigY = %origY;
   %obj.DestX = %destX;
   %obj.DestY = %origY;
   %obj.CurT = 0;
//   %obj.animateCenter(%destX+%sizeX/2, %origY+%sizeY/2, 0, %aniTime*1000, "0 0");
//   scheduleLCA("DartMinigame", %aniTime*1000, 0, 
//      "DartMinigame::onInsectHide", 1, %idx);

   scheduleLCA("DartMinigame", 50, 0, "DartMinigame::onInsectMove", 1, %idx);
}

// Called when an insect is moving
function DartMinigame::onInsectMove(%idx)
{
   %obj = ("DartInsect"@%idx);
   if(!isObject(%obj))
      return;
      
   if(%obj.DartInsectDuringHit)
      return;
      
   %obj.CurT += %obj.TSpeed;
   if(%obj.CurT >= 1)
   {
      DartMinigame::onInsectHide(%idx);
      return;
   }
  
   %newX = %obj.OrigX * (1-%obj.CurT) + %obj.DestX * %obj.CurT;
   %newY = %obj.OrigY * (1-%obj.CurT) + %obj.DestY * %obj.CurT;   
   %obj.setPosition(%newX, %newY);
   DartInsect.setPosition(%newX, %newY);
   
   scheduleLCA("DartMinigame", 50, 0, "DartMinigame::onInsectMove", 1, %idx);   
}

// Called when an insect was hit
function DartMinigame::onInsectHit(%idx)
{
   %obj = ("DartInsect"@%idx);
   if(!isObject(%obj))
      return;

   // Play a random bugs death sound      
   %num = getRandom(1,5);
   sfxPlayOnce(AudioDartInsectHit@%num);
   
   DartInsect.setVisible(false);      
      
   if(%obj.DartInsectDuringHit)
      return;
   %obj.DartInsectDuringHit = true;
   
   %winScaleX = 1.0 / 800 * getWord(canvas.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(canvas.getExtent(),1);   
   
   %scale = 0.33;
   %posY = 0;
   switch(%idx)
   {
      case 1: 
         %scale *= 1;
         %posY = 505;
      case 2: 
         %scale *= 0.47;
         %posY = 427;         
      case 3: 
         %scale *= 0.32;
         %posY = 388;         
      case 4: 
         %scale *= 0.25;
         %posY = 370;         
   }
   
   %curMousePos = canvas.getCursorPos();
   %mouseX = getWord(%curMousePos,0);
         
   if(%obj.DartIsASpider $= 1)
   {
      %sizeX = 321 * %scale * %winScaleX;
      %sizeY = 105 * %scale * %winScaleY;
      %obj.setDML("scriptsAndAssets/data/ui/minigame/spy/spider_splat.png");
      $DartScore += 2;
   }
   else
   {
      %sizeX = 230 * %scale * %winScaleX;
      %sizeY = 54 * %scale * %winScaleY;
      %obj.setDML("scriptsAndAssets/data/ui/minigame/spy/roach_splat.png");      
      $DartScore += 5;
   }
   %obj.setPosition(%mouseX-%sizeX/4, %posY-%sizeY);
 
   %obj.animateAlpha(0, 0, 3000, "0 0");
   scheduleLCA("DartMinigame", 3000, 0, "DartMinigame::onInsectHide", 1, %idx);   
}

// Called when an insect was hidden (after being hit or because it entered
// behind a pillar
function DartMinigame::onInsectHide(%idx)
{
   %obj = ("DartInsect"@%idx);
   %obj.setVisible(false);
   DartInsect.setVisible(false);   

   // Create new insect
   scheduleLCA("DartMinigame", getRandom(2000,10000), 0, 
      "DartMinigame::DartCreateNewInsect", 1, getRandom(1,4));
}

// Create a new dart
// %num = Number of the dart, between 1 and $DartsNumber
// %speed = Speed in ms/pixel
function DartMinigame::DartCreateNew(%num, %speed)
{
   if($DartsIsEnding $= true)
      return;
   
   %obj = ("Dart"@%num);

   // Determine start position and direction
   // We use a resolution of 800x600 and adjust it by the aspect of a new
   // resolution, when the window is resized during the minigame
   %screenWidth = 800;
   %screenHeight = 600;
//   %startX = getRandom(-100, 100) + %screenWidth/2;
//   %startY = getRandom(-50, 50) + %screenHeight/2;

   %imgNumber = getRandom(1,5);
   switch$(%imgNumber)
   {
      case 1: %startX = 288; %startY = 121;
      case 2: %startX = 540; %startY = 124;
      case 3: %startX = 346; %startY = 361;
      case 4: %startX = 488; %startY = 362;
      default: %startX = 394; %startY = 176;
   }

   %dirX = getRandom(-900, 900);
   %dirY = getRandom(-550, 550);
   %norm = mSqrt(%dirX*%dirX + %dirY*%dirY);
   %dirX /= %norm;
   %dirY /= %norm;
   %dirX *= 4;
   %dirY *= 4;

   // Determine end position
   %isInside = true;
   %t = 1;
   while(%isInside)
   {
      %t++;
      %x = %startX+%dirX*%t;
      if(%x < 0 || %x >= %screenWidth)
         %isInside = false;
      %y = %startY+%dirY*%t;
      if(%y < 0 || %y >= %screenHeight)
         %isInside = false;
   }
   %endX = %x;
   %endY = %y;

   // Set up dynamic fields
   %obj.OrigExtent = "406 498";
   %obj.StartPos = %startX SPC %startY;
   %obj.EndPos = %endX SPC %endY;
   %obj.DirVec = %dirX SPC %dirY;
   %obj.CurT = 1;
   %obj.TotalT = %t;
   %obj.IsDying = false;   
   %obj.IsExploding = false;
   %obj.setBitmap("scriptsAndAssets/data/ui/minigame/spy/dart");   

   // Audio feedback for dart firing
   sfxPlayOnce(AudioDartFired);   
 
   scheduleLCA("DartMinigame", %speed, 0, "DartMinigame::onDartMove", 2, %num, %speed);
}

// Called for updating a dart position
function DartMinigame::onDartMove(%num, %speed)
{
   if($DartsIsEnding $= true)
      return;
         
   if($DartsCurThrown >= $DartsMaxThrown)
   {
      DartMinigame::quit();
      return;
   }
  
   %obj = ("Dart"@%num);
   
   if(%obj.IsExploding $= true)
   {
      scheduleLCA("DartMinigame", %speed, 0, "DartMinigame::onDartMove", 2, %num, %speed);
      return;
   }
   
   if(%obj.IsDying $= true)
   {
      DartMinigame::onDartDied(%num);
      return;
   }

   // We use this to deal with resizing during the minigame   
   %winScaleX = 1.0 / 800 * getWord(canvas.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(canvas.getExtent(),1);   

   // Determine size of dart according to current t
   %maxScale = 1;
   %scale = %maxScale * %obj.CurT / %obj.TotalT;
   %a = getWord(%obj.OrigExtent,0) * %scale;
   %b = getWord(%obj.OrigExtent,1) * %scale;

   // Accelerate dart
   %accell = 1 + (%obj.CurT / %obj.TotalT) * 2;
   
   // Determine dart position
   %startX = getWord(%obj.StartPos,0);
   %startY = getWord(%obj.StartPos,1);
   %curX = %startX + getWord(%obj.DirVec,0) * %obj.CurT * %accell;
   %curY = %startY + getWord(%obj.DirVec,1) * %obj.CurT * %accell;
   %curX -= %a / 2;
   %curY -= %b / 2;
   %obj.setPosition(%curX * %winScaleX, %curY * %winScaleY);
   %obj.setExtent(%a * %winScaleX, %b * %winScaleY);

   // Next step
   %obj.CurT++;

   %screenWidth = 800;
   %screenHeight = 600;
   if(%curX+%a/2 < 0 || %curX+%a/2 >= %screenWidth || 
      %curY+%b/2 < 0 || %curY+%b/2 >= %screenHeight)
   {
      DartMinigame::onDartHitPlayer(%num);
      return;
   }

   %obj.Visible = true;

   scheduleLCA("DartMinigame", %speed, 0, "DartMinigame::onDartMove", 2, %num, %speed);
}

// Called when a dart hits the player
function DartMinigame::onDartHitPlayer(%num)
{
   // Audio feedback for missed dart
   sfxPlayOnce(AudioDartMissed);   
   
   $DartHitPlayer++;
   $DartsCurMoving--;   
   $DartsCurThrown++;
   DartMinigame::UpdateScore();   
   DartMinigame::onDartDied(%num);
}

function DartMinigame::onBackgroundHit()
{
   // Audio feedback
   %t = getRandom(1,2);
   switch(%t)
   {
      case 1: sfxPlayOnce(AudioDartGunFire1);
      case 2: sfxPlayOnce(AudioDartGunFire2);
   }
}

// Called when the player hits the dart
function DartMinigame::onDartPlayerHit(%num)
{
   sfxPlayOnce(AudioDartHit);

   $DartScore++;
   $DartsCurMoving--;   
   $DartsCurThrown++;
   DartMinigame::UpdateScore();
   %obj = ("Dart"@%num);
   %obj.setBitmap("scriptsAndAssets/data/ui/minigame/spy/dart_shot");
   scheduleLCA("DartMinigame", 1000, 0, "DartMinigame::onDartExploded", 1, %num);   
   %obj.IsExploding = true;
}

// Called when a dart finishes exploding
function DartMinigame::onDartExploded(%num)
{
   %obj = ("Dart"@%num);   
   %obj.IsDying = true;
   %obj.Visible = false;
   %obj.IsExploding = false;   
}

// Called when a dart has died (hit a player or was hit)
function DartMinigame::onDartDied(%num)
{
   %obj = ("Dart"@%num);
   %obj.Visible = false;
   
   if($DartsCurThrown >= $DartsMaxThrown)
   {
      DartMinigame::UpdateScore();
      DartMinigame::quit();
   }
   else
   if($DartsCurMoving < $DartsMaxThrown - $DartsCurThrown)
   {
      DartMinigame::UpdateScore();
      $DartsCurMoving++;      
      scheduleLCA("DartMinigame", getRandom(200,5000), 0, "DartMinigame::DartCreateNew", 2, %num, getRandom(50,100));
   }
}

// Update game countdown timer
function DartMinigame::onGameTimer()
{
   // Blink colon
   DartTimerColon.Visible = !DartTimerColon.Visible;
   
   // The colon toogles visibility at each second
   if(DartTimerColon.Visible)
   {
      $DartTimerCounter--;
      
      if($DartTimerCounter < 0)
      {
         DartMinigame::quit();
         return;
      }
      else
      {
         // Update countdown timer
         %minute = mFloor($DartTimerCounter / 60);
         DartTimerDigit1.bitmap = "scriptsAndAssets/data/ui/ho/timer/"@%minute@".png";
      
         %second = mRound((($DartTimerCounter / 60) - %minute) * 60);
         %digit2 = mFloor(%second / 10); // decimal digit
         %digit3 = mRound(((%second / 10) - %digit2) * 10); // unitary digit
      
         DartTimerDigit2.bitmap = "scriptsAndAssets/data/ui/ho/timer/"@%digit2@".png";
         DartTimerDigit3.bitmap = "scriptsAndAssets/data/ui/ho/timer/"@%digit3@".png";
      }
   }
   
   // Re-schedule event
   if($DartTimerCounter >= 0)
      scheduleLCA("DartMinigame", 500, 0, "DartMinigame::onGameTimer", 0);
}
