home *** CD-ROM | disk | FTP | other *** search
- class Game.PongGame
- {
- var player;
- var opponent;
- var side;
- var humanSide;
- var computerSide;
- var timeline;
- var world;
- var __scoreboard;
- var __engine;
- var __levelData;
- var __goalUpdate;
- var __itemLeft;
- var __itemRight;
- var dialog;
- var __blinkSide;
- var __blinkTimer;
- var __frameElapsedTime;
- var __blinkGoalDisp;
- static var LEVELS;
- static var LEFT_PLAYER = "left";
- static var RIGHT_PLAYER = "right";
- static var GOALS_PER_LEVEL = 3;
- static var FRIENDS = Game.PongLevels.FRIENDS;
- static var FOES = Game.PongLevels.FOES;
- static var DIALOG_LEVEL = 16000;
- var dialogHelpID = "DialogHelp";
- var dialogPauseID = "DialogPause";
- var dialogQuitID = "DialogQuit";
- var dialogLevelDoneID = "DialogLevelDone";
- var dialogGameOverID = "DialogGameOver";
- var __score = 0;
- var __level = 0;
- static var FRAME_TIME = Game.PongLevels.FRAME_TIME;
- var __blinkTime = 1;
- static var __inited = false;
- function PongGame(myTimeline, myEngine, myScoreboard, setup)
- {
- if(!Game.PongGame.__inited)
- {
- this.init();
- }
- this.player = setup.player;
- this.opponent = setup.opponent;
- this.side = setup.side;
- if(this.side == 1)
- {
- this.humanSide = Game.PongGame.RIGHT_PLAYER;
- this.computerSide = Game.PongGame.LEFT_PLAYER;
- }
- else
- {
- this.humanSide = Game.PongGame.LEFT_PLAYER;
- this.computerSide = Game.PongGame.RIGHT_PLAYER;
- }
- this.level = setup.startLevel == undefined ? 0 : setup.startLevel;
- this.timeline = myTimeline;
- this.scoreboard = myScoreboard;
- this.engine = myEngine;
- }
- function reset()
- {
- this.level = 0;
- this.score = 0;
- this.scoreboard.resetGoals();
- this.world.clearLevel();
- }
- function set scoreboard(myScoreboard)
- {
- this.__scoreboard = myScoreboard;
- this.__scoreboard.score = this.score;
- this.__scoreboard.level = this.level;
- if(this.side == 1)
- {
- this.__scoreboard.playerLeft = Game.PongGame.FOES[this.opponent];
- this.__scoreboard.playerRight = Game.PongGame.FRIENDS[this.player];
- }
- else
- {
- this.__scoreboard.playerLeft = Game.PongGame.FRIENDS[this.player];
- this.__scoreboard.playerRight = Game.PongGame.FOES[this.opponent];
- }
- this.__scoreboard.addEventListener("click",this);
- }
- function get scoreboard()
- {
- return this.__scoreboard;
- }
- function set engine(myEngine)
- {
- this.__engine = myEngine;
- this.world = new Engine.PongWorld();
- this.world.humanSide = this.humanSide;
- this.world.addEventListener("onGameUpdate",this);
- this.world.addEventListener("onGoal",this);
- this.world.addEventListener("onPoints",this);
- this.world.addEventListener("onWorldDisplay",this);
- this.world.addEventListener("onActivateItem",this);
- this.world.addEventListener("onBlinkItem",this);
- this.world.addEventListener("onBallInPlay",this);
- this.__engine.addWorld(this.world);
- this.__engine.setActiveWorld(this.world);
- this.__engine.start();
- }
- function get engine()
- {
- return this.__engine;
- }
- function set score(newScore)
- {
- if(newScore < 0)
- {
- newScore = 0;
- }
- this.__score = newScore;
- this.__scoreboard.score = this.__score;
- }
- function get score()
- {
- return this.__score;
- }
- function set level(newLevel)
- {
- this.__level = newLevel;
- this.__scoreboard.level = newLevel;
- while(newLevel > Game.PongGame.LEVELS.length - 1)
- {
- newLevel -= Game.PongGame.LEVELS.length;
- }
- this.__levelData = Game.PongGame.LEVELS[newLevel];
- }
- function get level()
- {
- return this.__level;
- }
- function startLevel()
- {
- this.scoreboard.resetGoals();
- this.engine.resume();
- this.world.clearLevel();
- this.world.createLevel(this.level,this.__levelData);
- }
- function onWorldDisplay(eventObj)
- {
- this.startLevel();
- }
- function onGameUpdate(eventObj)
- {
- var _loc4_ = eventObj.elapsed;
- this.checkBlinkLights(_loc4_);
- if(Key.isDown(80))
- {
- this.click({data:"pause"});
- return undefined;
- }
- if(Key.isDown(17) && Key.isDown(16))
- {
- if(Key.isDown(48))
- {
- if(this.level != 9)
- {
- this.level = 9;
- stopAllSounds();
- this.startLevel();
- }
- }
- var _loc2_ = undefined;
- _loc2_ = 49;
- while(_loc2_ <= 57)
- {
- if(Key.isDown(_loc2_))
- {
- if(this.level != _loc2_ - 49)
- {
- this.level = _loc2_ - 49;
- stopAllSounds();
- this.startLevel();
- }
- break;
- }
- _loc2_ = _loc2_ + 1;
- }
- if(Key.isDown(77))
- {
- var _loc3_ = new Sound(this.timeline);
- _loc3_.setVolume(0);
- return undefined;
- }
- if(Key.isDown(78))
- {
- _loc3_ = new Sound(this.timeline);
- _loc3_.setVolume(100);
- return undefined;
- }
- }
- }
- function onGoal(eventObj)
- {
- var _loc2_ = eventObj.who;
- var _loc3_ = undefined;
- if(_loc2_ == Game.PongGame.LEFT_PLAYER)
- {
- _loc3_ = this.__scoreboard.goalsLeft = this.__scoreboard.goalsLeft + 1;
- }
- else if(_loc2_ == Game.PongGame.RIGHT_PLAYER)
- {
- _loc3_ = this.__scoreboard.goalsRight = this.__scoreboard.goalsRight + 1;
- }
- this.__goalUpdate = {who:_loc2_,goals:_loc3_};
- this.startBlinkLights(_loc2_);
- }
- function reactToGoal()
- {
- if(this.__goalUpdate == undefined)
- {
- return undefined;
- }
- if(this.__goalUpdate.goals < Game.PongGame.GOALS_PER_LEVEL)
- {
- delete this.__goalUpdate;
- this.world.getReady();
- return undefined;
- }
- if(this.__goalUpdate.who == this.humanSide)
- {
- delete this.__goalUpdate;
- this.levelDone();
- return undefined;
- }
- delete this.__goalUpdate;
- this.gameOver();
- }
- function onPoints(eventObj)
- {
- var _loc3_ = eventObj.points;
- var _loc4_ = eventObj.who;
- var _loc2_ = eventObj.source;
- if(_loc4_ == this.humanSide)
- {
- if(_loc2_ == "brick" || _loc2_ == "bonuspoints" || _loc2_ == "goal" || _loc2_ == "gooditem")
- {
- this.score += _loc3_;
- }
- return undefined;
- }
- if(_loc2_ == "bonuspoints")
- {
- this.score -= _loc3_;
- return undefined;
- }
- if(_loc2_ == "brick")
- {
- this.score += _loc3_;
- }
- }
- function onActivateItem(eventObj)
- {
- var _loc3_ = eventObj.itemID;
- var _loc2_ = eventObj.who;
- if(_loc3_ > -1)
- {
- this.toggleLight(_loc2_,true);
- }
- else
- {
- this.toggleLight(_loc2_,false);
- }
- if(_loc2_ == Game.PongGame.LEFT_PLAYER)
- {
- this.__scoreboard.itemLeft = this.__itemLeft = _loc3_ + 1;
- }
- else
- {
- this.__scoreboard.itemRight = this.__itemRight = _loc3_ + 1;
- }
- }
- function onBlinkItem(eventObj)
- {
- var _loc2_ = eventObj.who;
- if(_loc2_ == Game.PongGame.LEFT_PLAYER)
- {
- if(this.__scoreboard.itemLeft == this.__itemLeft)
- {
- this.__scoreboard.itemLeft = 0;
- }
- else
- {
- this.__scoreboard.itemLeft = this.__itemLeft;
- }
- }
- else if(this.__scoreboard.itemRight == this.__itemRight)
- {
- this.__scoreboard.itemRight = 0;
- }
- else
- {
- this.__scoreboard.itemRight = this.__itemRight;
- }
- }
- function click(eventObj)
- {
- this.__engine.pause();
- var _loc3_ = new Sound(this.world.target);
- _loc3_.setVolume(0);
- if(eventObj.data == "pause")
- {
- this.dialog = this.createDialog(this.dialogPauseID);
- return undefined;
- }
- if(eventObj.data == "help")
- {
- this.dialog = this.createDialog(this.dialogHelpID);
- return undefined;
- }
- if(eventObj.data == "quit")
- {
- this.dialog = this.createDialog(this.dialogQuitID);
- this.dialog.score = this.score;
- this.dialog.addEventListener("onSubmitScore",this);
- return undefined;
- }
- }
- function onSubmitScore(eventObj)
- {
- eventObj.target.removeMovieClip();
- stopAllSounds();
- this.engine.stop();
- this.timeline.submitScore(this.score);
- }
- function closeModal(eventObj)
- {
- var _loc2_ = eventObj.name;
- if(_loc2_ == "help" || _loc2_ == "quit" || _loc2_ == "pause")
- {
- this.__engine.resume();
- var _loc3_ = new Sound(this.world.target);
- _loc3_.setVolume(100);
- return undefined;
- }
- if(_loc2_ == "leveldone")
- {
- this.level = this.level + 1;
- this.startLevel();
- return undefined;
- }
- if(_loc2_ == "gameover")
- {
- stopAllSounds();
- this.engine.stop();
- this.timeline.newGame();
- return undefined;
- }
- }
- function toString()
- {
- return "PongGame";
- }
- function createDialog(id)
- {
- if(this.dialog)
- {
- this.dialog.close();
- }
- _root._quality = "BEST";
- this.dialog = this.timeline.attachMovie(id,"dialog",Game.PongGame.DIALOG_LEVEL);
- this.dialog.addEventListener("closeModal",this);
- return this.dialog;
- }
- function levelDone()
- {
- this.world.active = false;
- this.world.clearLevel();
- this.engine.pause();
- this.dialog = this.createDialog(this.dialogLevelDoneID);
- this.dialog.level = this.level + 1;
- }
- function gameOver()
- {
- this.world.active = false;
- this.engine.pause();
- this.dialog = this.createDialog(this.dialogGameOverID);
- this.dialog.score = this.score;
- this.dialog.addEventListener("onSubmitScore",this);
- }
- function checkBlinkLights(elapsed)
- {
- if(this.__blinkSide == null)
- {
- return undefined;
- }
- this.__blinkTimer += elapsed;
- var _loc2_ = this.__blinkTime - this.__blinkTimer;
- if(_loc2_ > 0)
- {
- this.__frameElapsedTime += elapsed;
- if(this.__frameElapsedTime >= Game.PongGame.FRAME_TIME * 2)
- {
- this.__frameElapsedTime -= Game.PongGame.FRAME_TIME * 2;
- this.toggleLight(this.__blinkSide);
- this.toggleGoalDisp(this.__blinkGoalDisp);
- }
- }
- else
- {
- this.stopBlinkLights();
- }
- }
- function startBlinkLights(who)
- {
- this.__blinkTimer = 0;
- this.__frameElapsedTime = 0;
- this.__blinkSide = who != Game.PongGame.LEFT_PLAYER ? Game.PongGame.LEFT_PLAYER : Game.PongGame.RIGHT_PLAYER;
- this.__blinkGoalDisp = who;
- }
- function onBallInPlay()
- {
- this.stopBlinkLights();
- }
- function stopBlinkLights()
- {
- this.toggleLight(this.__blinkSide,false);
- this.__blinkSide = null;
- this.toggleGoalDisp(this.__blinkGoalDisp,true);
- this.__blinkGoalDisp = null;
- this.reactToGoal();
- }
- function toggleGoalDisp(side, force)
- {
- var _loc2_ = undefined;
- if(side == Game.PongGame.LEFT_PLAYER)
- {
- _loc2_ = this.__scoreboard.goalsLeft_txt;
- }
- else
- {
- _loc2_ = this.__scoreboard.goalsRight_txt;
- }
- if(force == undefined)
- {
- _loc2_._visible = !_loc2_._visible ? true : false;
- }
- else
- {
- _loc2_._visible = force;
- }
- }
- function toggleLight(side, force)
- {
- var _loc2_ = undefined;
- if(side == Game.PongGame.LEFT_PLAYER)
- {
- _loc2_ = this.timeline.arena_mc.activeLeft_mc;
- }
- else
- {
- _loc2_ = this.timeline.arena_mc.activeRight_mc;
- }
- if(force == undefined)
- {
- _loc2_.gotoAndStop(_loc2_._currentframe != 1 ? 1 : 3);
- }
- else
- {
- _loc2_.gotoAndStop(!force ? 1 : 3);
- }
- }
- function init()
- {
- Game.PongGame.LEVELS = Game.PongLevels.getLevels();
- Game.PongGame.__inited = true;
- }
- }
-