home *** CD-ROM | disk | FTP | other *** search
- package game
- {
- import flash.events.Event;
- import flash.events.EventDispatcher;
- import flash.events.TimerEvent;
- import flash.utils.Timer;
- import gamegraphics.TimeBar;
-
- public class Game extends EventDispatcher
- {
-
- public static var powerup:int = 0;
-
- public static var timer:Timer;
-
- private static var powerupMaxRounds:int = 250;
-
-
- public var obstacle:Obstacle;
-
- private var destroyDoubleBoards:Boolean = false;
-
- public var overWithoutHit:int = 0;
-
- public var scores:int = 0;
-
- private var powerupRounds:int;
-
- private var bonus:Bonus;
-
- private var destroySlowBoards:Boolean = false;
-
- public var bouncers:Array;
-
- public var lastScores:int = 0;
-
- private var sinceLastBouncer:int = 0;
-
- public var jumper:Jumper;
-
- public var bounces:int = 0;
-
- public var lastGameScores:int = 0;
-
- public function Game()
- {
- super();
- timer = new Timer(20);
- timer.addEventListener("timer",onTimer);
- }
-
- public function wentOver() : void
- {
- ++overWithoutHit;
- dispatchEvent(new Event("wentOver"));
- }
-
- private function addBouncer(x:int) : void
- {
- var newBouncer:Bouncer = new Bouncer(x);
- bouncers.push(newBouncer);
- var event:Event = new ObjectCreatedEvent(newBouncer,Bouncer.imageClass);
- dispatchEvent(event);
- }
-
- public function moveDown() : void
- {
- jumper.vy = 4;
- }
-
- public function stopMoveLeft() : void
- {
- if(jumper.vx == -4)
- {
- jumper.vx = 0;
- }
- }
-
- private function setPowerup() : void
- {
- var i:int = 0;
- var rand:Number = Math.random();
- if(rand < 0.33)
- {
- powerup = 1;
- }
- else if(rand < 0.66)
- {
- while(i < bouncers.length)
- {
- bouncers[i].vx /= 2;
- i++;
- }
- powerup = 2;
- }
- else
- {
- powerup = 3;
- addBouncer(0);
- addBouncer(250);
- addBouncer(500);
- }
- timer.addEventListener("timer",handlePowerup);
- dispatchEvent(new Event("powerupped"));
- new TimeBar(jumper.x,jumper.y - 13,25,5,timer,powerupMaxRounds,this,jumper);
- }
-
- public function addScores(amt:int) : void
- {
- scores += amt;
- lastScores = amt;
- dispatchEvent(new Event("scoresChanged"));
- }
-
- private function onTimer(event:TimerEvent) : void
- {
- }
-
- private function handlePowerup(event:Event) : void
- {
- ++powerupRounds;
- if(powerupRounds > powerupMaxRounds)
- {
- removePowerup(null);
- }
- }
-
- private function removeDoubleBoards() : void
- {
- bouncers[3].destroy();
- bouncers[4].destroy();
- bouncers[5].destroy();
- bouncers.pop();
- bouncers.pop();
- bouncers.pop();
- destroyDoubleBoards = false;
- }
-
- public function resume() : void
- {
- timer.start();
- }
-
- private function removePowerup(event:Event) : void
- {
- if(powerup == 2)
- {
- destroySlowBoards = true;
- }
- if(powerup == 3)
- {
- destroyDoubleBoards = true;
- }
- powerup = 0;
- timer.removeEventListener("timer",handlePowerup);
- powerupRounds = 0;
- }
-
- public function stopMoveDown() : void
- {
- jumper.vy = 0;
- }
-
- public function moveRight() : void
- {
- jumper.vx = 4;
- }
-
- public function moveUp() : void
- {
- jumper.vy = -4;
- }
-
- public function addObstacle(x:int, y:int) : void
- {
- obstacle = new Obstacle(x,y);
- dispatchEvent(new ObjectCreatedEvent(obstacle,Obstacle.imageClass));
- }
-
- private function removeSlowBoards() : void
- {
- for(var i:int = 0; i < bouncers.length; )
- {
- bouncers[i].vx *= 2;
- i++;
- }
- destroySlowBoards = false;
- }
-
- private function addJumper() : void
- {
- jumper = new Jumper(this,310,50,20,30);
- var event:Event = new ObjectCreatedEvent(jumper,Jumper.imageClass,40,true,10);
- dispatchEvent(event);
- }
-
- public function start() : void
- {
- powerup = 0;
- overWithoutHit = 0;
- scores = 0;
- bouncers = new Array();
- addObstacle(320,230);
- addJumper();
- addBouncer(50);
- addBouncer(200);
- addBouncer(400);
- timer.reset();
- }
-
- public function gameOver() : void
- {
- timer.stop();
- jumper.destroy();
- if(powerup != 0)
- {
- removePowerup(null);
- }
- if(destroyDoubleBoards)
- {
- removeDoubleBoards();
- }
- if(destroySlowBoards)
- {
- removeSlowBoards();
- }
- Bouncer.reset();
- Bonus.reset();
- bounces = 0;
- if(bonus != null)
- {
- removeBonus();
- }
- for(var i:int = 0; i < bouncers.length; i++)
- {
- bouncers[i].destroy();
- }
- bouncers.pop();
- bouncers.pop();
- bouncers.pop();
- lastGameScores = scores;
- start();
- dispatchEvent(new Event("gameover"));
- dispatchEvent(new Event("scoresChanged"));
- }
-
- public function stopMoveRight() : void
- {
- if(jumper.vx == 4)
- {
- jumper.vx = 0;
- }
- }
-
- public function stopMoveUp() : void
- {
- jumper.vy = 0;
- }
-
- public function checkHit() : Boolean
- {
- if(bonus != null && (jumper.isInside(bonus) || bonus.isInside(jumper)))
- {
- removeBonus(null);
- setPowerup();
- }
- if(jumper.distanceTo(obstacle) < obstacle.w / 2 && jumper.overGoingPhase != 3 && jumper.overGoingPhase != 4)
- {
- jumper.overGoingPhase = 3;
- if(overWithoutHit > 0)
- {
- --overWithoutHit;
- }
- dispatchEvent(new Event("obstacleTouched"));
- }
- for(var i:int = 0; i < bouncers.length; i++)
- {
- if(jumper.isInside(bouncers[i]) || Boolean(bouncers[i].isInside(jumper)))
- {
- jumper.y = bouncers[i].y - jumper.h - 1;
- jumper.jump();
- if(bouncers[i].w > Bouncer.minWidth)
- {
- bouncers[i].w -= 2;
- }
- if(powerup == 2)
- {
- bouncers[i].vx += bouncers[i].vx / Math.abs(bouncers[i].vx) * 0.2;
- }
- else
- {
- bouncers[i].vx += bouncers[i].vx / Math.abs(bouncers[i].vx) * 0.4;
- }
- bouncers[i].vx = -bouncers[i].vx;
- bouncers[i].vy = 3;
- bouncers[i].y += 1;
- bouncers[i].update();
- ++bounces;
- jumper.overGoingPhase = 0;
- if(bounces % 8 == 0)
- {
- addBonus();
- }
- addScores(1 + overWithoutHit);
- if(destroyDoubleBoards)
- {
- removeDoubleBoards();
- }
- if(destroySlowBoards)
- {
- removeSlowBoards();
- }
- return true;
- }
- }
- if(jumper.y > 430)
- {
- gameOver();
- }
- return false;
- }
-
- public function pause() : void
- {
- timer.stop();
- }
-
- public function moveLeft() : void
- {
- jumper.vx = -4;
- }
-
- private function removeBonus(event:Event = null) : void
- {
- bonus.removeEventListener("objectDestroyed",removeBonus);
- if(event == null)
- {
- bonus.destroy();
- }
- bonus = null;
- }
-
- private function addBonus() : void
- {
- bonus = new Bonus();
- bonus.addEventListener("objectDestroyed",removeBonus);
- var event:Event = new ObjectCreatedEvent(bonus,bonus.getImage(),40,true,0,30);
- dispatchEvent(event);
- }
- }
- }
-