home *** CD-ROM | disk | FTP | other *** search
- package
- {
- import flash.display.DisplayObject;
- import flash.display.DisplayObjectContainer;
- import flash.display.SimpleButton;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.events.TimerEvent;
- import flash.geom.Point;
- import flash.system.System;
- import flash.text.TextField;
- import flash.utils.Timer;
-
- public class Level extends Sprite
- {
-
- protected static var InterfaceGfx:Class = Level_InterfaceGfx;
-
- protected static var SaveDialog:Class = Level_SaveDialog;
-
- public static const CUSTOM:int = -1;
-
- protected static var Clocking:Class = Level_Clocking;
-
- protected static var Gap:Class = Level_Gap;
-
- public static const ADVANCED_RANDOM:int = -3;
-
- public static const BASIC_RANDOM:int = -2;
-
- protected static var VictoryScreen:Class = Level_VictoryScreen;
-
-
- protected var tileGhost:Sprite;
-
- public var tiles:Array;
-
- protected var saveButton:SimpleButton;
-
- private var saveDialog:Sprite;
-
- protected var timeLabel:TextField;
-
- private var victoryScreen:Sprite;
-
- private var _time:Array;
-
- public var slots:Array;
-
- private var timer:Timer;
-
- protected var currentTile:Tile;
-
- protected var levelLabel:TextField;
-
- private var victoryShowing:Boolean = false;
-
- protected var gap:Sprite;
-
- private var saveOutput:TextField;
-
- protected var resetButton:SimpleButton;
-
- private var victoryTimer:Timer;
-
- public var board:Board;
-
- private var levelNumber:int;
-
- protected var backButton:SimpleButton;
-
- public function Level(param1:Board = null, param2:int = -1, param3:Boolean = false, param4:Boolean = true)
- {
- _time = new Array(3);
- super();
- time = 0;
- tileGhost = Tile.getTileGhost();
- gap = new Gap();
- if(param1)
- {
- addBoard(param1);
- }
- else
- {
- addBoard(new Board(3));
- }
- board.addEventListener(HexEvent.GROWN,startTimer,false,0,true);
- board.grow();
- timer = new Timer(1000,0);
- victoryTimer = new Timer(500,1);
- victoryTimer.addEventListener(TimerEvent.TIMER,showVictoryScreenTime,false,0,true);
- this.levelNumber = param2;
- addInterface(param3,param4);
- addEventListener(Event.ADDED_TO_STAGE,addedToStage,false,0,true);
- addEventListener(Event.REMOVED_FROM_STAGE,removedFromStage,false,0,true);
- }
-
- protected function resetClick(param1:MouseEvent) : void
- {
- var _loc2_:Tile = null;
- victoryShowing = false;
- hideVictoryScreen();
- SoundBar.playSound(SoundBar.RESET_LEVEL_SOUND,0);
- reset();
- for each(_loc2_ in tiles)
- {
- _loc2_.flash();
- }
- }
-
- protected function startTimer(param1:HexEvent) : void
- {
- timer.addEventListener(TimerEvent.TIMER,secondTick,false,0,true);
- }
-
- protected function victoryOver(param1:MouseEvent) : void
- {
- if(victoryShowing)
- {
- showVictoryScreen();
- }
- }
-
- protected function continueToMenu(param1:HexEvent) : void
- {
- var _loc2_:int = levelNumber;
- if(levelNumber >= 1)
- {
- _loc2_++;
- }
- dispatchEvent(new HexEvent(HexEvent.NEXT_LEVEL,false,false,board,_loc2_));
- }
-
- protected function saveClick(param1:MouseEvent) : void
- {
- SoundBar.playSound(SoundBar.BUTTON_CLICK_SOUND,mouseX / Game.MIDX - 1);
- stage.focus = saveOutput;
- saveOutput.text = board.saveToText();
- saveOutput.scrollH = 0;
- saveOutput.setSelection(0,saveOutput.text.length);
- addChild(saveDialog);
- }
-
- protected function victory() : void
- {
- var _loc1_:Tile = null;
- SoundBar.playSound(SoundBar.VICTORY_SOUND,0);
- for each(_loc1_ in tiles)
- {
- _loc1_.flash();
- }
- victoryTimer.reset();
- victoryTimer.start();
- Game.completeLevel(levelNumber,board.size,time);
- }
-
- protected function overSlot() : Slot
- {
- var _loc2_:DisplayObject = null;
- var _loc3_:DisplayObjectContainer = null;
- var _loc4_:Slot = null;
- var _loc1_:Array = stage.getObjectsUnderPoint(new Point(stage.mouseX,stage.mouseY));
- if(_loc1_.length)
- {
- for each(_loc2_ in _loc1_)
- {
- _loc3_ = _loc2_.parent;
- while(_loc3_ != stage)
- {
- if(_loc4_ = _loc3_ as Slot)
- {
- return _loc4_;
- }
- _loc3_ = _loc3_.parent;
- }
- }
- }
- return null;
- }
-
- protected function showVictoryScreenTime(param1:TimerEvent) : void
- {
- var _loc2_:Sprite = null;
- var _loc3_:DisplayObject = null;
- if(Game.checkClocking())
- {
- _loc2_ = new Clocking();
- _loc3_ = victoryScreen.getChildAt(victoryScreen.numChildren - 1);
- _loc3_.y -= 30;
- _loc2_.addChild(_loc3_);
- _loc3_ = victoryScreen.getChildAt(victoryScreen.numChildren - 1);
- _loc3_.y -= 30;
- _loc2_.addChild(_loc3_);
- victoryScreen = _loc2_;
- }
- victoryShowing = true;
- victoryScreen.alpha = 0;
- showVictoryScreen();
- }
-
- protected function saveCopyClick(param1:MouseEvent) : void
- {
- SoundBar.playSound(SoundBar.BUTTON_CLICK_SOUND,mouseX / Game.MIDX - 1);
- System.setClipboard(saveOutput.text);
- }
-
- protected function addInterface(param1:Boolean, param2:Boolean) : void
- {
- var _loc4_:TextButton = null;
- var _loc3_:DisplayObjectContainer = new InterfaceGfx();
- resetButton = _loc3_.getChildByName("resetButton") as SimpleButton;
- saveButton = _loc3_.getChildByName("saveButton") as SimpleButton;
- backButton = _loc3_.getChildByName("backButton") as SimpleButton;
- levelLabel = _loc3_.getChildByName("levelLabel") as TextField;
- levelLabel.mouseEnabled = false;
- timeLabel = _loc3_.getChildByName("timeLabel") as TextField;
- timeLabel.mouseEnabled = false;
- if(param2)
- {
- addChild(levelLabel);
- if(levelNumber == CUSTOM)
- {
- levelLabel.text = "Custom";
- }
- else if(levelNumber == BASIC_RANDOM || levelNumber == ADVANCED_RANDOM)
- {
- levelLabel.text = "Random";
- }
- else
- {
- levelLabel.text = "Level " + levelNumber;
- }
- addChild(timeLabel);
- timer.start();
- updateTime();
- }
- saveDialog = new SaveDialog();
- (_loc4_ = new TextButton(TextButton.LONG,"Copy")).x = 140;
- _loc4_.y = 240;
- _loc4_.addEventListener(MouseEvent.CLICK,saveCopyClick,false,0,true);
- saveDialog.addChild(_loc4_);
- (_loc4_ = new TextButton(TextButton.LONG,"Ok")).x = 260;
- _loc4_.y = 240;
- _loc4_.addEventListener(MouseEvent.CLICK,saveOkClick,false,0,true);
- saveDialog.addChild(_loc4_);
- saveOutput = saveDialog.getChildByName("output") as TextField;
- if(param1)
- {
- addChild(saveButton);
- saveButton.addEventListener(MouseEvent.CLICK,saveClick,false,0,true);
- }
- addChild(resetButton);
- resetButton.addEventListener(MouseEvent.CLICK,resetClick,false,0,true);
- addChild(backButton);
- backButton.addEventListener(MouseEvent.CLICK,menuClick,false,0,true);
- victoryScreen = new VictoryScreen();
- victoryScreen.addEventListener(MouseEvent.ROLL_OVER,victoryOver,false,0,true);
- victoryScreen.addEventListener(MouseEvent.ROLL_OUT,victoryOut,false,0,true);
- (_loc4_ = new TextButton(TextButton.LONG,"Continue")).x = 200;
- _loc4_.y = 200;
- _loc4_.addEventListener(MouseEvent.CLICK,continueClick,false,0,true);
- victoryScreen.addChild(_loc4_);
- (_loc4_ = new TextButton(TextButton.LONG,"Replay")).x = 200;
- _loc4_.y = 230;
- _loc4_.addEventListener(MouseEvent.CLICK,resetClick,false,0,true);
- victoryScreen.addChild(_loc4_);
- (_loc4_ = new TextButton(TextButton.LONG,"Menu")).x = 200;
- _loc4_.y = 260;
- _loc4_.addEventListener(MouseEvent.CLICK,menuClick,false,0,true);
- victoryScreen.addChild(_loc4_);
- (_loc4_ = new TextButton(TextButton.LONG,"More Games")).x = 200;
- _loc4_.y = 290;
- _loc4_.addEventListener(MouseEvent.CLICK,moreGamesClick,false,0,true);
- victoryScreen.addChild(_loc4_);
- }
-
- private function set time(param1:int) : void
- {
- _time[0] = param1 * 3 - 1;
- _time[1] = param1 * 7 - 2;
- _time[2] = param1 * -5 - 1;
- }
-
- protected function disableMouse() : void
- {
- var _loc1_:Tile = null;
- for each(_loc1_ in tiles)
- {
- _loc1_.removeEventListener(MouseEvent.MOUSE_DOWN,tileDown);
- }
- }
-
- protected function tileMove(param1:MouseEvent) : void
- {
- if(!currentTile)
- {
- return;
- }
- currentTile.x = board.mouseX;
- currentTile.y = board.mouseY;
- }
-
- protected function continueClick(param1:MouseEvent) : void
- {
- victoryShowing = false;
- hideVictoryScreen();
- SoundBar.playSound(SoundBar.BUTTON_CLICK_SOUND,mouseX / Game.MIDX - 1);
- board.addEventListener(HexEvent.SHRUNK,continueToMenu,false,0,true);
- board.shrink();
- }
-
- protected function overTile() : Tile
- {
- var _loc2_:DisplayObject = null;
- var _loc3_:DisplayObjectContainer = null;
- var _loc4_:Tile = null;
- var _loc1_:Array = stage.getObjectsUnderPoint(new Point(stage.mouseX,stage.mouseY));
- if(_loc1_.length)
- {
- for each(_loc2_ in _loc1_)
- {
- _loc3_ = _loc2_.parent;
- while(_loc3_ != stage)
- {
- if((Boolean(_loc4_ = _loc3_ as Tile)) && _loc4_ != currentTile)
- {
- return _loc4_;
- }
- _loc3_ = _loc3_.parent;
- }
- }
- }
- return null;
- }
-
- protected function addedToStage(param1:Event) : void
- {
- addEventListener(Event.ENTER_FRAME,update,false,0,true);
- enableMouse();
- }
-
- private function showVictoryScreen() : void
- {
- addChild(victoryScreen);
- removeEventListener(Event.ENTER_FRAME,fadeOutVictoryStep);
- addEventListener(Event.ENTER_FRAME,fadeInVictoryStep,false,0,true);
- }
-
- protected function hideVictoryScreen() : void
- {
- removeEventListener(Event.ENTER_FRAME,fadeInVictoryStep);
- addEventListener(Event.ENTER_FRAME,fadeOutVictoryStep,false,0,true);
- }
-
- protected function secondTick(param1:TimerEvent) : void
- {
- ++time;
- updateTime();
- }
-
- protected function checkWin() : void
- {
- var _loc2_:Tile = null;
- var _loc3_:Slot = null;
- var _loc1_:Boolean = true;
- for each(_loc2_ in tiles)
- {
- _loc3_ = _loc2_.slot;
- if(Boolean(_loc2_.n) && (!_loc3_.n || _loc2_.n != _loc3_.n.tile.s))
- {
- _loc1_ = false;
- }
- if(Boolean(_loc2_.ne) && (!_loc3_.ne || _loc2_.ne != _loc3_.ne.tile.sw))
- {
- _loc1_ = false;
- }
- if(Boolean(_loc2_.se) && (!_loc3_.se || _loc2_.se != _loc3_.se.tile.nw))
- {
- _loc1_ = false;
- }
- if(Boolean(_loc2_.s) && (!_loc3_.s || _loc2_.s != _loc3_.s.tile.n))
- {
- _loc1_ = false;
- }
- if(Boolean(_loc2_.sw) && (!_loc3_.sw || _loc2_.sw != _loc3_.sw.tile.ne))
- {
- _loc1_ = false;
- }
- if(Boolean(_loc2_.nw) && (!_loc3_.nw || _loc2_.nw != _loc3_.nw.tile.se))
- {
- _loc1_ = false;
- }
- if(!_loc1_)
- {
- break;
- }
- }
- if(_loc1_)
- {
- timer.stop();
- victory();
- }
- }
-
- protected function updateTime() : void
- {
- if(Game.cheatHasBeenFound)
- {
- timeLabel.text = "Cheat Detected";
- timeLabel.textColor = 11141120;
- return;
- }
- var _loc1_:int = time;
- var _loc2_:* = "";
- var _loc3_:* = "";
- var _loc4_:String = "";
- var _loc5_:* = _loc1_ >= 3600;
- var _loc6_:* = _loc1_ >= 60;
- if(_loc5_)
- {
- _loc2_ = int(_loc1_ / 3600) + ":";
- }
- _loc1_ %= 3600;
- if(_loc5_)
- {
- _loc3_ += int(_loc1_ / 60);
- while(_loc3_.length < 2)
- {
- _loc3_ = "0" + _loc3_;
- }
- _loc3_ += ":";
- }
- else
- {
- _loc3_ = int(_loc1_ / 60) + ":";
- }
- _loc1_ %= 60;
- _loc4_ += _loc1_;
- while(_loc4_.length < 2)
- {
- _loc4_ = "0" + _loc4_;
- }
- timeLabel.text = _loc2_ + _loc3_ + _loc4_;
- }
-
- protected function removeBoard() : void
- {
- var _loc1_:Tile = null;
- if(!board)
- {
- return;
- }
- for each(_loc1_ in tiles)
- {
- _loc1_.removeEventListener(MouseEvent.MOUSE_DOWN,tileDown);
- }
- slots = null;
- tiles = null;
- if(board.parent == this)
- {
- removeChild(board);
- }
- board = null;
- }
-
- private function get time() : int
- {
- var _loc1_:int = (_time[0] + 1) / 3;
- if((_time[1] + 2) / 7 != _loc1_)
- {
- Game.cheatFound();
- }
- if((_time[2] + 1) / -5 != _loc1_)
- {
- Game.cheatFound();
- }
- return _loc1_;
- }
-
- protected function reset() : void
- {
- var _loc1_:Tile = null;
- for each(_loc1_ in tiles)
- {
- if(_loc1_.confirmed)
- {
- _loc1_.unconfirm();
- }
- }
- board.startPositions();
- }
-
- protected function returnToMenu(param1:HexEvent) : void
- {
- dispatchEvent(new HexEvent(HexEvent.MENU));
- }
-
- protected function addBoard(param1:Board) : void
- {
- var _loc2_:Tile = null;
- board = param1;
- slots = board.slots;
- tiles = board.tiles;
- addChild(board);
- board.x = 200;
- board.y = 200;
- board.scaleX = board.scaleY = 1;
- board.setScale(Math.min(0.65,350 / board.width,350 / board.height));
- board.addChild(tileGhost);
- for each(_loc2_ in tiles)
- {
- _loc2_.checkNeighbours();
- }
- }
-
- protected function victoryOut(param1:MouseEvent) : void
- {
- if(victoryShowing)
- {
- hideVictoryScreen();
- }
- }
-
- protected function saveOkClick(param1:MouseEvent) : void
- {
- SoundBar.playSound(SoundBar.BUTTON_CLICK_SOUND,mouseX / Game.MIDX - 1);
- if(saveDialog.parent == this)
- {
- removeChild(saveDialog);
- }
- }
-
- protected function enableMouse() : void
- {
- var _loc1_:Tile = null;
- for each(_loc1_ in tiles)
- {
- _loc1_.addEventListener(MouseEvent.MOUSE_DOWN,tileDown,false,0,true);
- }
- }
-
- public function update(param1:Event) : void
- {
- if(!currentTile)
- {
- tileGhost.visible = false;
- return;
- }
- var _loc2_:Slot = overSlot();
- var _loc3_:Tile = null;
- if(_loc2_)
- {
- _loc3_ = _loc2_.tile;
- }
- if(Boolean(_loc3_) && !_loc3_.locked)
- {
- tileGhost.x = _loc3_.x;
- tileGhost.y = _loc3_.y;
- tileGhost.visible = true;
- }
- else
- {
- tileGhost.visible = false;
- }
- }
-
- protected function fadeOutVictoryStep(param1:Event) : void
- {
- victoryScreen.alpha = Math.max(0,victoryScreen.alpha - 0.1);
- if(victoryScreen.alpha == 0)
- {
- removeEventListener(Event.ENTER_FRAME,fadeOutVictoryStep);
- if(victoryScreen.parent == this && !victoryShowing)
- {
- removeChild(victoryScreen);
- }
- }
- }
-
- protected function menuClick(param1:MouseEvent) : void
- {
- victoryShowing = false;
- hideVictoryScreen();
- SoundBar.playSound(SoundBar.BUTTON_CLICK_SOUND,mouseX / Game.MIDX - 1);
- board.addEventListener(HexEvent.SHRUNK,returnToMenu,false,0,true);
- board.shrink();
- }
-
- protected function swapSlots(param1:Slot, param2:Slot) : void
- {
- param1.swapTiles(param2);
- param1.tile.flash();
- param2.tile.flash();
- if(param1.tile.confirmed)
- {
- param1.tile.unconfirm();
- }
- if(param2.tile.confirmed)
- {
- param2.tile.unconfirm();
- }
- }
-
- protected function removedFromStage(param1:Event) : void
- {
- removeEventListener(Event.ENTER_FRAME,update);
- disableMouse();
- }
-
- protected function tileDown(param1:MouseEvent) : void
- {
- var _loc2_:Tile = param1.currentTarget as Tile;
- if(!_loc2_ || _loc2_.locked)
- {
- return;
- }
- if(param1.shiftKey)
- {
- SoundBar.playSound(SoundBar.LOCK_SOUND,mouseX / Game.MIDX - 1);
- if(_loc2_.confirmed)
- {
- _loc2_.unconfirm();
- }
- else
- {
- _loc2_.confirm();
- }
- return;
- }
- SoundBar.playSound(SoundBar.PICKUP_SOUND,mouseX / Game.MIDX - 1);
- if(_loc2_.confirmed)
- {
- _loc2_.unconfirm();
- }
- currentTile = _loc2_;
- currentTile.x = board.mouseX;
- currentTile.y = board.mouseY;
- currentTile.alpha = 0.75;
- _loc2_.parent.setChildIndex(currentTile,_loc2_.parent.numChildren - 1);
- stage.addEventListener(MouseEvent.MOUSE_MOVE,tileMove,false,0,true);
- stage.addEventListener(MouseEvent.MOUSE_UP,tileUp,false,0,true);
- _loc2_.slot.addChild(gap);
- }
-
- protected function tileUp(param1:MouseEvent) : void
- {
- if(!currentTile)
- {
- return;
- }
- if(gap.parent)
- {
- gap.parent.removeChild(gap);
- }
- var _loc2_:Slot = overSlot();
- var _loc3_:Tile = null;
- if(_loc2_)
- {
- _loc3_ = _loc2_.tile;
- }
- if(_loc3_ == currentTile)
- {
- SoundBar.playSound(SoundBar.DROP_SOUND,mouseX / Game.MIDX - 1);
- currentTile.snapToSlot();
- }
- else if(Boolean(_loc3_) && !_loc3_.locked)
- {
- SoundBar.playSound(SoundBar.DROP_SOUND,mouseX / Game.MIDX - 1);
- swapSlots(currentTile.slot,_loc3_.slot);
- checkWin();
- }
- else
- {
- SoundBar.playSound(SoundBar.RETURN_SOUND,mouseX / Game.MIDX - 1);
- currentTile.snapToSlot();
- }
- currentTile.alpha = 1;
- stage.removeEventListener(MouseEvent.MOUSE_MOVE,tileMove);
- stage.removeEventListener(MouseEvent.MOUSE_UP,tileUp);
- currentTile = null;
- }
-
- protected function moreGamesClick(param1:MouseEvent) : void
- {
- SoundBar.playSound(SoundBar.BUTTON_CLICK_SOUND,mouseX / Game.MIDX - 1);
- Links.sponsorLink();
- }
-
- protected function fadeInVictoryStep(param1:Event) : void
- {
- victoryScreen.alpha = Math.min(1,victoryScreen.alpha + 0.1);
- if(victoryScreen.alpha == 1)
- {
- removeEventListener(Event.ENTER_FRAME,fadeInVictoryStep);
- }
- }
- }
- }
-