home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Puzzle / HexiomConnect.swf / scripts / Game.as < prev    next >
Encoding:
Text File  |  2008-08-29  |  10.9 KB  |  376 lines

  1. package
  2. {
  3.    import flash.display.Loader;
  4.    import flash.display.LoaderInfo;
  5.    import flash.display.Sprite;
  6.    import flash.display.StageQuality;
  7.    import flash.events.Event;
  8.    import flash.events.KeyboardEvent;
  9.    import flash.net.SharedObject;
  10.    import flash.net.URLRequest;
  11.    
  12.    public class Game extends Sprite
  13.    {
  14.       
  15.       private static var soundBar:SoundBar;
  16.       
  17.       public static var kongregate:*;
  18.       
  19.       public static var background:Background;
  20.       
  21.       public static const MIDX:Number = 200;
  22.       
  23.       public static var colorBlind:Boolean = false;
  24.       
  25.       public static var cookie:SharedObject;
  26.       
  27.       public static var cheatHasBeenFound:Boolean = false;
  28.       
  29.       public static var quality:int;
  30.        
  31.       
  32.       private var level:Level;
  33.       
  34.       private var black:Sprite;
  35.       
  36.       private var titleScreen:TitleScreen;
  37.       
  38.       public function Game()
  39.       {
  40.          super();
  41.          addEventListener(Event.ADDED_TO_STAGE,addedToStage,false,0,true);
  42.       }
  43.       
  44.       public static function completeLevel(param1:int, param2:int, param3:int) : void
  45.       {
  46.          if(param1 > 0)
  47.          {
  48.             setLevelComplete(param1,true);
  49.             submitLevelsCompleted();
  50.          }
  51.          else if(param1 == Level.BASIC_RANDOM)
  52.          {
  53.             if(kongregate)
  54.             {
  55.                trace("Size",param2,"in",param3,"seconds.");
  56.                kongregate.stats.submit("Random Size " + param2,param3);
  57.             }
  58.          }
  59.       }
  60.       
  61.       public static function nextQuality() : void
  62.       {
  63.          setQuality((quality + 1) % 6);
  64.       }
  65.       
  66.       public static function setLevelComplete(param1:int, param2:Boolean) : void
  67.       {
  68.          if(!param2)
  69.          {
  70.             cookie.data.levelsComplete[param1] = 0;
  71.             return;
  72.          }
  73.          RandomNumber.initialize(cookie.data.levelsComplete[0]);
  74.          var _loc3_:int = 0;
  75.          while(_loc3_ < param1)
  76.          {
  77.             RandomNumber.random();
  78.             _loc3_++;
  79.          }
  80.          cookie.data.levelsComplete[param1] = (int(RandomNumber.random() * 1111) % 13 + 3) * param1;
  81.          cookie.flush();
  82.       }
  83.       
  84.       public static function getLevelComplete(param1:int) : Boolean
  85.       {
  86.          RandomNumber.initialize(cookie.data.levelsComplete[0]);
  87.          var _loc2_:int = 0;
  88.          while(_loc2_ < param1)
  89.          {
  90.             RandomNumber.random();
  91.             _loc2_++;
  92.          }
  93.          return cookie.data.levelsComplete[param1] / param1 == int(RandomNumber.random() * 1111) % 13 + 3;
  94.       }
  95.       
  96.       public static function submitLevelsCompleted() : void
  97.       {
  98.          var _loc1_:int = 0;
  99.          var _loc2_:int = 0;
  100.          if(kongregate)
  101.          {
  102.             _loc1_ = 0;
  103.             _loc2_ = 1;
  104.             while(_loc2_ <= 40)
  105.             {
  106.                if(getLevelComplete(_loc2_))
  107.                {
  108.                   _loc1_++;
  109.                }
  110.                _loc2_++;
  111.             }
  112.             trace("Completed:",_loc1_);
  113.             kongregate.stats.submit("Levels Completed",_loc1_);
  114.          }
  115.       }
  116.       
  117.       public static function cheatFound() : void
  118.       {
  119.          if(cheatHasBeenFound)
  120.          {
  121.             return;
  122.          }
  123.          trace("Cheat Found");
  124.          cheatHasBeenFound = true;
  125.       }
  126.       
  127.       public static function setColorBlind(param1:Boolean) : void
  128.       {
  129.          colorBlind = param1;
  130.          cookie.data.colorBlind = param1;
  131.       }
  132.       
  133.       public static function checkClocking() : Boolean
  134.       {
  135.          if(cookie.data.clocked)
  136.          {
  137.             return false;
  138.          }
  139.          var _loc1_:int = 1;
  140.          while(_loc1_ <= 40)
  141.          {
  142.             if(!getLevelComplete(_loc1_))
  143.             {
  144.                return false;
  145.             }
  146.             _loc1_++;
  147.          }
  148.          cookie.data.clocked = true;
  149.          return true;
  150.       }
  151.       
  152.       private static function setQuality(param1:int) : void
  153.       {
  154.          quality = param1;
  155.          background.playing = param1 >= 3;
  156.          if(param1 % 3 == 0)
  157.          {
  158.             background.stage.quality = StageQuality.LOW;
  159.          }
  160.          if(param1 % 3 == 1)
  161.          {
  162.             background.stage.quality = StageQuality.MEDIUM;
  163.          }
  164.          if(param1 % 3 == 2)
  165.          {
  166.             background.stage.quality = StageQuality.HIGH;
  167.          }
  168.          cookie.data.quality = param1;
  169.       }
  170.       
  171.       private function removeLevel() : void
  172.       {
  173.          if(!level)
  174.          {
  175.             return;
  176.          }
  177.          if(level.parent == this)
  178.          {
  179.             removeChild(level);
  180.          }
  181.          level.removeEventListener(HexEvent.MENU,showMenu);
  182.          level.removeEventListener(HexEvent.NEXT_LEVEL,showMenu);
  183.          level.removeEventListener(HexEvent.NEXT_LEVEL,nextRandom);
  184.          level.removeEventListener(HexEvent.NEXT_LEVEL,play);
  185.          level = null;
  186.       }
  187.       
  188.       private function keyUp(param1:KeyboardEvent) : void
  189.       {
  190.          if(param1.keyCode == 81)
  191.          {
  192.             nextQuality();
  193.          }
  194.       }
  195.       
  196.       public function addedToStage(param1:Event) : void
  197.       {
  198.          var _loc2_:Object = null;
  199.          var _loc3_:String = null;
  200.          var _loc4_:URLRequest = null;
  201.          var _loc5_:Loader = null;
  202.          removeEventListener(Event.ADDED_TO_STAGE,addedToStage);
  203.          if(Factory.ON_KONG)
  204.          {
  205.             _loc2_ = LoaderInfo(root.loaderInfo).parameters;
  206.             _loc3_ = String(String(_loc2_.api_path) || "http://www.kongregate.com/flash/API_AS3_Local.swf");
  207.             _loc4_ = new URLRequest(_loc3_);
  208.             (_loc5_ = new Loader()).contentLoaderInfo.addEventListener(Event.COMPLETE,loadComplete);
  209.             _loc5_.load(_loc4_);
  210.             this.addChild(_loc5_);
  211.          }
  212.          stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
  213.          Input.init(stage);
  214.          if(!background)
  215.          {
  216.             background = new Background();
  217.          }
  218.          addChild(background);
  219.          cookie = SharedObject.getLocal("Hexiom");
  220.          if(!cookie.data.levelsComplete)
  221.          {
  222.             cookie.data.levelsComplete = new Array(41);
  223.             cookie.data.levelsComplete[0] = int(Math.random() * 900) + 3;
  224.          }
  225.          if(cookie.data.quality == undefined)
  226.          {
  227.             setQuality(5);
  228.          }
  229.          else
  230.          {
  231.             setQuality(cookie.data.quality);
  232.          }
  233.          colorBlind = cookie.data.colorBlind;
  234.          titleScreen = new TitleScreen();
  235.          titleScreen.addEventListener(HexEvent.EDIT,edit,false,0,true);
  236.          titleScreen.addEventListener(HexEvent.PLAY,play,false,0,true);
  237.          titleScreen.addEventListener(HexEvent.LOAD,load,false,0,true);
  238.          titleScreen.addEventListener(HexEvent.RANDOM,random,false,0,true);
  239.          if(!soundBar)
  240.          {
  241.             soundBar = new SoundBar();
  242.          }
  243.          addChild(soundBar);
  244.          showMenu();
  245.          titleScreen.stopTitle();
  246.          black = new Sprite();
  247.          black.graphics.beginFill(0);
  248.          black.graphics.drawRect(0,0,400,400);
  249.          black.graphics.endFill();
  250.          addChild(black);
  251.          addEventListener(Event.ENTER_FRAME,fadeBlack,false,0,true);
  252.          MochiBot.track(this,"2db7052f");
  253.       }
  254.       
  255.       private function fadeBlack(param1:Event) : void
  256.       {
  257.          var _loc2_:Number = 1 / 30;
  258.          black.alpha -= _loc2_;
  259.          if(black.alpha < _loc2_)
  260.          {
  261.             removeEventListener(Event.ENTER_FRAME,fadeBlack);
  262.             removeChild(black);
  263.             black = null;
  264.             titleScreen.flashTitle();
  265.             SoundBar.playMusic();
  266.          }
  267.       }
  268.       
  269.       private function load(param1:HexEvent) : void
  270.       {
  271.          if(!param1.board)
  272.          {
  273.             return;
  274.          }
  275.          if(titleScreen.parent == this)
  276.          {
  277.             removeChild(titleScreen);
  278.          }
  279.          level = new Level(param1.board,Level.CUSTOM);
  280.          level.addEventListener(HexEvent.MENU,showMenu,false,0,true);
  281.          level.addEventListener(HexEvent.NEXT_LEVEL,showMenu,false,0,true);
  282.          addChild(level);
  283.          Game.background.setColor(Background.ORANGE);
  284.       }
  285.       
  286.       private function playLevel(param1:int) : void
  287.       {
  288.          level = new Level(Board.loadFromText(Levels.getBoardString(param1)),param1);
  289.          level.addEventListener(HexEvent.MENU,showMenu,false,0,true);
  290.          level.addEventListener(HexEvent.NEXT_LEVEL,play,false,0,true);
  291.          addChild(level);
  292.          Game.background.setDifficulty(param1 / 40);
  293.       }
  294.       
  295.       private function edit(param1:HexEvent = null) : void
  296.       {
  297.          if(titleScreen.parent == this)
  298.          {
  299.             removeChild(titleScreen);
  300.          }
  301.          level = new Editor();
  302.          level.addEventListener(HexEvent.MENU,showMenu,false,0,true);
  303.          addChild(level);
  304.       }
  305.       
  306.       private function random(param1:HexEvent) : void
  307.       {
  308.          if(!param1.board)
  309.          {
  310.             return;
  311.          }
  312.          if(titleScreen.parent == this)
  313.          {
  314.             removeChild(titleScreen);
  315.          }
  316.          level = new Level(param1.board,param1.level,true);
  317.          level.addEventListener(HexEvent.MENU,showMenu,false,0,true);
  318.          level.addEventListener(HexEvent.NEXT_LEVEL,nextRandom,false,0,true);
  319.          addChild(level);
  320.          Game.background.setDifficulty((param1.board.size - 3) / 3);
  321.       }
  322.       
  323.       private function play(param1:HexEvent) : void
  324.       {
  325.          removeLevel();
  326.          if(titleScreen.parent == this)
  327.          {
  328.             removeChild(titleScreen);
  329.          }
  330.          var _loc2_:int = param1.level;
  331.          if(_loc2_ == 0)
  332.          {
  333.             _loc2_ = 1;
  334.          }
  335.          if(_loc2_ > 40)
  336.          {
  337.             showMenu();
  338.          }
  339.          else
  340.          {
  341.             playLevel(_loc2_);
  342.          }
  343.       }
  344.       
  345.       private function nextRandom(param1:HexEvent) : void
  346.       {
  347.          if(!param1.board)
  348.          {
  349.             return;
  350.          }
  351.          removeLevel();
  352.          param1.board.redoRandomFill();
  353.          level = new Level(param1.board,param1.level,true);
  354.          level.addEventListener(HexEvent.MENU,showMenu,false,0,true);
  355.          level.addEventListener(HexEvent.NEXT_LEVEL,nextRandom,false,0,true);
  356.          addChild(level);
  357.       }
  358.       
  359.       private function showMenu(param1:HexEvent = null) : void
  360.       {
  361.          removeLevel();
  362.          titleScreen.update();
  363.          addChild(titleScreen);
  364.          titleScreen.flashTitle();
  365.       }
  366.       
  367.       private function loadComplete(param1:Event) : void
  368.       {
  369.          kongregate = param1.target.content;
  370.          kongregate.services.connect();
  371.          trace("Connected - Submitting levels completed");
  372.          submitLevelsCompleted();
  373.       }
  374.    }
  375. }
  376.