home *** CD-ROM | disk | FTP | other *** search
/ Champak 48 / cdrom_image.iso / Games / breakawish.swf / scripts / __Packages / Game.as < prev    next >
Encoding:
Text File  |  2007-09-28  |  12.7 KB  |  437 lines

  1. class Game extends Library.DispatcherBase
  2. {
  3.    var mcRef;
  4.    var bFirstPlay;
  5.    var bShowHint;
  6.    var nFrameHint;
  7.    var oMenuPopUp;
  8.    var oHelpPopUp;
  9.    var oConfirmPopUp;
  10.    var oHintPopUp;
  11.    var oPlayerStatus;
  12.    var oGameTimer;
  13.    var nBGTargetPosition;
  14.    var bMoveBG;
  15.    var oHelpKeyManager;
  16.    var oEE;
  17.    var nHintHideDelay;
  18.    var oLevel;
  19.    var sCurrentSection;
  20.    var sTargetSection;
  21.    var oMusicGame;
  22.    static var oCtrl;
  23.    static var sSECTION_EMPTY = "Empty";
  24.    static var sSECTION_GAME = "Game";
  25.    static var nHINT_HIDE_DELAY = 75;
  26.    static var nSPEED_BACKGROUND_MOVE = 12;
  27.    function Game(_mcRef)
  28.    {
  29.       super();
  30.       this.mcRef = _mcRef;
  31.       Game.oCtrl = this;
  32.       this.bFirstPlay = true;
  33.       this.bShowHint = true;
  34.       this.nFrameHint = 1;
  35.       this.oMenuPopUp = new Library.BasicPopUp(this.mcRef.mcPopups.mcMenu,true);
  36.       this.oMenuPopUp.Listener = this;
  37.       Main.Instance.doAddListener(this.oMenuPopUp);
  38.       this.oHelpPopUp = new Library.BasicPopUp(this.mcRef.mcPopups.mcHelp,true);
  39.       this.oHelpPopUp.Listener = this;
  40.       Main.Instance.doAddListener(this.oHelpPopUp);
  41.       this.oConfirmPopUp = new Library.BasicPopUp(this.mcRef.mcPopups.mcConfirm,true);
  42.       this.oConfirmPopUp.Listener = this;
  43.       Main.Instance.doAddListener(this.oConfirmPopUp);
  44.       this.oHintPopUp = new Library.BasicPopUp(this.mcRef.mcPopups.mcHints,false);
  45.       this.oHintPopUp.Listener = this;
  46.       this.doAddListener(this.oHintPopUp);
  47.       this.oHintPopUp.doHide();
  48.       this.oPlayerStatus = new PlayerStatus();
  49.       this.oPlayerStatus.setHud(this.mcRef.mcHud);
  50.       this.oGameTimer = new Library.Utils.Timer();
  51.       this.oGameTimer.FrameRate = Main.nFRAME_RATE;
  52.       this.oGameTimer.Method = Library.Utils.Timer.TIMER_COUNT_UP;
  53.       this.doAddListener(this.oGameTimer);
  54.       Main.Instance.doAddListener(this);
  55.       this.nBGTargetPosition = this.mcRef.mcBackgrounds._y;
  56.       this.bMoveBG = false;
  57.       this.oHelpKeyManager = new Library.Utils.KeysManager();
  58.       this.oHelpKeyManager.setListenerForKey(this,32);
  59.       this.oHelpKeyManager.setListenerForKey(this,69);
  60.       this.oEE = new EE(this.mcRef.mcEE);
  61.       this.doSetSection(Game.sSECTION_EMPTY);
  62.    }
  63.    static function get Instance()
  64.    {
  65.       return Game.oCtrl;
  66.    }
  67.    function onKeyManagerEvent(_nEvent, _nCode)
  68.    {
  69.       switch(_nEvent)
  70.       {
  71.          case Library.Utils.KeysManager.EVENT_KEY_DOWN:
  72.             if(_nCode !== 32)
  73.             {
  74.             }
  75.             break;
  76.          case Library.Utils.KeysManager.EVENT_KEY_UP:
  77.             switch(_nCode)
  78.             {
  79.                case 69:
  80.                   this.oEE.doStart();
  81.                   break;
  82.                case 32:
  83.                   if(this.oHelpPopUp.CurrentState == "Idle")
  84.                   {
  85.                      this.doCloseHelp();
  86.                   }
  87.                   Main.Instance.doCloseHSIfOpen();
  88.             }
  89.       }
  90.    }
  91.    function doEnterFrame()
  92.    {
  93.       super.doEnterFrame();
  94.       this.nHintHideDelay = this.nHintHideDelay - 1;
  95.       if(this.nHintHideDelay == 0)
  96.       {
  97.          this.oHintPopUp.doClose();
  98.       }
  99.       if(this.bMoveBG)
  100.       {
  101.          this.doMoveBG();
  102.       }
  103.    }
  104.    function onPlayerCatchItem(_nItemFrame)
  105.    {
  106.       this.nFrameHint = _nItemFrame;
  107.       this.oHintPopUp.Ref.gotoAndStop(this.nFrameHint);
  108.       if(this.oHintPopUp.CurrentState != "Idle" && this.bShowHint)
  109.       {
  110.          this.oHintPopUp.doOpen();
  111.       }
  112.       this.nHintHideDelay = Game.nHINT_HIDE_DELAY;
  113.    }
  114.    function onBeginGame()
  115.    {
  116.       this.doSetSection(Game.sSECTION_GAME);
  117.    }
  118.    function onConfirmAnswer(_bAnswer)
  119.    {
  120.       if(_bAnswer)
  121.       {
  122.          Main.Instance.onPlayerLose();
  123.       }
  124.       this.oConfirmPopUp.doClose();
  125.    }
  126.    function doLevelEnd(_bWon)
  127.    {
  128.       this.oGameTimer.doStopTimer();
  129.       this.oLevel.doPause();
  130.       if(_bWon)
  131.       {
  132.          Library.Sound.SoundManager.doPlaySoundInCat(Main.sSOUND_CAT_SOUND,"Level_Completed.mp3",125);
  133.          this.oPlayerStatus.onPlayerCompleteLevel(this.oGameTimer.Time);
  134.          if(this.oPlayerStatus.getIsMoreLevelToPlay())
  135.          {
  136.             this.doSetSection(Game.sSECTION_EMPTY);
  137.          }
  138.          else
  139.          {
  140.             Main.Instance.onPlayerWin();
  141.          }
  142.       }
  143.       else
  144.       {
  145.          Main.Instance.onPlayerLose();
  146.       }
  147.    }
  148.    function onTransitionEvent(_nEvent, _oTransition)
  149.    {
  150.       switch(_nEvent)
  151.       {
  152.          case Library.Transition.TRANSITION_SCREEN_COVERED:
  153.             this.doUnloadSection(this.sCurrentSection);
  154.             this.doSetSection(this.sTargetSection);
  155.             break;
  156.          case Library.Transition.TRANSITION_COMPLETE:
  157.       }
  158.    }
  159.    function onPopUpEvent(_nEvent, _oPopUp)
  160.    {
  161.       if(_nEvent === Library.BasicPopUp.NEED_UPDATE)
  162.       {
  163.          switch(_oPopUp)
  164.          {
  165.             case this.oHelpPopUp:
  166.                this.doUpdateHelpPopUp();
  167.                break;
  168.             case this.oHintPopUp:
  169.                this.oHintPopUp.Ref.gotoAndStop(this.nFrameHint);
  170.                break;
  171.             case this.oMenuPopUp:
  172.                this.doUpdateMenuPopUp();
  173.                break;
  174.             case this.oConfirmPopUp:
  175.                this.doUpdateConfirmPopUp();
  176.          }
  177.       }
  178.    }
  179.    function doSoundEvent(_nEvent, _oSound)
  180.    {
  181.       if(_nEvent === Library.Sound.SoundManager.EVENT_SOUND_COMPLETE)
  182.       {
  183.          if(_oSound == this.oMusicGame)
  184.          {
  185.             delete this.oMusicGame;
  186.          }
  187.       }
  188.    }
  189.    function doToggleShowHint()
  190.    {
  191.       this.bShowHint = !this.bShowHint;
  192.       this.doUpdateHintGroups();
  193.       if(!this.bShowHint && this.oHintPopUp.CurrentState != "Hidden")
  194.       {
  195.          this.oHintPopUp.doClose();
  196.       }
  197.    }
  198.    function doToggleMuteFor(_sCategory)
  199.    {
  200.       if(Library.Sound.SoundManager.isCategoryMuted(_sCategory))
  201.       {
  202.          Library.Sound.SoundManager.doUnMuteCategory(_sCategory);
  203.       }
  204.       else
  205.       {
  206.          Library.Sound.SoundManager.doMuteCategory(_sCategory);
  207.       }
  208.       this.doUpdateMenuSoundGroups();
  209.    }
  210.    function doShowMenu()
  211.    {
  212.       this.oLevel.doPause();
  213.       this.oMenuPopUp.doOpen();
  214.       this.oGameTimer.doStopTimer();
  215.    }
  216.    function doCloseMenu()
  217.    {
  218.       this.oLevel.doResume();
  219.       this.oMenuPopUp.doClose();
  220.       this.oGameTimer.doStartTimer();
  221.    }
  222.    function doCloseHelp()
  223.    {
  224.       this.oHelpPopUp.doClose();
  225.    }
  226.    function doShowHelp()
  227.    {
  228.       this.oHelpPopUp.doOpen();
  229.    }
  230.    function doViewHS()
  231.    {
  232.       Main.Instance.doShowInGameHS();
  233.    }
  234.    function doQuitGame()
  235.    {
  236.       this.oConfirmPopUp.doOpen();
  237.    }
  238.    function doDestroy()
  239.    {
  240.       this.oLevel.doDestroy();
  241.       delete this.oLevel;
  242.       Main.Instance.doRemoveListener(this);
  243.       delete this.oGameTimer;
  244.       this.oMenuPopUp.doHide();
  245.       this.oMenuPopUp.doDestroy();
  246.       delete this.oMenuPopUp;
  247.       this.oHelpPopUp.doDestroy();
  248.       delete this.oHelpPopUp;
  249.       this.oHintPopUp.doDestroy();
  250.       delete this.oHintPopUp;
  251.       this.oConfirmPopUp.doDestroy();
  252.       delete this.oConfirmPopUp;
  253.       this.oHelpKeyManager.doDestroy();
  254.       delete this.oHelpKeyManager;
  255.       this.doStopMusic();
  256.       Main.Instance.doStartPackMusic();
  257.       this.oMusicGame.doDestroy();
  258.       delete Game.oCtrl;
  259.       this.oPlayerStatus;
  260.    }
  261.    function get Status()
  262.    {
  263.       return this.oPlayerStatus;
  264.    }
  265.    function doUpdateHelpPopUp()
  266.    {
  267.       this.oHelpPopUp.Ref.btnResume.onRelease = Library.Utils.Delegate.create(this,this.doCloseHelp);
  268.    }
  269.    function doUpdateConfirmPopUp()
  270.    {
  271.       this.oConfirmPopUp.Ref.btnYes.onRelease = Library.Utils.Delegate.create(this,this.onConfirmAnswer,true);
  272.       this.oConfirmPopUp.Ref.btnNo.onRelease = Library.Utils.Delegate.create(this,this.onConfirmAnswer,false);
  273.    }
  274.    function doUpdateMenuPopUp()
  275.    {
  276.       this.oMenuPopUp.Ref.btnViewHS.onRelease = Library.Utils.Delegate.create(this,this.doViewHS);
  277.       this.oMenuPopUp.Ref.btnResume.onRelease = Library.Utils.Delegate.create(this,this.doCloseMenu);
  278.       this.oMenuPopUp.Ref.btnHelp.onRelease = Library.Utils.Delegate.create(this,this.doShowHelp);
  279.       this.oMenuPopUp.Ref.btnQuit.onRelease = Library.Utils.Delegate.create(this,this.doQuitGame);
  280.       this.doUpdateMenuSoundGroups();
  281.       this.doUpdateHintGroups();
  282.    }
  283.    function doUpdateHintGroups()
  284.    {
  285.       var _loc2_ = "On";
  286.       if(!this.bShowHint)
  287.       {
  288.          _loc2_ = "Off";
  289.       }
  290.       this.oMenuPopUp.Ref.mcHintToggle.gotoAndStop(_loc2_);
  291.       this.oMenuPopUp.Ref.mcHintToggle.btnToggle.onRelease = Library.Utils.Delegate.create(this,this.doToggleShowHint);
  292.    }
  293.    function doUpdateMenuSoundGroups()
  294.    {
  295.       var _loc2_ = "On";
  296.       if(Library.Sound.SoundManager.isCategoryMuted(Main.sSOUND_CAT_SOUND))
  297.       {
  298.          _loc2_ = "Off";
  299.       }
  300.       var _loc3_ = "On";
  301.       if(Library.Sound.SoundManager.isCategoryMuted(Main.sSOUND_CAT_MUSIC))
  302.       {
  303.          _loc3_ = "Off";
  304.       }
  305.       this.oMenuPopUp.Ref.mcSoundToggle.gotoAndStop(_loc2_);
  306.       this.oMenuPopUp.Ref.mcMusicToggle.gotoAndStop(_loc3_);
  307.       this.oMenuPopUp.Ref.mcSoundToggle.btnToggle.onRelease = Library.Utils.Delegate.create(this,this.doToggleMuteFor,Main.sSOUND_CAT_SOUND);
  308.       this.oMenuPopUp.Ref.mcMusicToggle.btnToggle.onRelease = Library.Utils.Delegate.create(this,this.doToggleMuteFor,Main.sSOUND_CAT_MUSIC);
  309.    }
  310.    function doCreateLevel()
  311.    {
  312.       var _loc2_ = this.oPlayerStatus.getLevelToPlay();
  313.       this.doSetBGPosition(_loc2_);
  314.       _loc2_ += 1;
  315.       this.oLevel = new LevelManager(this.mcRef.mcGame,_loc2_);
  316.       this.mcRef.mcHud.btnMenu.onRelease = Library.Utils.Delegate.create(this,this.doShowMenu);
  317.       this.oGameTimer.setTime(PlayerStatus.nTIME_FOR_EXTRA_POINTS);
  318.       this.oGameTimer.doStartTimer();
  319.       Main.Instance.doStopPackMusic();
  320.       this.doStartMusic();
  321.       if(this.bFirstPlay)
  322.       {
  323.          this.oHelpPopUp.doOpen();
  324.       }
  325.       this.bFirstPlay = false;
  326.    }
  327.    function doStartMusic()
  328.    {
  329.       if(this.oMusicGame == undefined)
  330.       {
  331.          this.oMusicGame = Library.Sound.SoundManager.doPlaySoundInCat(Main.sSOUND_CAT_MUSIC,"Music_Game.wav",0,999999);
  332.          this.oMusicGame.doAddListener(this);
  333.          this.oMusicGame.setFadeRate(8);
  334.       }
  335.       this.oMusicGame.doFadeTo(100);
  336.    }
  337.    function doStopMusic()
  338.    {
  339.       if(this.oMusicGame != undefined)
  340.       {
  341.          this.oMusicGame.setFadeRate(8);
  342.          this.oMusicGame.doFadeTo(0);
  343.       }
  344.    }
  345.    function doSetBGPosition(_nLevelToPlay)
  346.    {
  347.       var _loc2_ = undefined;
  348.       switch(_nLevelToPlay)
  349.       {
  350.          case 1:
  351.          case 2:
  352.          case 3:
  353.             _loc2_ = -3998;
  354.             break;
  355.          case 4:
  356.          case 5:
  357.             _loc2_ = -3500;
  358.             break;
  359.          case 6:
  360.          case 7:
  361.             _loc2_ = -3040;
  362.             break;
  363.          case 8:
  364.          case 9:
  365.             _loc2_ = -2680;
  366.             break;
  367.          case 10:
  368.          case 11:
  369.             _loc2_ = -2220;
  370.             break;
  371.          case 12:
  372.          case 13:
  373.             _loc2_ = -1700;
  374.             break;
  375.          case 14:
  376.          case 15:
  377.             _loc2_ = -1220;
  378.             break;
  379.          case 16:
  380.          case 17:
  381.             _loc2_ = -490;
  382.             break;
  383.          case 18:
  384.          case 19:
  385.          case 20:
  386.             _loc2_ = -10;
  387.       }
  388.       if(this.nBGTargetPosition != _loc2_)
  389.       {
  390.          this.nBGTargetPosition = _loc2_;
  391.          this.bMoveBG = true;
  392.       }
  393.    }
  394.    function doMoveBG()
  395.    {
  396.       var _loc2_ = this.nBGTargetPosition - this.mcRef.mcBackgrounds._y;
  397.       var _loc3_ = Library.Utils.MoreMath.getPolarity(_loc2_);
  398.       if(Math.abs(_loc2_) > Game.nSPEED_BACKGROUND_MOVE)
  399.       {
  400.          var _loc4_ = _loc3_ * Game.nSPEED_BACKGROUND_MOVE;
  401.          this.mcRef.mcBackgrounds._y += _loc4_;
  402.       }
  403.       else
  404.       {
  405.          this.mcRef.mcBackgrounds._y = this.nBGTargetPosition;
  406.       }
  407.    }
  408.    function doUnloadLevel()
  409.    {
  410.       this.oLevel.doDestroy();
  411.       delete this.oLevel;
  412.    }
  413.    function doSetSection(_sSection)
  414.    {
  415.       this.sCurrentSection = _sSection;
  416.       this.mcRef.gotoAndStop(this.sCurrentSection);
  417.       switch(this.sCurrentSection)
  418.       {
  419.          case Game.sSECTION_GAME:
  420.             this.doCreateLevel();
  421.             break;
  422.          case Game.sSECTION_EMPTY:
  423.             if(!this.bFirstPlay)
  424.             {
  425.                this.doSetSection(Game.sSECTION_GAME);
  426.             }
  427.       }
  428.    }
  429.    function doUnloadSection(_sSection)
  430.    {
  431.       if(_sSection === Game.sSECTION_GAME)
  432.       {
  433.          this.doUnloadLevel();
  434.       }
  435.    }
  436. }
  437.