home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Classicos / smashout.swf / scripts / __Packages / Game / PongGame.as < prev    next >
Encoding:
Text File  |  2005-11-09  |  12.6 KB  |  474 lines

  1. class Game.PongGame
  2. {
  3.    var player;
  4.    var opponent;
  5.    var side;
  6.    var humanSide;
  7.    var computerSide;
  8.    var timeline;
  9.    var world;
  10.    var __scoreboard;
  11.    var __engine;
  12.    var __levelData;
  13.    var __goalUpdate;
  14.    var __itemLeft;
  15.    var __itemRight;
  16.    var dialog;
  17.    var __blinkSide;
  18.    var __blinkTimer;
  19.    var __frameElapsedTime;
  20.    var __blinkGoalDisp;
  21.    static var LEVELS;
  22.    static var LEFT_PLAYER = "left";
  23.    static var RIGHT_PLAYER = "right";
  24.    static var GOALS_PER_LEVEL = 3;
  25.    static var FRIENDS = Game.PongLevels.FRIENDS;
  26.    static var FOES = Game.PongLevels.FOES;
  27.    static var DIALOG_LEVEL = 16000;
  28.    var dialogHelpID = "DialogHelp";
  29.    var dialogPauseID = "DialogPause";
  30.    var dialogQuitID = "DialogQuit";
  31.    var dialogLevelDoneID = "DialogLevelDone";
  32.    var dialogGameOverID = "DialogGameOver";
  33.    var __score = 0;
  34.    var __level = 0;
  35.    static var FRAME_TIME = Game.PongLevels.FRAME_TIME;
  36.    var __blinkTime = 1;
  37.    static var __inited = false;
  38.    function PongGame(myTimeline, myEngine, myScoreboard, setup)
  39.    {
  40.       if(!Game.PongGame.__inited)
  41.       {
  42.          this.init();
  43.       }
  44.       this.player = setup.player;
  45.       this.opponent = setup.opponent;
  46.       this.side = setup.side;
  47.       if(this.side == 1)
  48.       {
  49.          this.humanSide = Game.PongGame.RIGHT_PLAYER;
  50.          this.computerSide = Game.PongGame.LEFT_PLAYER;
  51.       }
  52.       else
  53.       {
  54.          this.humanSide = Game.PongGame.LEFT_PLAYER;
  55.          this.computerSide = Game.PongGame.RIGHT_PLAYER;
  56.       }
  57.       this.level = setup.startLevel == undefined ? 0 : setup.startLevel;
  58.       this.timeline = myTimeline;
  59.       this.scoreboard = myScoreboard;
  60.       this.engine = myEngine;
  61.    }
  62.    function reset()
  63.    {
  64.       this.level = 0;
  65.       this.score = 0;
  66.       this.scoreboard.resetGoals();
  67.       this.world.clearLevel();
  68.    }
  69.    function set scoreboard(myScoreboard)
  70.    {
  71.       this.__scoreboard = myScoreboard;
  72.       this.__scoreboard.score = this.score;
  73.       this.__scoreboard.level = this.level;
  74.       if(this.side == 1)
  75.       {
  76.          this.__scoreboard.playerLeft = Game.PongGame.FOES[this.opponent];
  77.          this.__scoreboard.playerRight = Game.PongGame.FRIENDS[this.player];
  78.       }
  79.       else
  80.       {
  81.          this.__scoreboard.playerLeft = Game.PongGame.FRIENDS[this.player];
  82.          this.__scoreboard.playerRight = Game.PongGame.FOES[this.opponent];
  83.       }
  84.       this.__scoreboard.addEventListener("click",this);
  85.    }
  86.    function get scoreboard()
  87.    {
  88.       return this.__scoreboard;
  89.    }
  90.    function set engine(myEngine)
  91.    {
  92.       this.__engine = myEngine;
  93.       this.world = new Engine.PongWorld();
  94.       this.world.humanSide = this.humanSide;
  95.       this.world.addEventListener("onGameUpdate",this);
  96.       this.world.addEventListener("onGoal",this);
  97.       this.world.addEventListener("onPoints",this);
  98.       this.world.addEventListener("onWorldDisplay",this);
  99.       this.world.addEventListener("onActivateItem",this);
  100.       this.world.addEventListener("onBlinkItem",this);
  101.       this.world.addEventListener("onBallInPlay",this);
  102.       this.__engine.addWorld(this.world);
  103.       this.__engine.setActiveWorld(this.world);
  104.       this.__engine.start();
  105.    }
  106.    function get engine()
  107.    {
  108.       return this.__engine;
  109.    }
  110.    function set score(newScore)
  111.    {
  112.       if(newScore < 0)
  113.       {
  114.          newScore = 0;
  115.       }
  116.       this.__score = newScore;
  117.       this.__scoreboard.score = this.__score;
  118.    }
  119.    function get score()
  120.    {
  121.       return this.__score;
  122.    }
  123.    function set level(newLevel)
  124.    {
  125.       this.__level = newLevel;
  126.       this.__scoreboard.level = newLevel;
  127.       while(newLevel > Game.PongGame.LEVELS.length - 1)
  128.       {
  129.          newLevel -= Game.PongGame.LEVELS.length;
  130.       }
  131.       this.__levelData = Game.PongGame.LEVELS[newLevel];
  132.    }
  133.    function get level()
  134.    {
  135.       return this.__level;
  136.    }
  137.    function startLevel()
  138.    {
  139.       this.scoreboard.resetGoals();
  140.       this.engine.resume();
  141.       this.world.clearLevel();
  142.       this.world.createLevel(this.level,this.__levelData);
  143.    }
  144.    function onWorldDisplay(eventObj)
  145.    {
  146.       this.startLevel();
  147.    }
  148.    function onGameUpdate(eventObj)
  149.    {
  150.       var _loc4_ = eventObj.elapsed;
  151.       this.checkBlinkLights(_loc4_);
  152.       if(Key.isDown(80))
  153.       {
  154.          this.click({data:"pause"});
  155.          return undefined;
  156.       }
  157.       if(Key.isDown(17) && Key.isDown(16))
  158.       {
  159.          if(Key.isDown(48))
  160.          {
  161.             if(this.level != 9)
  162.             {
  163.                this.level = 9;
  164.                stopAllSounds();
  165.                this.startLevel();
  166.             }
  167.          }
  168.          var _loc2_ = undefined;
  169.          _loc2_ = 49;
  170.          while(_loc2_ <= 57)
  171.          {
  172.             if(Key.isDown(_loc2_))
  173.             {
  174.                if(this.level != _loc2_ - 49)
  175.                {
  176.                   this.level = _loc2_ - 49;
  177.                   stopAllSounds();
  178.                   this.startLevel();
  179.                }
  180.                break;
  181.             }
  182.             _loc2_ = _loc2_ + 1;
  183.          }
  184.          if(Key.isDown(77))
  185.          {
  186.             var _loc3_ = new Sound(this.timeline);
  187.             _loc3_.setVolume(0);
  188.             return undefined;
  189.          }
  190.          if(Key.isDown(78))
  191.          {
  192.             _loc3_ = new Sound(this.timeline);
  193.             _loc3_.setVolume(100);
  194.             return undefined;
  195.          }
  196.       }
  197.    }
  198.    function onGoal(eventObj)
  199.    {
  200.       var _loc2_ = eventObj.who;
  201.       var _loc3_ = undefined;
  202.       if(_loc2_ == Game.PongGame.LEFT_PLAYER)
  203.       {
  204.          _loc3_ = this.__scoreboard.goalsLeft = this.__scoreboard.goalsLeft + 1;
  205.       }
  206.       else if(_loc2_ == Game.PongGame.RIGHT_PLAYER)
  207.       {
  208.          _loc3_ = this.__scoreboard.goalsRight = this.__scoreboard.goalsRight + 1;
  209.       }
  210.       this.__goalUpdate = {who:_loc2_,goals:_loc3_};
  211.       this.startBlinkLights(_loc2_);
  212.    }
  213.    function reactToGoal()
  214.    {
  215.       if(this.__goalUpdate == undefined)
  216.       {
  217.          return undefined;
  218.       }
  219.       if(this.__goalUpdate.goals < Game.PongGame.GOALS_PER_LEVEL)
  220.       {
  221.          delete this.__goalUpdate;
  222.          this.world.getReady();
  223.          return undefined;
  224.       }
  225.       if(this.__goalUpdate.who == this.humanSide)
  226.       {
  227.          delete this.__goalUpdate;
  228.          this.levelDone();
  229.          return undefined;
  230.       }
  231.       delete this.__goalUpdate;
  232.       this.gameOver();
  233.    }
  234.    function onPoints(eventObj)
  235.    {
  236.       var _loc3_ = eventObj.points;
  237.       var _loc4_ = eventObj.who;
  238.       var _loc2_ = eventObj.source;
  239.       if(_loc4_ == this.humanSide)
  240.       {
  241.          if(_loc2_ == "brick" || _loc2_ == "bonuspoints" || _loc2_ == "goal" || _loc2_ == "gooditem")
  242.          {
  243.             this.score += _loc3_;
  244.          }
  245.          return undefined;
  246.       }
  247.       if(_loc2_ == "bonuspoints")
  248.       {
  249.          this.score -= _loc3_;
  250.          return undefined;
  251.       }
  252.       if(_loc2_ == "brick")
  253.       {
  254.          this.score += _loc3_;
  255.       }
  256.    }
  257.    function onActivateItem(eventObj)
  258.    {
  259.       var _loc3_ = eventObj.itemID;
  260.       var _loc2_ = eventObj.who;
  261.       if(_loc3_ > -1)
  262.       {
  263.          this.toggleLight(_loc2_,true);
  264.       }
  265.       else
  266.       {
  267.          this.toggleLight(_loc2_,false);
  268.       }
  269.       if(_loc2_ == Game.PongGame.LEFT_PLAYER)
  270.       {
  271.          this.__scoreboard.itemLeft = this.__itemLeft = _loc3_ + 1;
  272.       }
  273.       else
  274.       {
  275.          this.__scoreboard.itemRight = this.__itemRight = _loc3_ + 1;
  276.       }
  277.    }
  278.    function onBlinkItem(eventObj)
  279.    {
  280.       var _loc2_ = eventObj.who;
  281.       if(_loc2_ == Game.PongGame.LEFT_PLAYER)
  282.       {
  283.          if(this.__scoreboard.itemLeft == this.__itemLeft)
  284.          {
  285.             this.__scoreboard.itemLeft = 0;
  286.          }
  287.          else
  288.          {
  289.             this.__scoreboard.itemLeft = this.__itemLeft;
  290.          }
  291.       }
  292.       else if(this.__scoreboard.itemRight == this.__itemRight)
  293.       {
  294.          this.__scoreboard.itemRight = 0;
  295.       }
  296.       else
  297.       {
  298.          this.__scoreboard.itemRight = this.__itemRight;
  299.       }
  300.    }
  301.    function click(eventObj)
  302.    {
  303.       this.__engine.pause();
  304.       var _loc3_ = new Sound(this.world.target);
  305.       _loc3_.setVolume(0);
  306.       if(eventObj.data == "pause")
  307.       {
  308.          this.dialog = this.createDialog(this.dialogPauseID);
  309.          return undefined;
  310.       }
  311.       if(eventObj.data == "help")
  312.       {
  313.          this.dialog = this.createDialog(this.dialogHelpID);
  314.          return undefined;
  315.       }
  316.       if(eventObj.data == "quit")
  317.       {
  318.          this.dialog = this.createDialog(this.dialogQuitID);
  319.          this.dialog.score = this.score;
  320.          this.dialog.addEventListener("onSubmitScore",this);
  321.          return undefined;
  322.       }
  323.    }
  324.    function onSubmitScore(eventObj)
  325.    {
  326.       eventObj.target.removeMovieClip();
  327.       stopAllSounds();
  328.       this.engine.stop();
  329.       this.timeline.submitScore(this.score);
  330.    }
  331.    function closeModal(eventObj)
  332.    {
  333.       var _loc2_ = eventObj.name;
  334.       if(_loc2_ == "help" || _loc2_ == "quit" || _loc2_ == "pause")
  335.       {
  336.          this.__engine.resume();
  337.          var _loc3_ = new Sound(this.world.target);
  338.          _loc3_.setVolume(100);
  339.          return undefined;
  340.       }
  341.       if(_loc2_ == "leveldone")
  342.       {
  343.          this.level = this.level + 1;
  344.          this.startLevel();
  345.          return undefined;
  346.       }
  347.       if(_loc2_ == "gameover")
  348.       {
  349.          stopAllSounds();
  350.          this.engine.stop();
  351.          this.timeline.newGame();
  352.          return undefined;
  353.       }
  354.    }
  355.    function toString()
  356.    {
  357.       return "PongGame";
  358.    }
  359.    function createDialog(id)
  360.    {
  361.       if(this.dialog)
  362.       {
  363.          this.dialog.close();
  364.       }
  365.       _root._quality = "BEST";
  366.       this.dialog = this.timeline.attachMovie(id,"dialog",Game.PongGame.DIALOG_LEVEL);
  367.       this.dialog.addEventListener("closeModal",this);
  368.       return this.dialog;
  369.    }
  370.    function levelDone()
  371.    {
  372.       this.world.active = false;
  373.       this.world.clearLevel();
  374.       this.engine.pause();
  375.       this.dialog = this.createDialog(this.dialogLevelDoneID);
  376.       this.dialog.level = this.level + 1;
  377.    }
  378.    function gameOver()
  379.    {
  380.       this.world.active = false;
  381.       this.engine.pause();
  382.       this.dialog = this.createDialog(this.dialogGameOverID);
  383.       this.dialog.score = this.score;
  384.       this.dialog.addEventListener("onSubmitScore",this);
  385.    }
  386.    function checkBlinkLights(elapsed)
  387.    {
  388.       if(this.__blinkSide == null)
  389.       {
  390.          return undefined;
  391.       }
  392.       this.__blinkTimer += elapsed;
  393.       var _loc2_ = this.__blinkTime - this.__blinkTimer;
  394.       if(_loc2_ > 0)
  395.       {
  396.          this.__frameElapsedTime += elapsed;
  397.          if(this.__frameElapsedTime >= Game.PongGame.FRAME_TIME * 2)
  398.          {
  399.             this.__frameElapsedTime -= Game.PongGame.FRAME_TIME * 2;
  400.             this.toggleLight(this.__blinkSide);
  401.             this.toggleGoalDisp(this.__blinkGoalDisp);
  402.          }
  403.       }
  404.       else
  405.       {
  406.          this.stopBlinkLights();
  407.       }
  408.    }
  409.    function startBlinkLights(who)
  410.    {
  411.       this.__blinkTimer = 0;
  412.       this.__frameElapsedTime = 0;
  413.       this.__blinkSide = who != Game.PongGame.LEFT_PLAYER ? Game.PongGame.LEFT_PLAYER : Game.PongGame.RIGHT_PLAYER;
  414.       this.__blinkGoalDisp = who;
  415.    }
  416.    function onBallInPlay()
  417.    {
  418.       this.stopBlinkLights();
  419.    }
  420.    function stopBlinkLights()
  421.    {
  422.       this.toggleLight(this.__blinkSide,false);
  423.       this.__blinkSide = null;
  424.       this.toggleGoalDisp(this.__blinkGoalDisp,true);
  425.       this.__blinkGoalDisp = null;
  426.       this.reactToGoal();
  427.    }
  428.    function toggleGoalDisp(side, force)
  429.    {
  430.       var _loc2_ = undefined;
  431.       if(side == Game.PongGame.LEFT_PLAYER)
  432.       {
  433.          _loc2_ = this.__scoreboard.goalsLeft_txt;
  434.       }
  435.       else
  436.       {
  437.          _loc2_ = this.__scoreboard.goalsRight_txt;
  438.       }
  439.       if(force == undefined)
  440.       {
  441.          _loc2_._visible = !_loc2_._visible ? true : false;
  442.       }
  443.       else
  444.       {
  445.          _loc2_._visible = force;
  446.       }
  447.    }
  448.    function toggleLight(side, force)
  449.    {
  450.       var _loc2_ = undefined;
  451.       if(side == Game.PongGame.LEFT_PLAYER)
  452.       {
  453.          _loc2_ = this.timeline.arena_mc.activeLeft_mc;
  454.       }
  455.       else
  456.       {
  457.          _loc2_ = this.timeline.arena_mc.activeRight_mc;
  458.       }
  459.       if(force == undefined)
  460.       {
  461.          _loc2_.gotoAndStop(_loc2_._currentframe != 1 ? 1 : 3);
  462.       }
  463.       else
  464.       {
  465.          _loc2_.gotoAndStop(!force ? 1 : 3);
  466.       }
  467.    }
  468.    function init()
  469.    {
  470.       Game.PongGame.LEVELS = Game.PongLevels.getLevels();
  471.       Game.PongGame.__inited = true;
  472.    }
  473. }
  474.