// Initialize variables related to the mini game
//function Minigame::initialize(%this)
function Minigame::onWake(%this)
{
   %this.miniGameState = 0;
   %this.isRunning = false;
   %this.hintsGained = 0;
   
   // Force resizing
   %this.setExtent(getWord(Canvas.extent,0), getWord(Canvas.extent,1));   
   
   Canvas.setWidescreen(MinigameCenter, 0, false);   
}

function Minigame::onResize(%this, %width, %height)
{
   Canvas.setWidescreen(MinigameCenter, 2000, true);
}

function Minigame::start(%this)
{
   if(!SceneGui.inIntro && !SceneGui.inOutro)
   {
      // Schedule mini game triggering
      if(isObject(MissionInfo))
      {
         %minigameTriggerTime = MissionInfo.miniGameTriggerTime;
         
         //%minigameTriggerTime = 0;  // Uncomment this to trigger minigame at the start of the scene
         if(%minigameTriggerTime > 0) // Comment this to trigger minigame at the start of the scene
            scheduleObjLCA(%this, "HO", %minigameTriggerTime*1000, "play", 0);
      }
   }   
}

// Add minigame GUI control to Minigame control
function MiniGame::addMinigame(%this)
{
   switch$($CurSceneName)
   {
      case "SpyLevel":
         if($CurTake $= 1)
            %obj = InsectMinigame;                  
         else if($CurTake $= 2)
            %obj = SliderMinigame;
         else if($CurTake $= 4)
            %obj = InsectMinigame;                  
      case "GangsterLevel":
         if($CurTake $= 2)
            %obj = BejeweledMinigame;            
         else if($CurTake $= 3)
            %obj = MazeMinigame;            
      case "KungFuLevel":
         if($CurTake $= 1)
            %obj = PicResortMinigame;
         else if($CurTake $= 3)
            %obj = MazeMinigame;
         else if($CurTake $= 4)
            %obj = MazeMinigame;
      case "DinosaurLevel":
         if($CurTake $= 2)
            %obj = PipeMinigame;
         else if($CurTake $= 3)
            %obj = MazeMinigame;
         else if($CurTake $= 4)
            %obj = MazeMinigame;
      case "WesternLevel":
         if ($CurTake $= 2)
            %obj = SimonMinigame;
         else if($CurTake $= 4)
            %obj = SliderMinigame;
      case "SciFiLevel":
         if($CurTake $= 1)
            %obj = BejeweledMinigame;
         if($CurTake $= 3)
            %obj = BejeweledMinigame;            
         if($CurTake $= 4)
            %obj = PicResortMinigame;
      case "MusicalLevel":
         if($CurTake $= 1)
            %obj = SimonMinigame;
         if($CurTake $= 4)
            %obj = SimonMinigame;
      case "MonsterLevel":
         if($CurTake $= 1)
            %obj = PunchMinigame;
         if($CurTake $= 3)
            %obj = PunchMinigame;      
         if($CurTake $= 4)
            %obj = MazeMinigame;
   }
   if(isObject(%obj))
   {
      Minigame.add(%obj);
      Minigame.bringToFront(%obj);
      
//      MinigameCenter.setVisible(false);
//      Minigame.pushToBack(MinigameCenter);
   }
   
   if(!SceneGui.inIntro)
      scheduleObjLCA(%this, "HO", 100, "playClapperBoardTutOut", 0);   
}

// Remove minigame GUI control from Minigame control
function MiniGame::removeMinigame(%this)
{
//   if(isObject(KeypadMinigame))    %obj = KeypadMinigame;
   if(isObject(InsectMinigame))    %obj = InsectMinigame;
   if(isObject(SliderMinigame))    %obj = SliderMinigame;
   if(isObject(BejeweledMinigame)) %obj = BejeweledMinigame;
   if(isObject(MazeMinigame))      %obj = MazeMinigame;
   if(isObject(PicResortMinigame)) %obj = PicResortMinigame;
   if(isObject(PipeMinigame))      %obj = PipeMinigame;
   if(isObject(SimonMinigame))     %obj = SimonMinigame;   
   if(isObject(PunchMinigame))     %obj = PunchMinigame;
   
   if(isObject(%obj))
   {
      %this.remove(%obj);
      %obj.delete();
   }
}

// Pause HO game before starting the minigame
function Minigame::pauseHOGame()
{
   // Save camera settings
   %this.cam1UseOverlay = Cam1.useOverlayColor;
   %this.cam2UseOverlay = Cam2.useOverlayColor;
   %this.cam3UseOverlay = Cam3.useOverlayColor;
   %this.cam4UseOverlay = Cam4.useOverlayColor;
   
   %this.cam1OverlayColor = Cam1.overlayColor;
   %this.cam2OverlayColor = Cam2.overlayColor;
   %this.cam3OverlayColor = Cam3.overlayColor;
   %this.cam4OverlayColor = Cam4.overlayColor;
   
   SceneGui.stopStaticCamAni(1);
   SceneGui.stopStaticCamAni(2);
   SceneGui.stopStaticCamAni(3);
   SceneGui.stopStaticCamAni(4);   

   SceneGui.pauseAudio();         
   pauseAnims();   
   SceneGui.pauseTriggerCtrl();
   
   // New camera settings
   Cam1.useOverlayColor = true;
   Cam2.useOverlayColor = true;
   Cam3.useOverlayColor = true;
   Cam4.useOverlayColor = true;

   Cam1.setOverlayColor(0.5, 0.5, 0.5, 1);
   Cam2.setOverlayColor(0.5, 0.5, 0.5, 1);
   Cam3.setOverlayColor(0.5, 0.5, 0.5, 1);
   Cam4.setOverlayColor(0.5, 0.5, 0.5, 1);

   pauseSchedulesLCA("HO");
   
   // Prevent HO game GUI from becoming clickable during pause
   PauseDummy.setVisible(true);
  
   // Pause elapsed time
   %this.elapsedTimePause = getRealTime();
}

// Resume the HO game after the minigame
function Minigame::resumeHOGame()
{
   resumeSchedulesLCA("HO");   
   
   // Resume elapsed time
   SceneGui.timeElapsedBase += getRealTime()-%this.elapsedTimePause;
   
   // Make the HO game GUI clickable again
   PauseDummy.setVisible(false);

   // Restore camera settings
   Cam1.useOverlayColor = %this.cam1UseOverlay;
   Cam2.useOverlayColor = %this.cam2UseOverlay;
   Cam3.useOverlayColor = %this.cam3UseOverlay;
   Cam4.useOverlayColor = %this.cam4UseOverlay;   

   Cam1.setOverlayColor(getWord(%this.cam1OverlayColor,0), getWord(%this.cam1OverlayColor,1), getWord(%this.cam1OverlayColor,2), getWord(%this.cam1OverlayColor,3));
   Cam2.setOverlayColor(getWord(%this.cam2OverlayColor,0), getWord(%this.cam2OverlayColor,1), getWord(%this.cam2OverlayColor,2), getWord(%this.cam2OverlayColor,3));
   Cam3.setOverlayColor(getWord(%this.cam3OverlayColor,0), getWord(%this.cam3OverlayColor,1), getWord(%this.cam3OverlayColor,2), getWord(%this.cam3OverlayColor,3));
   Cam4.setOverlayColor(getWord(%this.cam4OverlayColor,0), getWord(%this.cam4OverlayColor,1), getWord(%this.cam4OverlayColor,2), getWord(%this.cam4OverlayColor,3));
   
   SceneGui.resumeTriggerCtrl();
   resumeAnims();
   SceneGui.resumeAudio();
}

// Play the minigame
function Minigame::play(%this)
{  
   %this.isRunning = true;
   %this.hintsGained = 0;
   
   // Pause game
   if(!SceneGui.inIntro && !SceneGui.inOutro)
      %this.pauseHOGame();

   if(SceneGui.inIntro)
   {
      %this.execMinigame();
      %this.addMinigame(%this);
   }
   else
   if(!SceneGui.inOutro && SceneGui.dirBehaviorMinigameIntro $= true)
   {
      %this.setVisible(false);
      SceneGui.showDirAni1(10000, SceneGui.dirTrigger[10000,"type",0], true, false);
   }
   else
   {
      %this.setVisible(true);
      %this.playClapperBoardTutIn();
   }
}

// Show score in score clapper board
function Minigame::playScore(%this)
{
   // Increase number of minigame stars gained
   %curMiniStars = PlayerProfile.getFieldValue("StarsMini"@$CurPlayerNum);   
   PlayerProfile.setFieldValue("StarsMini"@$CurPlayerNum, %curMiniStars + %this.hintsGained);
   
   // Add hints (cap to 20)
   if(PlayMinigameDlg.isPlayingMinigame !$= true)
   {
      %curHints = PlayerProfile.getFieldValue("PlayerHints"@$CurPlayerNum);
      if(%curHints < 20)
      {
         if(%curHints + %this.hintsGained > 20)
            %this.hintsGained -= ((%curHints + %this.hintsGained) - 20);
         HintSystem.addHints(%this.hintsGained);
      }
   }
   
   SceneGui.scoreMinigame = 1000;   

//   MiniGameScore.setText("<just:center><font:Comic Sans MS:62><color:FFFFFF>"@%this.hintsGained);   
//   MiniGameScore.setVisible(true);
   
   scheduleObjLCA(%this, "HO", 100, "beforeQuit", 0);

   Minigame.playClapperBoardScoreOut();
//   ClapperOK.setVisible(true);
}

function Minigame::beforeQuit(%this)
{   
   MiniGame.removeMinigame();
}

// Exit minigame
function Minigame::quit(%this)
{
   if(SceneGui.inOutro)
      OutroGuiCenter.setVisible(true);   
   
   // Resume HO game
   if(!SceneGui.inOutro)
   {
      SceneGui.inIntro = false;
      SceneGui.resumeHOGame();
   }
        
   MiniGame.setVisible(false);   
   
   %this.isRunning = false;
   
   cancelSchedulesLCA("Minigame");
}

function Minigame::playClapperBoardCloseSound(%this)
{
   sfxPlay(AudioClapperBoardClose);
}

// Trigger playing of clapper board tutorial intro
function Minigame::playClapperBoardTutIn(%this)
{
   sfxPlay(AudioClapperBoardMoving);
   
   scheduleObjLCA(%this, "Minigame", 1600, "playClapperBoardCloseSound", 0);

   %this.miniGameState = 1;
   
   %this.execMinigame();
   
   %path = "scriptsAndAssets/data/ui/clapperboard/clapper.jpg";
   Clapper.setDML(%path); 
   Clapper.play(1); 
   Clapper.setVisible(true);
   
   %path = "scriptsAndAssets/data/ui/clapperboard/minigame/" @ $CurSceneName @ "_take" @ $CurTake @ ".jpg";
   Board.setDML(%path);     
   Board.play(1);
   Board.setVisible(true);
}

// Trigger playing of clapper board tutorial outro
function Minigame::playClapperBoardTutOut(%this)
{
   sfxPlay(AudioClapperBoardMoving);
   
   %this.miniGameState = 2;   
   
   Clapper.playRev(1);
   Board.playRev(1);
}

// Trigger playing of clapper board score intro
function Minigame::playClapperBoardScoreIn(%this)
{
   %this.miniGameState = 3;
   
   Clapper.rewind();
   Clapper.play(1);
   Clapper.setVisible(true);   
   
   %path = "scriptsAndAssets/data/ui/clapperboard/minigame/Minigame_end.jpg";
   Board.setDML(%path);
   Board.rewind();   
   Board.play(1);
   Board.setVisible(true);   

   sfxPlay(AudioClapperBoardMoving);
   
   scheduleObjLCA(%this, "Minigame", 1600, "playClapperBoardCloseSound", 0);   
}

// Trigger playing of clapper board score outro
function Minigame::playClapperBoardScoreOut(%this)
{
   %this.miniGameState = 4;

//   MiniGameScore.setVisible(false);

   // Minigame was triggered during intro screen?
   if(SceneGui.inIntro)
   {
      SceneGui.clapperAniState = 0;
      // Play background noise   
//      sfxPlay(BkgndNoise@SceneGui.currentLevel);
      
      // Go to next director dialogue
//      IntroGui.showDirAniText3(IntroGui.triggerNum, IntroGui.textNum, 0);
   }
   
   // Minigame was triggered during outro screen?   
   if(SceneGui.inOutro)
   {
      // Play background noise   
//      sfxPlay(BkgndNoise@SceneGui.currentLevel);
      
      // Go to next director dialogue
//      OutroGui.showDirAniText3(OutroGui.triggerNum, OutroGui.textNum, 0);

      OutroGui.exitFromMinigame();
      return;
   }
   
   Clapper.playRev(1);
   Board.playRev(1);
   
   sfxPlay(AudioClapperBoardMoving);   
}

function Clapper::onStoppedAtEnd(%this)
{
   switch(Minigame.miniGameState)
   {
      case 1: // End of tutorial in animation
         ClapperStart.setVisible(true);
         
      case 3: // End of score in animation
         MinigameParticleFx.exit();      
         MiniGame.playScore();
   }
}

function Clapper::onStoppedAtStart(%this)
{
   if(SceneGui.clapperAniState)
   {
      switch(SceneGui.clapperAniState)
      {
         case 1: // End of first animation in sceneGui
            SceneGui.clapperAniState = 0;
            Clapper.setVisible(false);
            Board.setVisible(false);
            MiniGame.setVisible(false);
            SceneGui.resumeHOGame();
            Minigame.start();
         case 2: // Minigame at intro
            Minigame.startMinigame();
      }
   }
   else   
   switch(Minigame.miniGameState)
   {
      case 2: // End of tutorial out animation
         Minigame.startMinigame();
      
      case 4: // End of score out animation
         Clapper.setVisible(false);
         Board.setVisible(false);
         if(SceneGui.dirBehaviorMinigameOutro $= true)
         {
            Minigame.setVisible(false);
            SceneGui.showDirAni1(20000, SceneGui.dirTrigger[20000,"type",0], false, true);
         }
         else
            Minigame.quit();
   }
}

// OK button of clapper board was pressed
/*function ClapperOK::onClick(%this)
{
   sfxPlay(AudioButtonPress); //Button Press Sound
   
   ClapperOK.setVisible(false);
   Minigame.playClapperBoardScoreOut();   
}*/

function Minigame::onClapperStart(%this)
{
   sfxPlay(AudioButtonPress); //Button Press Sound
      
   %this.addMinigame();
   ClapperStart.setVisible(false);
}

// Run the minigame scripts but don't actually start it
// To do so, call Minigame::startMinigame
function Minigame::execMinigame(%this)
{  
   switch$($CurSceneName)
   {
      case "SpyLevel": // Spy level
         if($CurTake $= 1)
         {
            exec("scriptsAndAssets/client/ui/InsectMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/InsectMinigame.cs");
         }
         else if($CurTake $= 2)
         {
            exec("scriptsAndAssets/client/ui/SliderMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/SliderMinigame.cs");
         }
         else if($CurTake $= 4)
         {
            exec("scriptsAndAssets/client/ui/InsectMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/InsectMinigame.cs");            
         }
      case "GangsterLevel": // Gangster level
         if($CurTake $= 2)
         {
            exec("scriptsAndAssets/client/ui/BejeweledMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/BejeweledMinigame.cs");
         }
         else if($CurTake $= 3)
         {
            exec("scriptsAndAssets/client/ui/MazeMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/MazeMinigame.cs");
         }
      case "KungFuLevel": // Kung Fu level
         if($CurTake $= 1) 
         {
            exec("scriptsAndAssets/client/ui/PicResortMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/PicResortMinigame.cs");
         }
         else if($CurTake $= 3)
         {
            exec("scriptsAndAssets/client/ui/MazeMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/MazeMinigame.cs");
         }         
         else if($CurTake $= 4)
         {
            exec("scriptsAndAssets/client/ui/MazeMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/MazeMinigame.cs");
         }         
      case "DinosaurLevel":
         if($CurTake $= 2)
         {
            exec("scriptsAndAssets/client/ui/PipeMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/PipeMinigame.cs");
         }
         else if($CurTake $= 3)
         {
            exec("scriptsAndAssets/client/ui/MazeMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/MazeMinigame.cs");
         }
         else if($CurTake $= 4)
         {
            exec("scriptsAndAssets/client/ui/MazeMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/MazeMinigame.cs");
         }
      case "WesternLevel":
         if ($CurTake $= 2)
         {
            exec("scriptsAndAssets/client/ui/SimonMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/SimonMinigame.cs");
         }
         if($CurTake $= 4)
         {
            exec("scriptsAndAssets/client/ui/SliderMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/SliderMinigame.cs");
         }
      case "SciFiLevel":
         if($CurTake $= 1)
         {
            exec("scriptsAndAssets/client/ui/BejeweledMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/BejeweledMinigame.cs");
         }
         else if($CurTake $= 3)
         {
            exec("scriptsAndAssets/client/ui/BejeweledMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/BejeweledMinigame.cs");
         }
         else if($CurTake $= 4)
         {
            exec("scriptsAndAssets/client/ui/PicResortMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/PicResortMinigame.cs");
         }
      case "MusicalLevel":
         if($CurTake $= 1)
         {
            exec("scriptsAndAssets/client/ui/SimonMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/SimonMinigame.cs");
         }
         else if($CurTake $= 4)
         {
            exec("scriptsAndAssets/client/ui/SimonMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/SimonMinigame.cs");
         }
      case "MonsterLevel":
         if($CurTake $= 1 || $CurTake $= 3)
         {
            exec("scriptsAndAssets/client/ui/PunchMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/PunchMinigame.cs");
         }      
         if($CurTake $= 4)
         {
            exec("scriptsAndAssets/client/ui/MazeMinigame.gui");
            exec("scriptsAndAssets/client/scripts/minigame/MazeMinigame.cs");
         }
   }
}

// Start the minigame of the current scene/take
function Minigame::startMinigame(%this)
{
   Clapper.setVisible(false);
   Board.setVisible(false);
   
   MinigameCenter.setVisible(false);
   
   if(SceneGui.inOutro)
      OutroGuiCenter.setVisible(false);
         
   switch$($CurPlayerNum)
   {
      case 1:
         switch$($CurSceneName)
         {
            case "SpyLevel": // Spy level
               if($CurTake $= 1)       { PlayerProfile.Minigame1_1 = 1; }
               else if($CurTake $= 2)  { PlayerProfile.Minigame2_1 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame1_1 = 1; }
            case "GangsterLevel": // Gangster level
               if($CurTake $= 2)       { PlayerProfile.Minigame3_1 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame4_1 = 1; }
            case "KungFuLevel": // Kung Fu level
               if($CurTake $= 1)       { PlayerProfile.Minigame5_1 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame6_1 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame6_1 = 1; }
            case "DinosaurLevel": // Dinosaur level
               if($CurTake $= 2)       { PlayerProfile.Minigame7_1 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame14_1 = 1; }         
               else if($CurTake $= 4)  { PlayerProfile.Minigame14_1 = 1; }
            case "WesternLevel": // Dinosaur level
               if ($CurTake $= 2)      { PlayerProfile.Minigame13_1 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame9_1 = 1; }
            case "SciFiLevel": // Dinosaur level
               if($CurTake $= 1)       { PlayerProfile.Minigame10_1 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame10_1 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame8_1 = 1; }
            case "MusicalLevel": // Musical level
               if($CurTake $= 1)       { PlayerProfile.Minigame11_1 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame12_1 = 1; }
            case "MonsterLevel": // Monster level
               if($CurTake $= 1)       { PlayerProfile.Minigame16_1 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame16_1 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame15_1 = 1; }
         }
      
      case 2:
         switch$($CurSceneName)
         {
            case "SpyLevel": // Spy level
               if($CurTake $= 1)       { PlayerProfile.Minigame1_2 = 1; }
               else if($CurTake $= 2)  { PlayerProfile.Minigame2_2 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame1_2 = 1; }
            case "GangsterLevel": // Gangster level
               if($CurTake $= 2)       { PlayerProfile.Minigame3_2 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame4_2 = 1; }
            case "KungFuLevel": // Kung Fu level
               if($CurTake $= 1)       { PlayerProfile.Minigame5_2 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame6_2 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame6_2 = 1; }
            case "DinosaurLevel": // Dinosaur level
               if($CurTake $= 2)       { PlayerProfile.Minigame7_2 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame14_2 = 1; }         
               else if($CurTake $= 4)  { PlayerProfile.Minigame14_2 = 1; }
            case "WesternLevel": // Dinosaur level
               if ($CurTake $= 2)      { PlayerProfile.Minigame13_2 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame9_2 = 1; }
            case "SciFiLevel": // Dinosaur level
               if($CurTake $= 1)       { PlayerProfile.Minigame10_2 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame10_2 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame8_2 = 1; }
            case "MusicalLevel": // Musical level
               if($CurTake $= 1)       { PlayerProfile.Minigame11_2 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame12_2 = 1; }
            case "MonsterLevel": // Monster level
               if($CurTake $= 1)       { PlayerProfile.Minigame16_2 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame16_2 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame15_2 = 1; }
         }
      
      case 3:
         switch$($CurSceneName)
         {
            case "SpyLevel": // Spy level
               if($CurTake $= 1)       { PlayerProfile.Minigame1_3 = 1; }
               else if($CurTake $= 2)  { PlayerProfile.Minigame2_3 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame1_3 = 1; }
            case "GangsterLevel": // Gangster level
               if($CurTake $= 2)       { PlayerProfile.Minigame3_3 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame4_3 = 1; }
            case "KungFuLevel": // Kung Fu level
               if($CurTake $= 1)       { PlayerProfile.Minigame5_3 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame6_3 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame6_3 = 1; }
            case "DinosaurLevel": // Dinosaur level
               if($CurTake $= 2)       { PlayerProfile.Minigame7_3 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame14_3 = 1; }         
               else if($CurTake $= 4)  { PlayerProfile.Minigame14_3 = 1; }
            case "WesternLevel": // Dinosaur level
               if ($CurTake $= 2)      { PlayerProfile.Minigame13_3 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame9_3 = 1; }
            case "SciFiLevel": // Dinosaur level
               if($CurTake $= 1)       { PlayerProfile.Minigame10_3 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame10_3 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame8_3 = 1; }
            case "MusicalLevel": // Musical level
               if($CurTake $= 1)       { PlayerProfile.Minigame11_3 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame12_3 = 1; }
            case "MonsterLevel": // Monster level
               if($CurTake $= 1)       { PlayerProfile.Minigame16_3 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame16_3 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame15_3 = 1; }
         }
      
      case 4:
         switch$($CurSceneName)
         {
            case "SpyLevel": // Spy level
               if($CurTake $= 1)       { PlayerProfile.Minigame1_4 = 1; }
               else if($CurTake $= 2)  { PlayerProfile.Minigame2_4 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame1_4 = 1; }
            case "GangsterLevel": // Gangster level
               if($CurTake $= 2)       { PlayerProfile.Minigame3_4 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame4_4 = 1; }
            case "KungFuLevel": // Kung Fu level
               if($CurTake $= 1)       { PlayerProfile.Minigame5_4 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame6_4 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame6_4 = 1; }
            case "DinosaurLevel": // Dinosaur level
               if($CurTake $= 2)       { PlayerProfile.Minigame7_4 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame14_4 = 1; }         
               else if($CurTake $= 4)  { PlayerProfile.Minigame14_4 = 1; }
            case "WesternLevel": // Dinosaur level
               if ($CurTake $= 2)      { PlayerProfile.Minigame13_4 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame9_4 = 1; }
            case "SciFiLevel": // Dinosaur level
               if($CurTake $= 1)       { PlayerProfile.Minigame10_4 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame10_4 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame8_4 = 1; }
            case "MusicalLevel": // Musical level
               if($CurTake $= 1)       { PlayerProfile.Minigame11_4 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame12_4 = 1; }
            case "MonsterLevel": // Monster level
               if($CurTake $= 1)       { PlayerProfile.Minigame16_4 = 1; }
               else if($CurTake $= 3)  { PlayerProfile.Minigame16_4 = 1; }
               else if($CurTake $= 4)  { PlayerProfile.Minigame15_4 = 1; }
         }
   }
   
   %obj = "";
   
   switch$($CurSceneName)
   {
      case "SpyLevel": // Spy level
         if($CurTake $= 1)       { %obj = InsectMinigame; InsectMinigame.start(); }
         else if($CurTake $= 2)  { %obj = SliderMinigame; SliderMinigame.start(); }
         else if($CurTake $= 4)  { %obj = InsectMinigame; InsectMinigame.start(); }
      case "GangsterLevel": // Gangster level
         if($CurTake $= 2)       { %obj = BejeweledMinigame; BejeweledMinigame.start(); }
         else if($CurTake $= 3)  { %obj = MazeMinigame;      MazeMinigame.start();      }
      case "KungFuLevel": // Kung Fu level
         if($CurTake $= 1)       { %obj = PicResortMinigame; PicResortMinigame.start(); }
         else if($CurTake $= 3)  { %obj = MazeMinigame;      MazeMinigame.start();      }
         else if($CurTake $= 4)  { %obj = MazeMinigame;      MazeMinigame.start();      }
      case "DinosaurLevel": // Dinosaur level
         if($CurTake $= 2)       { %obj = PipeMinigame;      PipeMinigame.start();      }
         else if($CurTake $= 3)  { %obj = MazeMinigame;      MazeMinigame.start();      }         
         else if($CurTake $= 4)  { %obj = MazeMinigame;      MazeMinigame.start();      }
      case "WesternLevel": // Dinosaur level
         if ($CurTake $= 2)      { %obj = SimonMinigame;     SimonMinigame.start();     }
         else if($CurTake $= 4)  { %obj = SliderMinigame;    SliderMinigame.start();    }
      case "SciFiLevel": // Dinosaur level
         if($CurTake $= 1)       { %obj = BejeweledMinigame; BejeweledMinigame.start(); }
         else if($CurTake $= 3)  { %obj = BejeweledMinigame; BejeweledMinigame.start(); }
         else if($CurTake $= 4)  { %obj = PicResortMinigame; PicResortMinigame.start(); }
      case "MusicalLevel": // Musical level
         if($CurTake $= 1)       { %obj = SimonMinigame; SimonMinigame.start(); }
         else if($CurTake $= 4)  { %obj = SimonMinigame; SimonMinigame.start(); }
      case "MonsterLevel": // Monster level
         if($CurTake $= 1)       { %obj = PunchMinigame; PunchMinigame.start(); }
         else if($CurTake $= 3)  { %obj = PunchMinigame; PunchMinigame.start(); }
         else if($CurTake $= 4)  { %obj = MazeMinigame;  MazeMinigame.start();  }
   }
   
   if(isObject(%obj) && SceneGui.inOutro)
      Minigame.pushToBack(%obj);
   
   SavePlayerProfile();
}
