home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / Jumper.swf / scripts / game / Game.as < prev    next >
Encoding:
Text File  |  2008-09-05  |  8.9 KB  |  352 lines

  1. package game
  2. {
  3.    import flash.events.Event;
  4.    import flash.events.EventDispatcher;
  5.    import flash.events.TimerEvent;
  6.    import flash.utils.Timer;
  7.    import gamegraphics.TimeBar;
  8.    
  9.    public class Game extends EventDispatcher
  10.    {
  11.       
  12.       public static var powerup:int = 0;
  13.       
  14.       public static var timer:Timer;
  15.       
  16.       private static var powerupMaxRounds:int = 250;
  17.        
  18.       
  19.       public var obstacle:Obstacle;
  20.       
  21.       private var destroyDoubleBoards:Boolean = false;
  22.       
  23.       public var overWithoutHit:int = 0;
  24.       
  25.       public var scores:int = 0;
  26.       
  27.       private var powerupRounds:int;
  28.       
  29.       private var bonus:Bonus;
  30.       
  31.       private var destroySlowBoards:Boolean = false;
  32.       
  33.       public var bouncers:Array;
  34.       
  35.       public var lastScores:int = 0;
  36.       
  37.       private var sinceLastBouncer:int = 0;
  38.       
  39.       public var jumper:Jumper;
  40.       
  41.       public var bounces:int = 0;
  42.       
  43.       public var lastGameScores:int = 0;
  44.       
  45.       public function Game()
  46.       {
  47.          super();
  48.          timer = new Timer(20);
  49.          timer.addEventListener("timer",onTimer);
  50.       }
  51.       
  52.       public function wentOver() : void
  53.       {
  54.          ++overWithoutHit;
  55.          dispatchEvent(new Event("wentOver"));
  56.       }
  57.       
  58.       private function addBouncer(x:int) : void
  59.       {
  60.          var newBouncer:Bouncer = new Bouncer(x);
  61.          bouncers.push(newBouncer);
  62.          var event:Event = new ObjectCreatedEvent(newBouncer,Bouncer.imageClass);
  63.          dispatchEvent(event);
  64.       }
  65.       
  66.       public function moveDown() : void
  67.       {
  68.          jumper.vy = 4;
  69.       }
  70.       
  71.       public function stopMoveLeft() : void
  72.       {
  73.          if(jumper.vx == -4)
  74.          {
  75.             jumper.vx = 0;
  76.          }
  77.       }
  78.       
  79.       private function setPowerup() : void
  80.       {
  81.          var i:int = 0;
  82.          var rand:Number = Math.random();
  83.          if(rand < 0.33)
  84.          {
  85.             powerup = 1;
  86.          }
  87.          else if(rand < 0.66)
  88.          {
  89.             while(i < bouncers.length)
  90.             {
  91.                bouncers[i].vx /= 2;
  92.                i++;
  93.             }
  94.             powerup = 2;
  95.          }
  96.          else
  97.          {
  98.             powerup = 3;
  99.             addBouncer(0);
  100.             addBouncer(250);
  101.             addBouncer(500);
  102.          }
  103.          timer.addEventListener("timer",handlePowerup);
  104.          dispatchEvent(new Event("powerupped"));
  105.          new TimeBar(jumper.x,jumper.y - 13,25,5,timer,powerupMaxRounds,this,jumper);
  106.       }
  107.       
  108.       public function addScores(amt:int) : void
  109.       {
  110.          scores += amt;
  111.          lastScores = amt;
  112.          dispatchEvent(new Event("scoresChanged"));
  113.       }
  114.       
  115.       private function onTimer(event:TimerEvent) : void
  116.       {
  117.       }
  118.       
  119.       private function handlePowerup(event:Event) : void
  120.       {
  121.          ++powerupRounds;
  122.          if(powerupRounds > powerupMaxRounds)
  123.          {
  124.             removePowerup(null);
  125.          }
  126.       }
  127.       
  128.       private function removeDoubleBoards() : void
  129.       {
  130.          bouncers[3].destroy();
  131.          bouncers[4].destroy();
  132.          bouncers[5].destroy();
  133.          bouncers.pop();
  134.          bouncers.pop();
  135.          bouncers.pop();
  136.          destroyDoubleBoards = false;
  137.       }
  138.       
  139.       public function resume() : void
  140.       {
  141.          timer.start();
  142.       }
  143.       
  144.       private function removePowerup(event:Event) : void
  145.       {
  146.          if(powerup == 2)
  147.          {
  148.             destroySlowBoards = true;
  149.          }
  150.          if(powerup == 3)
  151.          {
  152.             destroyDoubleBoards = true;
  153.          }
  154.          powerup = 0;
  155.          timer.removeEventListener("timer",handlePowerup);
  156.          powerupRounds = 0;
  157.       }
  158.       
  159.       public function stopMoveDown() : void
  160.       {
  161.          jumper.vy = 0;
  162.       }
  163.       
  164.       public function moveRight() : void
  165.       {
  166.          jumper.vx = 4;
  167.       }
  168.       
  169.       public function moveUp() : void
  170.       {
  171.          jumper.vy = -4;
  172.       }
  173.       
  174.       public function addObstacle(x:int, y:int) : void
  175.       {
  176.          obstacle = new Obstacle(x,y);
  177.          dispatchEvent(new ObjectCreatedEvent(obstacle,Obstacle.imageClass));
  178.       }
  179.       
  180.       private function removeSlowBoards() : void
  181.       {
  182.          for(var i:int = 0; i < bouncers.length; )
  183.          {
  184.             bouncers[i].vx *= 2;
  185.             i++;
  186.          }
  187.          destroySlowBoards = false;
  188.       }
  189.       
  190.       private function addJumper() : void
  191.       {
  192.          jumper = new Jumper(this,310,50,20,30);
  193.          var event:Event = new ObjectCreatedEvent(jumper,Jumper.imageClass,40,true,10);
  194.          dispatchEvent(event);
  195.       }
  196.       
  197.       public function start() : void
  198.       {
  199.          powerup = 0;
  200.          overWithoutHit = 0;
  201.          scores = 0;
  202.          bouncers = new Array();
  203.          addObstacle(320,230);
  204.          addJumper();
  205.          addBouncer(50);
  206.          addBouncer(200);
  207.          addBouncer(400);
  208.          timer.reset();
  209.       }
  210.       
  211.       public function gameOver() : void
  212.       {
  213.          timer.stop();
  214.          jumper.destroy();
  215.          if(powerup != 0)
  216.          {
  217.             removePowerup(null);
  218.          }
  219.          if(destroyDoubleBoards)
  220.          {
  221.             removeDoubleBoards();
  222.          }
  223.          if(destroySlowBoards)
  224.          {
  225.             removeSlowBoards();
  226.          }
  227.          Bouncer.reset();
  228.          Bonus.reset();
  229.          bounces = 0;
  230.          if(bonus != null)
  231.          {
  232.             removeBonus();
  233.          }
  234.          for(var i:int = 0; i < bouncers.length; i++)
  235.          {
  236.             bouncers[i].destroy();
  237.          }
  238.          bouncers.pop();
  239.          bouncers.pop();
  240.          bouncers.pop();
  241.          lastGameScores = scores;
  242.          start();
  243.          dispatchEvent(new Event("gameover"));
  244.          dispatchEvent(new Event("scoresChanged"));
  245.       }
  246.       
  247.       public function stopMoveRight() : void
  248.       {
  249.          if(jumper.vx == 4)
  250.          {
  251.             jumper.vx = 0;
  252.          }
  253.       }
  254.       
  255.       public function stopMoveUp() : void
  256.       {
  257.          jumper.vy = 0;
  258.       }
  259.       
  260.       public function checkHit() : Boolean
  261.       {
  262.          if(bonus != null && (jumper.isInside(bonus) || bonus.isInside(jumper)))
  263.          {
  264.             removeBonus(null);
  265.             setPowerup();
  266.          }
  267.          if(jumper.distanceTo(obstacle) < obstacle.w / 2 && jumper.overGoingPhase != 3 && jumper.overGoingPhase != 4)
  268.          {
  269.             jumper.overGoingPhase = 3;
  270.             if(overWithoutHit > 0)
  271.             {
  272.                --overWithoutHit;
  273.             }
  274.             dispatchEvent(new Event("obstacleTouched"));
  275.          }
  276.          for(var i:int = 0; i < bouncers.length; i++)
  277.          {
  278.             if(jumper.isInside(bouncers[i]) || Boolean(bouncers[i].isInside(jumper)))
  279.             {
  280.                jumper.y = bouncers[i].y - jumper.h - 1;
  281.                jumper.jump();
  282.                if(bouncers[i].w > Bouncer.minWidth)
  283.                {
  284.                   bouncers[i].w -= 2;
  285.                }
  286.                if(powerup == 2)
  287.                {
  288.                   bouncers[i].vx += bouncers[i].vx / Math.abs(bouncers[i].vx) * 0.2;
  289.                }
  290.                else
  291.                {
  292.                   bouncers[i].vx += bouncers[i].vx / Math.abs(bouncers[i].vx) * 0.4;
  293.                }
  294.                bouncers[i].vx = -bouncers[i].vx;
  295.                bouncers[i].vy = 3;
  296.                bouncers[i].y += 1;
  297.                bouncers[i].update();
  298.                ++bounces;
  299.                jumper.overGoingPhase = 0;
  300.                if(bounces % 8 == 0)
  301.                {
  302.                   addBonus();
  303.                }
  304.                addScores(1 + overWithoutHit);
  305.                if(destroyDoubleBoards)
  306.                {
  307.                   removeDoubleBoards();
  308.                }
  309.                if(destroySlowBoards)
  310.                {
  311.                   removeSlowBoards();
  312.                }
  313.                return true;
  314.             }
  315.          }
  316.          if(jumper.y > 430)
  317.          {
  318.             gameOver();
  319.          }
  320.          return false;
  321.       }
  322.       
  323.       public function pause() : void
  324.       {
  325.          timer.stop();
  326.       }
  327.       
  328.       public function moveLeft() : void
  329.       {
  330.          jumper.vx = -4;
  331.       }
  332.       
  333.       private function removeBonus(event:Event = null) : void
  334.       {
  335.          bonus.removeEventListener("objectDestroyed",removeBonus);
  336.          if(event == null)
  337.          {
  338.             bonus.destroy();
  339.          }
  340.          bonus = null;
  341.       }
  342.       
  343.       private function addBonus() : void
  344.       {
  345.          bonus = new Bonus();
  346.          bonus.addEventListener("objectDestroyed",removeBonus);
  347.          var event:Event = new ObjectCreatedEvent(bonus,bonus.getImage(),40,true,0,30);
  348.          dispatchEvent(event);
  349.       }
  350.    }
  351. }
  352.