//-----------------------------------------------------------------------------
// Torque Game Engine 
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// SceneGui is the main TSControl through which the demo is viewed.
// The SceneGui also contains the hud controls.
//-----------------------------------------------------------------------------
function SceneGui::onWake(%this)
{
   // Avoid audio memory leak
   sfxShutdown();
   sfxStartup();
      
   %this.inIntro = false;
   %this.inOutro = false;
   %this.isHOGamePaused = 0;   
   %this.isTriggerCtrlPaused = 0;
   %this.isAudioPaused = 0;
   %this.isLevelMusicPlaying = false;
   
   %this.loadAwardXML();
   
   %this.showUnlockedAwardAtScore = false;
   
   %this.earlyQuit = false;
   
   $enableDirectInput = "1";
   activateDirectInput();

   // Update the action map
   moveMap.push();
   
   Canvas.setWidescreen(GuiCenter, 0, false);
   
   %this.potentialTimerAward = false;
   %this.potentialTimerAwardCounter = 0;
   
   // Initialize hints
   %this.hintObjectsFound = 0;
   %this.usedAnyHint = false;
   HintSystem.init();
   
   // Initialize award feedback   
   AwardRoom.addAwardGui(SceneGuiCamMouseGroup);
   SceneGuiCamMouseGroup.pushToBack(CamMouseHandler);
   
//   $CurObjsTaken = PlayerProfile.getFieldValue("ObjectsTaken"@$CurPlayerNum);   
   
   %this.initTutorial();
   %this.playTutorial();
    
   if($PlayTutorial $= false)
   {
      // Create a red slashed circle for showing when a wrong GUI element
      // is clicked during the tutorial
      %this.redSlashedCursor = new GuiBitmapCtrl() {
         canSaveDynamicFields = "0";
         Enabled = "1";
         isContainer = "0";
         HorizSizing = "relative";
         VertSizing = "relative";
         Position = "0 0";
         Extent = "50 50";
         MinExtent = "2 2";
         canSave = "1";
         Visible = "0";
         hovertime = "1000";
         alpha = "1";
         color = "255 255 255 255";
         unitClip = "0 0 1 1";
         bitmap = "scriptsAndAssets/data/ui/tutorial/wrong";
         wrap = "0";
      };

      GuiCenter.add(%this.redSlashedCursor);
      GuiCenter.pushToBack(%this.redSlashedCursor);
            
      %this.redSlashedCursorEventID = -1;
      %this.redSlashedCursorShow = false;
   }
      
   // Initialize scoring
   %this.initScore(MissionInfo);
 
   //this code has a provisory implementation and just spy is using it as test
   //if(%this.currentLevel $= "SpyLevel" && $CurTake == 1)
      //SubtitleBox.init("");   
 
   // Schedule camera light switching triggers
   %this.initCameraLightBehavior();
   
   // Play game music
   sfxPlay(MusicStarting@%this.currentLevel);
   scheduleObjLCA(%this, "HO", 10100, "playLevelMusic", 0);
   
   // Reset game time counter
   // To obtain time elapsed, use getRealTime()-%this.timeElapsedBase;
   %this.timeElapsedBase = getRealTime();
   
   // Countdown timer event
   %this.blinkTimerEventID = -1;
   %this.timerCounter = MissionInfo.sceneEndTime;
//   %this.timerCounter = 5;
   %this.updateTimer();
   
   TimerDigit1.setVisible(true);
   TimerDigit2.setVisible(true);
   TimerDigit3.setVisible(true);
   TimerColon.setVisible(true);
   
   if($CurSceneName $= "SpyLevel"){ //just for SpyLevel (PistolEffect)
      //scheduleObjLCA(%this, "HO", 1, "gameTimerCallbackMs", 0); // Called each milisec
   }
   
   scheduleObjLCA(%this, "HO", 1000, "gameTimerCallback", 0); // Called each sec
   
   schedulePopUpEvents();
   
   // Schedule director behavior triggers
   %this.initDirectorBehavior();
   
   if(%this.tutIsPlaying $= true)
      %this.pauseHOGameTutorial();   

   %this.pauseHOGame();   
     
   %this.clapperAniState = 0;
   
   // Load intro screen   
   if(isObject(IntroGui))
      IntroGui.delete();
   exec("scriptsAndAssets/client/ui/IntroGui.gui");   
   Canvas.pushDialog(IntroGui);
}

function SceneGui::onSleep(%this)
{
   // Cancel schedules
   cancelSchedulesLCA();
   
   // Clean up 2D popups in 3D
   popUpCleanUp();
   
   // Stop game music
   sfxStopAll();
   
   // pop the keymaps
   moveMap.pop();
}

// Start level
function SceneGui::start(%this, %playingMinigame)
{
   if(isObject(WorldMapGui))
      WorldMapGui.delete();
  
  // This was causing a crash!
//   if(isObject(IntroGui))
//      IntroGui.delete();
   
   // Add minigame object
   %this.inOutro = false;
   if(isObject(Minigame))
      Minigame.delete();   
   exec("scriptsAndAssets/client/ui/Minigame.gui");
   %this.add(Minigame);
   %this.pushToBack(Minigame);
      
   if($PlayTutorial || $CurFirstPlay $= 0)
   {
      // Show clapper board
      Minigame.setVisible(true);

      %path = "scriptsAndAssets/data/ui/clapperboard/clapper.jpg";
      Clapper.setDML(%path);    
      Clapper.forward();

      if(%playingMinigame)
         %path = "scriptsAndAssets/data/ui/clapperboard/minigame/" @ $CurSceneName @ "_take" @ $CurTake @ ".jpg";   
      else
         %path = "scriptsAndAssets/data/ui/clapperboard/inoutro/" @ $CurSceneName @ "_intro_take" @ $CurTake @ ".jpg";
      Board.setDML(%path);        
      Board.forward();

      Clapper.setVisible(true);
      Board.setVisible(true);

      IntroClapperStart.setVisible(true);

      if(%playingMinigame)
      {
         Minigame.play();
         %this.clapperAniState = 2;
      }
      else
         %this.clapperAniState = 1;
   }
      
   HintSystem.updateHint();
   
   if(!$PlayTutorial && $CurFirstPlay $= 1)   
   {
      %this.clapperAniState = 0;
      Clapper.setVisible(false);
      Board.setVisible(false);
      MiniGame.setVisible(false);
      %this.resumeHOGame();
      Minigame.start();
   }
}

function SceneGui::onIntroClapperStart(%this)
{
   sfxPlay(AudioButtonPress);
   
   IntroClapperStart.setVisible(false);
   
   Clapper.forward();
   Board.forward();
   
   Clapper.playRev(1);
   Board.playRev(1);
   
   sfxPlay(AudioClapperBoardMoving);
}

// Pause HO game
function SceneGui::pauseHOGame(%this)
{
   if(%this.tutIsPlaying $= true)
      return;
   
   %this.stopStaticCamAni(1);
   %this.stopStaticCamAni(2);
   %this.stopStaticCamAni(3);
   %this.stopStaticCamAni(4);
   
   // Pause elapsed time
   if(%this.isHOGamePaused $= 0)
   {
      %this.elapsedTimePause = getRealTime();
   }
   
   %this.isHOGamePaused++;
   
   // Prevent GUI from becoming clickable during pause
   PauseDummy.setVisible(true);
   
   pauseAnims();
   
   pauseSchedulesLCA();
   
   %this.pauseTriggerCtrl();
   
   %this.pauseAudio();
}

// Resume HO game previously paused with SceneGui::pauseHOGame
function SceneGui::resumeHOGame(%this)
{
   if($pausedOnLoseFocus)
   {
      $HOPausedOnLoseFocus = true;
      return;
   }
   
   if(%this.tutIsPlaying $= true)
      return;
   
   if(%this.isHOGamePaused $= 0)
      return;

   resumeSchedulesLCA();
   
   %this.resumeTriggerCtrl();
   
   %this.resumeAudio();
 
   if(%this.isHOGamePaused $= 1)
   { 
      // Make the GUI clickable again
      PauseDummy.setVisible(false);
      
      // Resume elapsed time
      %this.timeElapsedBase += getRealTime()-%this.elapsedTimePause;
   }
   
   resumeAnims();  
   
   %this.isHOGamePaused--;
}

// Pause trigger controls
function SceneGui::pauseTriggerCtrl(%this)
{
   if(%this.isTriggerCtrlPaused $= 0)   
      TriggerCtrl.pause();   
   %this.isTriggerCtrlPaused++;     
}

// Resume trigger controls
function SceneGui::resumeTriggerCtrl(%this)
{
   if(%this.isTriggerCtrlPaused $= 0)
      return;
   if(%this.isTriggerCtrlPaused $= 1)
      TriggerCtrl.resume();   
   %this.isTriggerCtrlPaused--;      
}

function SceneGui::playButtonPressSound(%this)
{
   sfxPlay(AudioButtonPress);
}

// Pause audio
function SceneGui::pauseAudio(%this)
{     
   if(%this.isAudioPaused $= 0)   
   {
      %this.isLevelMusicPlaying = false;
      sfxStopAll();
   }
   %this.isAudioPaused++;   
}

// Resume audio
function SceneGui::resumeAudio(%this)
{
   if(%this.isAudioPaused $= 0)
      return;
      
   if(%this.isAudioPaused $= 1)
   {
      %this.isLevelMusicPlaying = false;
      sfxStopAll();
      %this.playLevelMusic();
   }
   %this.isAudioPaused--;
}

// Trigger playing of game music
function SceneGui::playLevelMusic(%this)
{
   if(%this.isLevelMusicPlaying $= false)
   {
      SceneGui.lvlMusic = sfxPlay(Music@%this.currentLevel);
	  
	  //Play the western sfx
	  if (%this.currentLevel $= "WesternLevel") 
	  {
		// play stage loop
		sfxPlay(WESTERN_FX_stagecoach_Loop);
	  }
      
	  %this.isLevelMusicPlaying = true;
   }
}
function stopLevelMusic()
{
   SceneGui.stopLevelMusic();
}

function SceneGui::stopLevelMusic(%this)
{
	SceneGui.lvlMusic.stop();
}

function SceneGui::blinkTimer(%this, %period)
{
   if(%timerCounter <= 0)
   {
      TimerDigit1.setVisible(true);
      TimerDigit2.setVisible(true);
      TimerDigit3.setVisible(true);
      TimerColon.setVisible(true);     
      return;
   }
   
   TimerDigit1.Visible = !TimerDigit1.Visible;
   TimerDigit2.Visible = !TimerDigit2.Visible;
   TimerDigit3.Visible = !TimerDigit3.Visible;
   TimerColon.Visible  = !TimerColon.Visible;
   
   %this.blinkTimerEventID = scheduleObjLCA(%this, "HO", %period, 
      "blinkTimer", 1, %period);
}

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

// Call outro screen
function SceneGui::loadOutroScreen(%this)
{
   $returningFromScene = true;
   
   // Show clapper board
   Minigame.setVisible(true);

   %path = "scriptsAndAssets/data/ui/clapperboard/clapper.jpg";
   Clapper.setDML(%path);    
   Clapper.rewind();
   
   %path = "scriptsAndAssets/data/ui/clapperboard/inoutro/" @ $CurSceneName @ "_intro_take" @ $CurTake @ ".jpg";   
   Board.setDML(%path);        
   Board.rewind();

   Clapper.setVisible(true);
   Board.setVisible(true);
   Clapper.play(1);
   Board.play(1);
   
   sfxPlay(AudioClapperBoardMoving);
   scheduleObjLCA(%this, "HO", 1600, "playClapperBoardCloseSound", 0);
   
   SceneGui.schedule(1700, "loadOutroScreen2");
}

function SceneGui::loadOutroScreen2(%this)
{
   // Load outro screen
   if(isObject(OutroGui))
      OutroGui.delete();
   exec("scriptsAndAssets/client/ui/OutroGui.gui");
   Canvas.pushDialog(OutroGui);
}

function SceneGui::goMapScreen(%this)
{
   if(isObject(OutroGui))
      OutroGui.delete();
   exec("scriptsAndAssets/client/ui/OutroGui.gui");
   
   $returningFromScene = true;
   OutroGui.updateProfile();
   
   // Load map screen
   //MapGui.update(true);
   //Canvas.popDialog(OutroGui);
   MainMenuGui.goHome();
   //MainMenuGui.pushThread(map_menu_thread);
   showWorldMap(false);
}

function doSmokeEffectCaller(%pos3D)
{
   /*%x = getWord(%params, 0);
   %y = getWord(%params, 1);
   %z = getWord(%params, 2);
   
   %pos3D = %x SPC %y SPC %z;*/
   SceneGui.doSmokeEffect(%pos3D);
}

// Callback for checking TriggerCtrl.getCurTime() 
//just for SpyLevel (PistolEffect)
function SceneGui::doSmokeEffect(%this, %pos3D)
{
   /*if(TriggerCtrl.getCurTime() == 218050) %pos3D = "-15.4607 -14.5546 0.80816";
   else if(TriggerCtrl.getCurTime() == 218750) %pos3D = "-15.5407 -14.5546 0.81816";
   else if(TriggerCtrl.getCurTime() == 219450) %pos3D = "-15.5807 -14.5246 0.82816";
   else if(TriggerCtrl.getCurTime() == 220200) %pos3D = "-15.4007 -14.5246 0.81816";
   else if(TriggerCtrl.getCurTime() == 221000) %pos3D = "-15.5407 -14.5546 0.81816";
   
   if((TriggerCtrl.getCurTime() == 218050)||
      (TriggerCtrl.getCurTime() == 218750)||
      (TriggerCtrl.getCurTime() == 219450)||
      (TriggerCtrl.getCurTime() == 220200)||
      (TriggerCtrl.getCurTime() == 221000)){*/
      
      if(isObject(ParticleSmoke))
         ParticleSmoke.delete();
         
      new ParticleEmitterNode(ParticleSmoke) {
         canSaveDynamicFields = "1";
         rotation = "1 0 0 0";
         scale = "1 1 1";
         dataBlock = "SmokeEffectEmitterNode";
         emitter = "SmokeEffectEmitter";
         velocity = "1";
      };
      
      ParticleSmoke.position = %pos3D;// %obj.position;
      ParticleEffects.add(ParticleSmoke);
      
      scheduleObjLCA(%this, "HO", 500, "onKillParticle", 1, ParticleSmoke);
   /*}
   scheduleObjLCA(%this, "HO", 1, "gameTimerCallbackMs", 0);*/
}


// Callback for countdown timer
function SceneGui::gameTimerCallback(%this)
{
   if(!$HOPausedOnLoseFocus)
   {
      %this.EvaluateAwardsAtHO();
      
      // Used for checking an award
      if(%this.potentialTimerAward && 
         %this.potentialTimerAwardCounter-%this.timerCounter > 3)
      {
         %this.objsClickedIn3Sec = 0;
         %this.potentialTimerAward = false;
      }
    
      if(!$PlayTutorial)
         %this.timerCounter--;   
      
      if(%this.timerCounter < 0)
      {
         cancelScheduleLCA(%this.blinkTimerEventID);
         
         TimerDigit1.Visible = true;
         TimerDigit2.Visible = true;
         TimerDigit3.Visible = true;
         TimerColor.Visible  = true;
         
   /*      if(SceneGui.dirBehaviorGoodEnding $= true)
         {
            %this.showingEnding = true;
            SceneGui.showDirAni1(30000, SceneGui.dirTrigger[30000,"type",0], false, false);
         }
         else */
   //      {
            %this.pauseHOGame();
            scheduleObjLCA(%this, "HO", 250, "showScoreStarsPart1", 0);
            //%this.showScoreStarsPart1();
   //      }
         return;
      }
      else
      {
         %this.updateTimer();
         
         // Activate timer blinking at 2 minutes, 1 minute and 30 secs
         if(%this.timerCounter $= 120 || %this.timerCounter $= 60 || %this.timerCounter $= 30)
         {
            switch$(%this.timerCounter)
            {
               case 120: %period = 500;
               case 60:  %period = 250;
               case 30:  %period = 125;
            }
            
            cancelScheduleLCA(%this.blinkTimerEventID);
            %this.blinkTimerEventID = scheduleObjLCA(%this, "HO", %period,
               "blinkTimer", 1, %period);
         }
      }
   }
   
   // Re-schedule event
   if(%this.timerCounter >= 0)
      scheduleObjLCA(%this, "HO", 1000, "gameTimerCallback", 0);
}

// Update digits of countdown timer
function SceneGui::updateTimer(%this)
{     
   // Update countdown timer
   %minute = mFloor(%this.timerCounter / 60);
   TimerDigit1.bitmap = "scriptsAndAssets/data/ui/ho/timer/"@%minute;//@".png";

   %second = mFloor(((%this.timerCounter / 60) - %minute) * 60);
   %digit2 = mFloor(%second / 10); // decimal digit
   %digit3 = mFloor(((%second / 10) - %digit2) * 10); // unitary digit

   TimerDigit2.bitmap = "scriptsAndAssets/data/ui/ho/timer/"@%digit2;//@".png";
   TimerDigit3.bitmap = "scriptsAndAssets/data/ui/ho/timer/"@%digit3;//@".png";
}

//Reset the settings for all the cameras to default
function SceneGui::initCameras(%this) 
{
   //Set the reference object to the cameras
   %this.setCamShape(Cam0, MissionInfo.firstCamera !$= ""? MissionInfo.firstCamera : %this.camShape1);   
   %this.setCamShape(Cam1, %this.camShape1);   
   %this.setCamShape(Cam2, %this.camShape2);   
   %this.setCamShape(Cam3, %this.camShape3);   
   %this.setCamShape(Cam4, %this.camShape4);   
   
   //Set the an object to be attached to the Cam0, this object will
   //have the same position of the active camera (considering animation)
   Cam0.setAttachedObject(AttachedCamera);
   
   //setting static field of LCAByTurnsTSCtrl
   Cam1.setNumOfTurns(4);
   
   Cam1.renderTurn = 0;
   Cam2.renderTurn = 1;
   Cam3.renderTurn = 2;
   Cam4.renderTurn = 3;
}

//default is 1, increasing it we increase the game performance by decreasing the small cameras fps
function SceneGui::setMainCamSmallCamsUpdateRate(%rate)
{
   Cam1.setNumOfTurns(4*%rate);
   
   Cam1.renderTurn = 0;
   Cam2.renderTurn = %rate;
   Cam3.renderTurn = 2*%rate;
   Cam4.renderTurn = 3*%rate;
}

// Set the current selected camera
function SceneGui::setCamera(%this, %cam, %camNumber)
{
   if(%this.curCam $= %camNumber)
      return;

   %this.curCam = %camNumber;
   /*
   // Change director meter when choosing wrong camera
   if(%camNumber !$= %this.camBorderCur &&
      %this.camBorderCur !$= -1)
   {
      if(%this.decreaseDirectorMeter() $= false)
         return false;
   }
   else
   {
      if(%this.camBorderCur !$= -1)
      {
         %this.increaseDirectorMeter();
         %this.camBorderCur = -1;
      }
   }
   */
   CamLight1.Visible = false;
   CamLight2.Visible = false;
   CamLight3.Visible = false;
   CamLight4.Visible = false;
   ("CamLight"@%camNumber).Visible = true;
   
   sfxPlayOnce(AudioViewportStatic);

   ("StaticCam"@ %camNumber).setFps(30);
   ("StaticCam"@ %camNumber).play(0);
   ("StaticCam"@ %camNumber).Visible = true;
   scheduleObjLCA(%this, "HO", MissionInfo.sceneEndTime, 
      "stopStaticCamAni", 1, %camNumber);   
   
   %this.setCamShape(Cam0, %cam.refObjName);
}

function SceneGui::stopStaticCamAni(%this, %camNumber)
{
   ("StaticCam"@ %camNumber).stop();
   ("StaticCam"@ %camNumber).Visible = false;   
}

//// Add a value to the score
//// Use this function instead of assigning directly to %this.scoreHidObj
//function SceneGui::addToScore(%this, %value)
//{
   //if(%value)
   //{
      //%this.scoreHidObj += %value;
      //if(!%this.isUpdatingScore)
      //{
         //scheduleObjLCA(%this, "HO", %this.scoreAniCallbackTime, 
            //"scoreAniCallback", 0);
         //%this.isUpdatingScore = true;
      //}
   //}
//}
//
//// Schedule event for animating the score
//function SceneGui::scoreAniCallback(%this)
//{
   //%useCallbackAgain = true;
   //
   //if(%this.scoreHidObj < 0)
      //%this.scoreHidObj = 0;
   //
   //if(%this.scoreAni !$= %this.scoreHidObj)
   //{
      //if(%this.scoreAni < %this.scoreHidObj)
      //{
         //%this.scoreAni+=3;
         //if(%this.scoreAni > %this.scoreHidObj)
         //{
            //%this.scoreAni = %this.scoreHidObj;
            //%useCallbackAgain = false;
         //}
      //}
         //
      //if(%this.scoreAni > %this.scoreHidObj)
      //{
         //%this.scoreAni-=3;         
         //if(%this.scoreAni < %this.scoreHidObj)
         //{
            //%this.scoreAni = %this.scoreHidObj;
            //%useCallbackAgain = false;            
         //}
      //}
      //
      //%fontSize = %this.scoreFontSize * Canvas.scaleSize;
      //%text = "<just:center><font:Comic Sans MS:"@%fontSize@"><color:FFFFFF>"@%this.scoreAni;
      //ScoreWindowText.setText(%text);
   //}
   //
   //if(%useCallbackAgain)
   //{
      //scheduleObjLCA(%this, "HO", %this.scoreAniCallbackTime, 
         //"scoreAniCallback", 0);
   //}
   //else
   //{
      //%this.isUpdatingScore = false;
   //}
//}

// Callback for resizing events
function SceneGui::onResize(%this, %width, %height)
{
   //%this.onResizeScore();
   %this.updateSpeechBubble();

   if($PlayTutorial)
   {   
      %text = %this.tutTrigger[%this.tutCurState,"text",0];
      TutorialBoxText.setText(%text);
   }
   
   Canvas.setWidescreen(GuiCenter, 2000, true);   
}

//// Resizes the HO score text
//function SceneGui::onResizeScore(%this)
//{
   //%fontSize = %this.scoreFontSize * Canvas.scaleSize;
   //if(%fontSize > 62) %fontSize = 62;
   //%text = "<just:center><font:Comic Sans MS:"@%fontSize@"><color:FFFFFF>"@%this.scoreAni;
   //ScoreWindowText.setVisible(true);   
   //ScoreWindowText.setText(%text);   
//}

// Set initial state of score bar and director meter
// %obj = MissionInfo object
function SceneGui::initScore(%this, %obj)
{
   // Default settings
   %this.wrongObjsClicked     = 0;
   $CorrectObjsClickedInScene = 0;
   $HintObjectsFound          = 0;
   $InsectsFound              = 0;
   $AnimalsFound              = 0;
   $ToysFound                 = 0;   
   %this.scoreHidObj          = 0;
   %this.scoreMinigame        = 0;
   %this.scoreAni             = 0; // Used to animate the score change
   %this.wrongObjScore        = 0;
   //%this.scoreAniCallbackTime = 50;
   %this.isUpdatingScore      = false;
   %this.scoreFontSize        = 36;
   
   //%this.onResizeScore();
    
   // How much points to add/subtract from score when wrong object is clicked
   if(!isObject(%obj))  
      return;   
   %this.wrongObjScore = %obj.wrongObjScore;
   
   // If the tutorial is playing, we set a different initial score for
   // the director meter
   if(%this.tutIsPlaying $= true)
      %directorMeterScore = 39;
   else
      %directorMeterScore = %obj.dirMeterInit;
   
   %this.initDirectorMeter(%directorMeterScore);
}

// Show the pause dialog box
function SceneGui::showPauseDialog(%this)
{
   SceneGui.pauseHOGame();
   
//   Canvas.pushDialog(PauseDlg);
   Canvas.pushDialog(OptionsHODlg);
//   SceneGui.pushThread(options_ho_thread);
}

function CamMouseHandler::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks )
{
   //check first against the bounding box
   %obj = Cam0.getObjectAt(getWord(%worldPosition, 0), getWord(%worldPosition, 1), true);
   //take by polys if the object should not be detected this way
   if (!%obj.clickBox)
      %obj = Cam0.getObjectAt(getWord(%worldPosition, 0), getWord(%worldPosition, 1));

   if (%obj == 0)
      return;
      
   // Check if object is valid to click during tutorial
   if(SceneGui.isClickable(%obj.getName()) $= false)
      return;

   // A potential shopping list item was clicked
   if(%obj.shopItem && !%obj.getClientId().isHidden())
   {
      %pos3D = Cam0.get3DPosAt(getWord(%worldPosition, 0), getWord(%worldPosition, 1));
            
      // Get mouse position
      // We'll it need for generating the particles
      %curMousePos = canvas.getCursorPos();
      %mouseX = getWord(%curMousePos,0);
      %mouseY = getWord(%curMousePos,1);
      
      %fileType = 0;
      //if(%obj.itemName $= "Mouse")
      //   %fileType = 1;
         
      if(%obj.priority $= -2 || ShoppingList.remove(%obj))
      {
         // Check for specific behavior of each tutorial state
         if(SceneGui.tutCurState $= 9)
         {
            if(%obj.itemName $= "Ladder")
               SceneGui.onTutorialBoxArrow();
         }
         
         // Spider, Ant, Bee, Cockroach, Fly
         if(%obj.itemName $= "Spider"    ||
            %obj.itemName $= "Ant"       ||
            %obj.itemName $= "Bee"       ||
            %obj.itemName $= "Cockroach" ||
            %obj.itemName $= "Fly")
            $InsectsFound++;
         else
         // Mouse, Crab, Bird
         if(%obj.itemName $= "Mouse" ||
            %obj.itemName $= "Crab"  ||
            %obj.itemName $= "Bird")
            $AnimalsFound++;
         else
         // Tank, Plane, Bus, Car, Taxi            
         if(%obj.itemName $= "Tank"  ||
            %obj.itemName $= "Plane" ||
            %obj.itemName $= "Bus"   ||
            %obj.itemName $= "Car"   ||
            %obj.itemName $= "Taxi")
            $ToysFound++;
         else if(!$PlayTutorial)
         {
/*            %objID = $CurSceneName @ $CurTake @ %obj.hintFile;
            
            %notTakenYet = true;
            %curWord = "";
            %wordCount = getWordCount($CurObjsTaken);

            for(%ct=0; %ct < %wordCount; %ct++)
            {
               %curWord = getWord($CurObjsTaken, %ct);
               if(%curWord $= %objID)
               {
                  %notTakenYet = false;
                  %ct = %wordCount;
               }
            }
            
            if(%notTakenYet)
            {
               if(strlen($CurObjsTaken) $= 0)
                  $CurObjsTaken = %objID;
               else
                  $CurObjsTaken = $CurObjsTaken SPC %objID;
*/                                 
               $CorrectObjsClickedInScene++;
//            }
         }
         
         SceneGui.potentialTimerAward = true;
         SceneGui.potentialTimerAwardCounter = SceneGui.timerCounter;        
         SceneGui.objsClickedIn3Sec++;
        
         // Visual feedback
         //ParticleFx.RippleFx(%mouseX, %mouseY, 25, 1.6, true); // 1.8
         //ParticleFx.FireworksFx(%mouseX, %mouseY, 3, 0.6); // 0.8
         
         if(%obj.hintsToGain !$= "")
		   {
		      $HintObjectsFound++;
            HintSystem.addHints(%obj.hintsToGain);  
			   sfxPlay(AudioHintFound);
		   }
         else
         {
            %points = %obj.dmPoints $= "" ? (%obj.score $= ""? 2 : mRound(%obj.score/50)) : %obj.dmPoints;         
            if($pref::CometEffectOn $= "true" || $pref::CometEffectOn $= 1)
            {
               ParticleFx.CometDirectorFx(%mouseX, %mouseY, 1, 0.6, %fileType); // 0.8
               
               //ParticleFx.CometHitMeterFx(34, 232, 2, 1.0);
               //sfxPlay(AudioWinStar);
               if(SceneGui.timerCounter < 2)
               {
                  SceneGui.addToDirectorMeter(%points);
               }
               else
                  scheduleObjLCA(SceneGui, "HO", 1000, "addToDirectorMeter", 1, %points);            
            }
            else
            {
               SceneGui.addToDirectorMeter(%points);            
            }

            ParticleFx.DirectorParticles("scriptsAndAssets/data/ui/ho/particles/particle_star_50small.png", 1, 2);
         }

         SceneGui.CloudEffectRightObjectClick(%pos3D,true);
         SceneGui.SparkEffect(%pos3D);

         // Audio feedback
         %audio = AudioRightObject; 
         if (%obj.dyingAudio !$= "") //if an specific audio is set for the obj
         {
            %audio = %obj.dyingAudio; 
         } 
         else if (isObject(%obj.dataBlock) && %obj.dataBlock.isMethod(getDyingAudio)) //or for the datablock
         {
            %audio = %obj.dataBlock.getDyingAudio();
         }
         sfxPlay(%audio);
		   //%obj.dump();
		   echo ("Respawning MO: RspCount:"@ %obj.respawnCount @" RspTime:"@ %obj.respawnTime);
         if (%obj.priority $= -2 && %obj.respawnTime !$= "" && %obj.respawnCount > 0)
		   {
            %obj.respawnCount--;
            %min = getWord(%obj.respawnTime,0);
            %max = getWord(%obj.respawnTime,1);
            %time = (getRandom(%max - %min) + %min)*1000;
            echo ("Respawning MO: RspCount:"@ %obj.respawnCount @" RspTime:"@ %time);
            %obj.respawn( %time); //respawn moving objects
   		 }
          else
		    {
            %obj.delete();
          }
      }
      else
      {
         // Wrong object
         SceneGui.enableRedSlashedCursor(0.5);         
         
         // Audio feedback
         sfxPlay(AudioWrongObject);
   
         // Visual feedback
         //ParticleFx.RippleFx(%mouseX, %mouseY, 25, 1.5, false); // 1.8
         //SceneGui.CloudEffectRightObjectClick(%pos3D,false);
      
         // Update score when wrong object was clicked
         SceneGui.scoreHidObj += SceneGui.wrongObjScore;
      
         SceneGui.wrongObjsClicked++;
         SceneGui.addToDirectorMeter(%obj.dmPoints $= "" || %obj.dmPoints >= 0? -SceneGui.wrongObjScore : %obj.dmPoints);
      }
   } 
}

function SceneGui::setCamShape(%this, %obj, %camShape)
{
   %obj.setRefObject(%camShape);
   
   //dynamic attribute camera sepecific shapes for LCA may have.
   switch$ (%camShape.effect)
   {
      case "security_camera":
         //set security cam specific settings
         %obj.setOverlayBitmap("scriptsAndAssets/data/misc/static");
         %obj.setOverlayColor(0.4, 0.4, 0.3, 0.2);
         %obj.useOverlayBitmap = true;
         %obj.useOverlayColor = true;
         %obj.spriteSize = 128 SPC 128;
         %obj.framesPerSec = 16;
         %obj.framesCount = 16;

      //....
      //other effects supported
      //....         
      
      default:
         // no effect (Reset LCAOverlayTSCtrl settings)
         %obj.setOverlayBitmap(NULL);
         %obj.setOverlayColor(1.0, 1.0, 1.0, 0.5);
         %obj.useOverlayBitmap = false;
         %obj.useOverlayColor = false;
         %obj.spriteSize = 0 SPC 0;
         %obj.framesPerSec = 0;
         %obj.framesCount = 1;
   }
}

function SceneGui::onKillParticle(%this, %obj)
{
   if(isObject(%obj))
   {
      ParticleEffects.remove(%obj);
      %obj.delete();
   }
}

function SceneGui::CloudEffectRightObjectClick(%this,%position,%isRightObject)
{
   if(isObject(ParticleCloud))
      ParticleCloud.delete();
   
   if(%isRightObject){
      new ParticleEmitterNode(ParticleCloud) {
         canSaveDynamicFields = "1";
         rotation = "1 0 0 0";
         scale = "1 1 1";
         dataBlock = "CloudEmitterNode";
         emitter = "CloudEmitter";
         velocity = "1";
      };
   }else{
      new ParticleEmitterNode(ParticleCloud) {
         canSaveDynamicFields = "1";
         rotation = "1 0 0 0";
         scale = "1 1 1";

         dataBlock = "DarkCloudEmitterNode";
         emitter = "DarkCloudEmitter";
                  
         velocity = "1";
      };
   }
   ParticleCloud.position = %position;// %obj.position;
   if(isObject(ParticleCloud))
      ParticleEffects.add(ParticleCloud);
   scheduleObjLCA(%this, "HO", 500, "onKillParticle", 1, ParticleCloud);
}

function SceneGui::SparkEffect(%this,%position)
{
   if(isObject(ParticleClick))
      ParticleClick.delete();
      
   new ParticleEmitterNode(ParticleClick) {
      canSaveDynamicFields = "1";
      rotation = "1 0 0 0";
      scale = "1 1 1";
      dataBlock = "SparkEmitterNode";
      emitter = "SparkEmitter";
      velocity = "1";
   };
   ParticleClick.position = %position;// %obj.position;
   ParticleEffects.add(ParticleClick);
   scheduleObjLCA(%this, "HO", 500, "onKillParticle", 1, ParticleClick);
}

// Evaluate awards won (to be shown at HO screen)
function SceneGui::EvaluateAwardsAtHO(%this)
{
   %time = 0;
   %numAwards = 0;
         
   switch$($CurPlayerNum)
   {
   case 1:
   
      // [16] Find 2 objects in 3 seconds
      if(%this.potentialTimerAward &&
         %this.objsClickedIn3Sec >= 2 &&
         PlayerProfile.Award_2objects_3seconds_1 $= 0) 
      {
         %this.awardText[%numAwards] = %this.awardWon[16];//"You have won an award!\nRapid Fire";
         %this.awardNum[%numAwards] = 16;
         PlayerProfile.Award_2objects_3seconds_1 = 1;
         %numAwards++;
      }

      // [15] Find 3 objects in 3 seconds
      if(%this.potentialTimerAward &&
         %this.objsClickedIn3Sec >= 3 &&
         PlayerProfile.Award_3objects_3seconds_1 $= 0) 
      {
         %this.awardText[%numAwards] = %this.awardWon[15];//"You have won an award!\nSharpshooter";
         %this.awardNum[%numAwards] = 15;
         PlayerProfile.Award_3objects_3seconds_1 = 1;
         %numAwards++;
      }
      
      // [22] Find 600 items
      if(PlayerProfile.CorrectObjs_1 + $CorrectObjsClickedInScene >= 600 &&
         PlayerProfile.Award_600_objects_1 $= 0)   
      {
         %this.awardText[%numAwards] = %this.awardWon[22];//"You have won an award!\nTask Master";
         %this.awardNum[%numAwards] = 22;
         PlayerProfile.Award_600_objects_1 = 1;
         %numAwards++;
      }

      // [17] Find all items within 2 minutes
      %remainingShopItems = false;   
      for(%j=1; %j<=ShoppingList.shopListSize; %j++)
      {
         if(ShoppingList.shopList[%j,"quantity"] > 0)
            %remainingShopItems = true;
      }
      if(!%remainingShopItems && 
         MissionInfo.sceneEndTime - %this.timerCounter <= 120 &&
         PlayerProfile.Award_all_items_2minutes_1 $= 0)      
      {
         %this.awardText[%numAwards] = %this.awardWon[17];//"You have won an award!\nPersonal Assistant";
         %this.awardNum[%numAwards] = 17;
         PlayerProfile.Award_all_items_2minutes_1 = 1;
         %numAwards++;
      }
      
      // [12] Remove 50 Animals
      if(PlayerProfile.Animals1 + $AnimalsFound >= 50 &&
         PlayerProfile.Award_wildlife_1 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[12];//"You have won an award!\nConservationist";
         %this.awardNum[%numAwards] = 12;
         PlayerProfile.Award_wildlife_1 = 1;
         %numAwards++;         
      }
      
      // [13] Clear 50 Toys
      if(PlayerProfile.Toys1 + $ToysFound >= 50 &&
         PlayerProfile.Award_toys_1 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[13];//"You have won an award!\nBabysitter";
         %this.awardNum[%numAwards] = 13;
         PlayerProfile.Award_toys_1 = 1;
         %numAwards++;         
      }
      
      // [14] Swat 100 Bugs
      if(PlayerProfile.Insects1 + $InsectsFound >= 100 &&
         PlayerProfile.Award_insects_1 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[14];//"You have won an award!\nPest Control";
         %this.awardNum[%numAwards] = 14;
         PlayerProfile.Award_insects_1 = 1;
         %numAwards++;         
      }
   
   case 2:
   
      // [16] Find 2 objects in 3 seconds
      if(%this.potentialTimerAward &&
         %this.objsClickedIn3Sec >= 2 &&
         PlayerProfile.Award_2objects_3seconds_2 $= 0) 
      {
         %this.awardText[%numAwards] = %this.awardWon[16];//"You have won an award!\nRapid Fire";
         %this.awardNum[%numAwards] = 16;
         PlayerProfile.Award_2objects_3seconds_2 = 1;
         %numAwards++;
      }

      // [15] Find 3 objects in 3 seconds
      if(%this.potentialTimerAward &&
         %this.objsClickedIn3Sec >= 3 &&
         PlayerProfile.Award_3objects_3seconds_2 $= 0) 
      {
         %this.awardText[%numAwards] = %this.awardWon[15];//"You have won an award!\nSharpshooter";
         %this.awardNum[%numAwards] = 15;
         PlayerProfile.Award_3objects_3seconds_2 = 1;
         %numAwards++;
      }
      
      // [22] Find 600 items
      if(PlayerProfile.CorrectObjs_2 + $CorrectObjsClickedInScene >= 600 &&
         PlayerProfile.Award_600_objects_2 $= 0)   
      {
         %this.awardText[%numAwards] = %this.awardWon[22];//"You have won an award!\nTask Master";
         %this.awardNum[%numAwards] = 22;
         PlayerProfile.Award_600_objects_2 = 1;
         %numAwards++;
      }

      // [17] Find all items within 2 minutes
      %remainingShopItems = false;   
      for(%j=1; %j<=ShoppingList.shopListSize; %j++)
      {
         if(ShoppingList.shopList[%j,"quantity"] > 0)
            %remainingShopItems = true;
      }
      if(!%remainingShopItems && 
         MissionInfo.sceneEndTime - %this.timerCounter <= 120 &&
         PlayerProfile.Award_all_items_2minutes_2 $= 0)      
      {
         %this.awardText[%numAwards] = %this.awardWon[17];//"You have won an award!\nPersonal Assistant";
         %this.awardNum[%numAwards] = 17;
         PlayerProfile.Award_all_items_2minutes_2 = 1;
         %numAwards++;
      }
      
      // [12] Remove 50 Animals
      if(PlayerProfile.Animals2 + $AnimalsFound >= 50 &&
         PlayerProfile.Award_wildlife_2 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[12];//"You have won an award!\nConservationist";
         %this.awardNum[%numAwards] = 12;
         PlayerProfile.Award_wildlife_2 = 1;
         %numAwards++;         
      }
      
      // [13] Clear 50 Toys
      if(PlayerProfile.Toys2 + $ToysFound >= 50 &&
         PlayerProfile.Award_toys_2 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[13];//"You have won an award!\nBabysitter";
         %this.awardNum[%numAwards] = 13;
         PlayerProfile.Award_toys_2 = 1;
         %numAwards++;         
      }
      
      // [14] Swat 100 Bugs
      if(PlayerProfile.Insects2 + $InsectsFound >= 100 &&
         PlayerProfile.Award_insects_2 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[14];//"You have won an award!\nPest Control";
         %this.awardNum[%numAwards] = 14;
         PlayerProfile.Award_insects_2 = 1;
         %numAwards++;         
      }
      
   case 3:
   
      // [16] Find 2 objects in 3 seconds
      if(%this.potentialTimerAward &&
         %this.objsClickedIn3Sec >= 2 &&
         PlayerProfile.Award_2objects_3seconds_3 $= 0) 
      {
         %this.awardText[%numAwards] = %this.awardWon[16];//"You have won an award!\nRapid Fire";
         %this.awardNum[%numAwards] = 16;
         PlayerProfile.Award_2objects_3seconds_3 = 1;
         %numAwards++;
      }

      // [15] Find 3 objects in 3 seconds
      if(%this.potentialTimerAward &&
         %this.objsClickedIn3Sec >= 3 &&
         PlayerProfile.Award_3objects_3seconds_3 $= 0) 
      {
         %this.awardText[%numAwards] = %this.awardWon[15];//"You have won an award!\nSharpshooter";
         %this.awardNum[%numAwards] = 15;
         PlayerProfile.Award_3objects_3seconds_3 = 1;
         %numAwards++;
      }
      
      // [22] Find 600 items
      if(PlayerProfile.CorrectObjs_3 + $CorrectObjsClickedInScene >= 600 &&
         PlayerProfile.Award_600_objects_3 $= 0)   
      {
         %this.awardText[%numAwards] = %this.awardWon[22];//"You have won an award!\nTask Master";
         %this.awardNum[%numAwards] = 22;
         PlayerProfile.Award_600_objects_3 = 1;
         %numAwards++;
      }

      // [17] Find all items within 2 minutes
      %remainingShopItems = false;   
      for(%j=1; %j<=ShoppingList.shopListSize; %j++)
      {
         if(ShoppingList.shopList[%j,"quantity"] > 0)
            %remainingShopItems = true;
      }
      if(!%remainingShopItems && 
         MissionInfo.sceneEndTime - %this.timerCounter <= 120 &&
         PlayerProfile.Award_all_items_2minutes_3 $= 0)      
      {
         %this.awardText[%numAwards] = %this.awardWon[17];//"You have won an award!\nPersonal Assistant";
         %this.awardNum[%numAwards] = 17;
         PlayerProfile.Award_all_items_2minutes_3 = 1;
         %numAwards++;
      }
      
      // [12] Remove 50 Animals
      if(PlayerProfile.Animals3 + $AnimalsFound >= 50 &&
         PlayerProfile.Award_wildlife_3 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[12];//"You have won an award!\nConservationist";
         %this.awardNum[%numAwards] = 12;
         PlayerProfile.Award_wildlife_3 = 1;
         %numAwards++;         
      }
      
      // [13] Clear 50 Toys
      if(PlayerProfile.Toys3 + $ToysFound >= 50 &&
         PlayerProfile.Award_toys_3 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[13];//"You have won an award!\nBabysitter";
         %this.awardNum[%numAwards] = 13;
         PlayerProfile.Award_toys_3 = 1;
         %numAwards++;         
      }
      
      // [14] Swat 100 Bugs
      if(PlayerProfile.Insects3 + $InsectsFound >= 100 &&
         PlayerProfile.Award_insects_3 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[14];//"You have won an award!\nPest Control";
         %this.awardNum[%numAwards] = 14;
         PlayerProfile.Award_insects_3 = 1;
         %numAwards++;         
      }      
      
   case 4:
   
      // [16] Find 2 objects in 3 seconds
      if(%this.potentialTimerAward &&
         %this.objsClickedIn3Sec >= 2 &&
         PlayerProfile.Award_2objects_3seconds_4 $= 0) 
      {
         %this.awardText[%numAwards] = %this.awardWon[16];//"You have won an award!\nRapid Fire";
         %this.awardNum[%numAwards] = 16;
         PlayerProfile.Award_2objects_3seconds_4 = 1;
         %numAwards++;
      }

      // [15] Find 3 objects in 3 seconds
      if(%this.potentialTimerAward &&
         %this.objsClickedIn3Sec >= 3 &&
         PlayerProfile.Award_3objects_3seconds_4 $= 0) 
      {
         %this.awardText[%numAwards] = %this.awardWon[15];//"You have won an award!\nSharpshooter";
         %this.awardNum[%numAwards] = 15;
         PlayerProfile.Award_3objects_3seconds_4 = 1;
         %numAwards++;
      }
      
      // [22] Find 600 items
      if(PlayerProfile.CorrectObjs_4 + $CorrectObjsClickedInScene >= 600 &&
         PlayerProfile.Award_600_objects_4 $= 0)   
      {
         %this.awardText[%numAwards] = %this.awardWon[22];//"You have won an award!\nTask Master";
         %this.awardNum[%numAwards] = 22;
         PlayerProfile.Award_600_objects_4 = 1;
         %numAwards++;
      }

      // [17] Find all items within 2 minutes
      %remainingShopItems = false;   
      for(%j=1; %j<=ShoppingList.shopListSize; %j++)
      {
         if(ShoppingList.shopList[%j,"quantity"] > 0)
            %remainingShopItems = true;
      }
      if(!%remainingShopItems && 
         MissionInfo.sceneEndTime - %this.timerCounter <= 120 &&
         PlayerProfile.Award_all_items_2minutes_4 $= 0)      
      {
         %this.awardText[%numAwards] = %this.awardWon[17];//"You have won an award!\nPersonal Assistant";
         %this.awardNum[%numAwards] = 17;
         PlayerProfile.Award_all_items_2minutes_4 = 1;
         %numAwards++;
      }
      
      // [12] Remove 50 Animals
      if(PlayerProfile.Animals4 + $AnimalsFound >= 50 &&
         PlayerProfile.Award_wildlife_4 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[12];//"You have won an award!\nConservationist";
         %this.awardNum[%numAwards] = 12;
         PlayerProfile.Award_wildlife_4 = 1;
         %numAwards++;         
      }
      
      // [13] Clear 50 Toys
      if(PlayerProfile.Toys4 + $ToysFound >= 50 &&
         PlayerProfile.Award_toys_4 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[13];//"You have won an award!\nBabysitter";
         %this.awardNum[%numAwards] = 13;
         PlayerProfile.Award_toys_4 = 1;
         %numAwards++;         
      }
      
      // [14] Swat 100 Bugs
      if(PlayerProfile.Insects4 + $InsectsFound >= 100 &&
         PlayerProfile.Award_insects_4 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[14];//"You have won an award!\nPest Control";
         %this.awardNum[%numAwards] = 14;
         PlayerProfile.Award_insects_4 = 1;
         %numAwards++;         
      }      
   }
      
   if(%numAwards > 0)
      SavePlayerProfile();
   
   for(%i=0; %i < %numAwards; %i++)
   {
      %text = %this.awardText[%i];
      %awardNum = %this.awardNum[%i];
      if(%text $= "")
         %text =  "      " @ %this.awardGeneric/*"You have won an award!"*/ @  "      ";
      
      scheduleObjLCA(SceneGui, "HO", %time, "showUnlockedAward", 2, %text, %awardNum);
      
      %time += 3000;
   }
     
   return %time;
}

//SceneGui.showUnlockedAward("You have won an award!\nSharpshooter", 15)

// Visual feedback for the recently unlocked scenes
function SceneGui::showUnlockedAward(%this, %text, %awardNum)
{
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
   
   %obj  = AwardUnlockedSceneTextHO;
   %objs = AwardUnlockedSceneBoxHO;
   %obja = AwardUnlockedSceneHO;
      
   switch(%awardNum)
   {
      // [1] Award_fiji
      case 1:
         %ex = 100; %ey = 201;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/fiji.png");
      // [2] Award_london
      case 2:
         %ex = 68; %ey = 198;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/london.png");
      // [3] Award_tokyo
      case 3:
         %ex = 71; %ey = 199;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/tokyo.png");
      // [4] Award_mexico
      case 4:
         %ex = 74; %ey =  190;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/mexico.png");
      // [5] Award_hongkong
      case 5:
         %ex = 76; %ey = 202;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/hongkong.png");
      // [6] Award_hollywood
      case 6:
         %ex = 122; %ey = 147;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/hollywood.png");
      // [7] Award_newyork
      case 7:
         %ex = 68; %ey = 203;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/newyork.png");
      // [8] Award_paris
      case 8:
         %ex = 91; %ey = 189;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/paris.png");
      // [9] Award_no_hints
      case 9:
         %ex = 72; %ey = 112;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/no_hints.png");
      // [10] Award_no_wrong_items
      case 10:
         %ex = 65; %ey = 117;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/no_wrong_items.png");
      // [11] Award_takes_movie
      case 11:
         %ex = 72; %ey = 141;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/takes_movie.png");
      // [12] Award_wildlife
      case 12:
         %ex = 104; %ey = 130;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/wildlife.png");
      // [13] Award_toys
      case 13:
         %ex = 122; %ey = 148;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/toys.png");
      // [14] Award_insects
      case 14:
         %ex = 99; %ey = 151;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/insects.png");
      // [15] Award_3objects_3seconds
      case 15:
         %ex = 89; %ey = 128;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/3objects_3seconds.png");
      // [16] Award_2objects_3seconds
      case 16:
         %ex = 73; %ey = 123;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/2objects_3seconds.png");
      // [17] Award_all_items_2minutes
      case 17:
         %ex = 79; %ey = 148;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/all_items_2minutes.png");
      // [18] Award_5star_take
      case 18:
         %ex = 64; %ey = 103;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/5star_take.png");
      // [19] Award_all_hint_objects
      case 19:
         %ex = 67; %ey = 161;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/all_hint_objects.png");
      // [20] Award_150_stars
      case 20:
         %ex = 96; %ey = 149;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/160_stars.png");
      // [21] Award_takes_game
      case 21:
         %ex = 96; %ey = 165;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/takes_game.png");
      // [22] Award_600_objects
      case 22:
         %ex = 94; %ey = 173;
         %obja.setDML("scriptsAndAssets/data/ui/menu/map/awards/400_objects.png");
   }
   
   %offsetX = 0;
   %offsetY = 0;
   
   if(%this.showUnlockedAwardAtScore)
   {
      %offsetX = 80;
      %offsetY = -50;
      
      GuiCenter.pushToBack(%objs);
      GuiCenter.pushToBack(%obj);
      GuiCenter.pushToBack(%obja);

      %objs.setPosition((200+%offsetX)*%winScaleX, (360+%offsetY)*%winScaleY);
      %obj.setPosition((270+%offsetX)*%winScaleX, (370+%offsetY)*%winScaleY);
   }      
   
   %ex *= 0.3;
   %ey *= 0.3;
   
   %origPX = 235 + %offsetX;
   %origPY = 399 + %offsetY;
   
   %obja.setExtent(%ex*%winScaleX, %ey*%winScaleY);
   %obja.setPosition((%origPX-%ex/2)*%winScaleX, (%origPY-%ey/2)*%winScaleY);
   
   %obj.setAlpha(0);
   %objs.setAlpha(0);
   %obja.setAlpha(0);
   
   %obj.setText(%text);
   
   %obj.setVisible(true);
   %objs.setVisible(true);
   %obja.setVisible(true);
      
   %obj.animateAlpha(1, 0, 500, "0 0");
   %objs.animateAlpha(1, 0, 500, "0 0");
   %obja.animateAlpha(1, 0, 500, "0 0");   
   
   scheduleObjLCA(%this, "HO", 1800, "showUnlockedAward2", 0);
}

function SceneGui::showUnlockedAward2(%this)
{
   %obj  = AwardUnlockedSceneTextHO;
   %objs = AwardUnlockedSceneBoxHO;
   %obja = AwardUnlockedSceneHO;
   
   %obj.animateAlpha(0, 0, 500, "0 0");
   %objs.animateAlpha(0, 0, 500, "0 0");
   %obja.animateAlpha(0, 0, 500, "0 0");
   
   scheduleObjLCA(%this, "HO", 1000, "showUnlockedAward3", 0);
}

function SceneGui::showUnlockedAward3(%this)
{
   %obj  = AwardUnlockedSceneTextHO;
   %objs = AwardUnlockedSceneBoxHO;
   %obja = AwardUnlockedSceneHO;
   
   %obj.setVisible(false);
   %objs.setVisible(false);
   %obja.setVisible(false);
}

// Evaluate awards won (to be shown at score screen)
function SceneGui::EvaluateAwardsAtScore(%this)
{
   %time = 0;
   %numAwards = 0;

   %this.showUnlockedAwardAtScore = true;
   
   switch$($CurPlayerNum)
   {
   case 1:  
         
      %remainingShopItems = false;   
      for(%j=1; %j<=ShoppingList.shopListSize; %j++)
      {
         if(ShoppingList.shopList[%j,"quantity"] > 0)
            %remainingShopItems = true;
      }
            
      // [9] Find all items in a take without using any hints
      if(!%remainingShopItems && !%this.usedAnyHint && PlayerProfile.Award_no_hints_1 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[9];//"You have won an award!\nDetective";
         %this.awardNum[%numAwards] = 9;
         PlayerProfile.Award_no_hints_1 = 1;
         %numAwards++;
      }
      
      // [19] Find all the hidden hints     
      if(PlayerProfile.StarsMini1 >= 40 && PlayerProfile.Award_all_hint_objects_1 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[19];//"You have won an award!\nPuzzle Master";
         %this.awardNum[%numAwards] = 19;
         PlayerProfile.Award_all_hint_objects_1 = 1;
         %numAwards++;
      }
      
      // [10] Complete a take without hitting any wrong objects
      if(!%remainingShopItems && %this.wrongObjsClicked $= 0 && PlayerProfile.Award_no_wrong_items_1 $= 0) 
      {
         %this.awardText[%numAwards] = %this.awardWon[10];//"You have won an award!\nEagle Eyed";
         %this.awardNum[%numAwards] = 10;
         PlayerProfile.Award_no_wrong_items_1 = 1;
         %numAwards++;
      }
  
   case 2:
   
      %remainingShopItems = false;   
      for(%j=1; %j<=ShoppingList.shopListSize; %j++)
      {
         if(ShoppingList.shopList[%j,"quantity"] > 0)
            %remainingShopItems = true;
      }   
   
      // [9] Find all items in a take without using any hints
      if(!%remainingShopItems && !%this.usedAnyHint && PlayerProfile.Award_no_hints_2 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[9];//"You have won an award!\nDetective";
         %this.awardNum[%numAwards] = 9;
         PlayerProfile.Award_no_hints_2 = 1;
         %numAwards++;
      }
      
      // [19] Find all the hidden hints
      if(PlayerProfile.StarsMini2 >= 40 && PlayerProfile.Award_all_hint_objects_2 $= 0)      
      {
         %this.awardText[%numAwards] = %this.awardWon[19];//"You have won an award!\nClue Master";
         %this.awardNum[%numAwards] = 19;
         PlayerProfile.Award_all_hint_objects_2 = 1;
         %numAwards++;
      }
      
      // [10] Complete a take without hitting any wrong objects
      if(!%remainingShopItems && %this.wrongObjsClicked $= 0 && PlayerProfile.Award_no_wrong_items_2 $= 0) 
      {
         %this.awardText[%numAwards] = %this.awardWon[10];//"You have won an award!\nEagle Eyed";
         %this.awardNum[%numAwards] = 10;
         PlayerProfile.Award_no_wrong_items_2 = 1;
         %numAwards++;
      }
      
   case 3:
   
      %remainingShopItems = false;   
      for(%j=1; %j<=ShoppingList.shopListSize; %j++)
      {
         if(ShoppingList.shopList[%j,"quantity"] > 0)
            %remainingShopItems = true;
      }   
   
      // [9] Find all items in a take without using any hints
      if(!%remainingShopItems && !%this.usedAnyHint && PlayerProfile.Award_no_hints_3 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[9];//"You have won an award!\nDetective";
         %this.awardNum[%numAwards] = 9;
         PlayerProfile.Award_no_hints_3 = 1;
         %numAwards++;
      }
      
      // [19] Find all the hidden hints
      //if(PlayerProfile.HintObjs_3 + $HintObjectsFound >= 80 && PlayerProfile.Award_all_hint_objects_3 $= 0)   
      if(PlayerProfile.StarsMini3 >= 40 && PlayerProfile.Award_all_hint_objects_3 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[19];//"You have won an award!\nClue Master";
         %this.awardNum[%numAwards] = 19;
         PlayerProfile.Award_all_hint_objects_3 = 1;
         %numAwards++;
      }
      
      // [10] Complete a take without hitting any wrong objects
      if(!%remainingShopItems && %this.wrongObjsClicked $= 0 && PlayerProfile.Award_no_wrong_items_3 $= 0) 
      {
         %this.awardText[%numAwards] = %this.awardWon[10];//"You have won an award!\nEagle Eyed";
         %this.awardNum[%numAwards] = 10;
         PlayerProfile.Award_no_wrong_items_3 = 1;
         %numAwards++;
      }
   
   case 4:
   
      %remainingShopItems = false;   
      for(%j=1; %j<=ShoppingList.shopListSize; %j++)
      {
         if(ShoppingList.shopList[%j,"quantity"] > 0)
            %remainingShopItems = true;
      }   
   
      // [9] Find all items in a take without using any hints
      if(!%remainingShopItems && !%this.usedAnyHint && PlayerProfile.Award_no_hints_4 $= 0)
      {
         %this.awardText[%numAwards] = %this.awardWon[9];//"You have won an award!\nDetective";
         %this.awardNum[%numAwards] = 9;
         PlayerProfile.Award_no_hints_4 = 1;
         %numAwards++;
      }
      
      // [19] Find all the hidden hints
      //if(PlayerProfile.HintObjs_4 + $HintObjectsFound >= 80 && PlayerProfile.Award_all_hint_objects_4 $= 0)   
      if(PlayerProfile.StarsMini4 >= 40 && PlayerProfile.Award_all_hint_objects_4 $= 0)      
      {
         %this.awardText[%numAwards] = %this.awardWon[19];//"You have won an award!\nClue Master";
         %this.awardNum[%numAwards] = 19;
         PlayerProfile.Award_all_hint_objects_4 = 1;
         %numAwards++;
      }
      
      // [10] Complete a take without hitting any wrong objects
      if(!%remainingShopItems && %this.wrongObjsClicked $= 0 && PlayerProfile.Award_no_wrong_items_4 $= 0) 
      {
         %this.awardText[%numAwards] = %this.awardWon[10];//"You have won an award!\nEagle Eyed";
         %this.awardNum[%numAwards] = 10;
         PlayerProfile.Award_no_wrong_items_4 = 1;
         %numAwards++;
      }
   }
      
   if(%numAwards > 0)
      SavePlayerProfile();

   for(%i=0; %i < %numAwards; %i++)
   {
      %text = %this.awardText[%i];      
      %awardNum = %this.awardNum[%i];
      if(%text $= "")
         %text =  "      " @ %this.awardGeneric/*"You have won an award!"*/ @  "      ";
      
      scheduleObjLCA(SceneGui, "HO", %time, "showUnlockedAward", 2, %text, %awardNum);     
//      scheduleObjLCA(AwardRoom, "HO", %time, "showUnlockedAward", 1, %text);
      %time += 3000;
   }
   
   return %time;
}

// Parse XML file looking for award tags
function SceneGui::loadAwardXML(%this)
{
   %path = "./scriptsAndAssets/data/ui/award.xml";
   
   %pXml = new SimXMLDocument();
   // Load and parse XML file.
   if(!%pXml.loadFile(%path))
   {
      echo("Failed to load " @ %path);
      return;
   }
   
   // Push the first child element named "award" onto 
   // the XML document stack.
   if(!%pXml.pushFirstChildElement("award"))
   {
      echo("Failed to load award tag.");
      return;
   }
   
   // Get award win texts
   if(!%pXml.pushFirstChildElement("awardgeneric" @ %i))
   {
      echo("Failed to load awardgeneric tag.");
      return;
   }
   %this.awardGeneric = %pXml.attribute("text");
   %pXml.popElement();
   
   for(%i=9; %i<=22; %i++)
   {
      if(%i $= 21) continue; // Ignore the 21th
      
      if(!%pXml.pushFirstChildElement("awardwon" @ %i))
      {
         echo("Failed to load awardwon" @ %i @ " tag.");
         return;
      }
      %this.awardWon[%i] = %pXml.attribute("text");
      %pXml.popElement();
   }   
	
   %pXml.popElement();
   
   %pXml.delete();   
   
   echo("Debug: Award SceneGui XML loaded");
}
