home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
404 Jogos
/
CLJG.iso
/
Puzzle
/
HexiomConnect.swf
/
scripts
/
Board.as
< prev
next >
Wrap
Text File
|
2008-08-29
|
17KB
|
576 lines
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Board extends Sprite
{
public static const MAX_SIZE:int = 7;
public static const MAX_LOCKED:Number = 0.5;
public static const MIN_CONNECTIONS:Number = 0.2;
public static const MIN_SIZE:int = 2;
public static const DEFAULT_LOCKED:Number = 0.2;
private static var BoardGfx:Class = Board_BoardGfx;
public static const MIN_LOCKED:Number = 0;
public static const MAX_CONNECTIONS:Number = 1;
public static const DEFAULT_COLORS:int = 4;
public static const DEFAULT_CONNECTIONS:Number = 0.35;
public var size:int;
public var tileLayer:Sprite;
public var tiles:Array;
private var randomConnections:Number;
private var randomColors:int;
public var scaleFraction:Number = 1;
public var background:Sprite;
public var slotLayer:Sprite;
private var randomLocked:Number;
public var slots:Array;
public var fullScale:Number = 1;
public function Board(param1:int)
{
var _loc2_:Slot = null;
var _loc3_:Tile = null;
var _loc6_:int = 0;
super();
this.size = param1;
slots = new Array();
tiles = new Array();
background = new BoardGfx();
addChild(background);
background.scaleX = background.scaleY = (param1 * 2 + 0.75 - Math.sqrt(param1)) * 3.05;
slotLayer = new Sprite();
addChild(slotLayer);
tileLayer = new Sprite();
addChild(tileLayer);
_loc2_ = new Slot(0,0);
slots.push(_loc2_);
var _loc4_:int = 1;
while(_loc4_ < param1)
{
_loc6_ = slots.length - 1;
while(_loc6_ >= 0)
{
expand(slots[_loc6_]);
_loc6_--;
}
_loc4_++;
}
var _loc5_:int = slots.length - 1;
while(_loc5_ >= 0)
{
_loc2_ = slots[_loc5_];
slotLayer.addChild(_loc2_);
_loc3_ = new Tile(_loc2_);
tileLayer.addChild(_loc3_);
tiles.push(_loc3_);
_loc3_.buttonMode = true;
_loc5_--;
}
}
public static function loadFromText(param1:String) : Board
{
var _loc4_:Tile = null;
var _loc5_:int = 0;
var _loc6_:int = 0;
var _loc7_:int = 0;
var _loc8_:Tile = null;
while(param1.charAt(0) == " ")
{
param1 = param1.substr(1);
}
param1 = Encrypt.decrypt(param1);
var _loc2_:int = int(param1.charAt(0));
if(_loc2_ < MIN_SIZE || _loc2_ > MAX_SIZE)
{
return null;
}
var _loc3_:Board = new Board(_loc2_);
param1 = param1.substr(1);
for each(_loc4_ in _loc3_.tiles)
{
if(param1.charAt(0) == "1")
{
_loc4_.lock();
}
param1 = param1.substr(1);
_loc5_ = param1.indexOf("_");
_loc6_ = int(param1.substr(0,_loc5_));
_loc4_.startSlot = _loc3_.slots[_loc6_];
param1 = param1.substr(_loc5_ + 1);
if((_loc7_ = int(param1.charAt(0))) >= Tile.COLORS.length)
{
return null;
}
_loc4_.setN(_loc7_);
if((_loc7_ = int(param1.charAt(1))) >= Tile.COLORS.length)
{
return null;
}
_loc4_.setNE(_loc7_);
if((_loc7_ = int(param1.charAt(2))) >= Tile.COLORS.length)
{
return null;
}
_loc4_.setSE(_loc7_);
if((_loc7_ = int(param1.charAt(3))) >= Tile.COLORS.length)
{
return null;
}
_loc4_.setS(_loc7_);
if((_loc7_ = int(param1.charAt(4))) >= Tile.COLORS.length)
{
return null;
}
_loc4_.setSW(_loc7_);
if((_loc7_ = int(param1.charAt(5))) >= Tile.COLORS.length)
{
return null;
}
_loc4_.setNW(_loc7_);
param1 = param1.substr(6);
}
for each(_loc4_ in _loc3_.tiles)
{
for each(_loc8_ in _loc3_.tiles)
{
if(_loc8_ != _loc4_ && _loc8_.startSlot == _loc4_.startSlot)
{
return null;
}
}
if(_loc4_.n && (!_loc4_.slot.n || _loc4_.slot.n.tile.s != _loc4_.n))
{
return null;
}
if(_loc4_.ne && (!_loc4_.slot.ne || _loc4_.slot.ne.tile.sw != _loc4_.ne))
{
return null;
}
if(_loc4_.se && (!_loc4_.slot.se || _loc4_.slot.se.tile.nw != _loc4_.se))
{
return null;
}
if(_loc4_.s && (!_loc4_.slot.s || _loc4_.slot.s.tile.n != _loc4_.s))
{
return null;
}
if(_loc4_.sw && (!_loc4_.slot.sw || _loc4_.slot.sw.tile.ne != _loc4_.sw))
{
return null;
}
if(_loc4_.nw && (!_loc4_.slot.nw || _loc4_.slot.nw.tile.se != _loc4_.nw))
{
return null;
}
}
_loc3_.startPositions();
return _loc3_;
}
public function redoRandomFill() : void
{
var _loc1_:Tile = null;
homePositions();
for each(_loc1_ in tiles)
{
if(_loc1_.locked)
{
_loc1_.unlock();
}
if(_loc1_.confirmed)
{
_loc1_.unconfirm();
}
_loc1_.setN(0);
_loc1_.setNE(0);
_loc1_.setSE(0);
_loc1_.setS(0);
_loc1_.setSW(0);
_loc1_.setNW(0);
_loc1_.startSlot = _loc1_.slot;
}
if(!randomColors)
{
randomFill();
}
else
{
randomFill(randomConnections,randomLocked,randomColors);
}
shuffleStart();
}
public function setScale(param1:Number) : void
{
fullScale = scaleX = scaleY = param1;
}
public function shrink() : void
{
SoundBar.playSound(SoundBar.EXIT_LEVEL_SOUND,mouseX / Game.MIDX - 1);
scaleFraction = 1;
addEventListener(Event.ENTER_FRAME,shrinkStep,false,0,true);
}
public function homePositions() : void
{
var _loc1_:Tile = null;
for each(_loc1_ in tiles)
{
_loc1_.slot = _loc1_.homeSlot;
_loc1_.slot.tile = _loc1_;
_loc1_.snapToSlot();
}
for each(_loc1_ in tiles)
{
_loc1_.checkNeighbours();
}
}
private function expand(param1:Slot) : void
{
var _loc2_:Slot = null;
if(!param1)
{
return;
}
if(!param1.n)
{
_loc2_ = new Slot(param1.x + Slot.N.x,param1.y + Slot.N.y);
slots.push(_loc2_);
param1.n = _loc2_;
_loc2_.s = param1;
if(param1.nw)
{
param1.nw.ne = _loc2_;
_loc2_.sw = param1.nw;
if(param1.nw.n)
{
param1.nw.n.se = _loc2_;
_loc2_.nw = param1.nw.n;
}
}
if(param1.ne)
{
param1.ne.nw = _loc2_;
_loc2_.se = param1.ne;
if(param1.ne.n)
{
param1.ne.n.sw = _loc2_;
_loc2_.ne = param1.ne.n;
}
}
}
if(!param1.ne)
{
_loc2_ = new Slot(param1.x + Slot.NE.x,param1.y + Slot.NE.y);
slots.push(_loc2_);
param1.ne = _loc2_;
_loc2_.sw = param1;
if(param1.n)
{
param1.n.se = _loc2_;
_loc2_.nw = param1.n;
if(param1.n.ne)
{
param1.n.ne.s = _loc2_;
_loc2_.n = param1.n.ne;
}
}
if(param1.se)
{
param1.se.n = _loc2_;
_loc2_.s = param1.se;
if(param1.se.ne)
{
param1.se.ne.nw = _loc2_;
_loc2_.se = param1.se.ne;
}
}
}
if(!param1.se)
{
_loc2_ = new Slot(param1.x + Slot.SE.x,param1.y + Slot.SE.y);
slots.push(_loc2_);
param1.se = _loc2_;
_loc2_.nw = param1;
if(param1.ne)
{
param1.ne.s = _loc2_;
_loc2_.n = param1.ne;
if(param1.ne.se)
{
param1.ne.se.sw = _loc2_;
_loc2_.ne = param1.ne.se;
}
}
if(param1.s)
{
param1.s.ne = _loc2_;
_loc2_.sw = param1.s;
if(param1.s.se)
{
param1.s.se.n = _loc2_;
_loc2_.s = param1.s.se;
}
}
}
if(!param1.s)
{
_loc2_ = new Slot(param1.x + Slot.S.x,param1.y + Slot.S.y);
slots.push(_loc2_);
param1.s = _loc2_;
_loc2_.n = param1;
if(param1.se)
{
param1.se.sw = _loc2_;
_loc2_.ne = param1.se;
if(param1.se.s)
{
param1.se.s.nw = _loc2_;
_loc2_.se = param1.se.s;
}
}
if(param1.sw)
{
param1.sw.se = _loc2_;
_loc2_.nw = param1.sw;
if(param1.sw.s)
{
param1.sw.s.ne = _loc2_;
_loc2_.sw = param1.sw.s;
}
}
}
if(!param1.sw)
{
_loc2_ = new Slot(param1.x + Slot.SW.x,param1.y + Slot.SW.y);
slots.push(_loc2_);
param1.sw = _loc2_;
_loc2_.ne = param1;
if(param1.s)
{
param1.s.nw = _loc2_;
_loc2_.se = param1.s;
if(param1.s.sw)
{
param1.s.sw.n = _loc2_;
_loc2_.s = param1.s.sw;
}
}
if(param1.nw)
{
param1.nw.s = _loc2_;
_loc2_.n = param1.nw;
if(param1.nw.sw)
{
param1.nw.sw.se = _loc2_;
_loc2_.nw = param1.nw.sw;
}
}
}
if(!param1.nw)
{
_loc2_ = new Slot(param1.x + Slot.NW.x,param1.y + Slot.NW.y);
slots.push(_loc2_);
param1.nw = _loc2_;
_loc2_.se = param1;
if(param1.sw)
{
param1.sw.n = _loc2_;
_loc2_.s = param1.sw;
if(param1.sw.nw)
{
param1.sw.nw.ne = _loc2_;
_loc2_.sw = param1.sw.nw;
}
}
if(param1.n)
{
param1.n.sw = _loc2_;
_loc2_.ne = param1.n;
if(param1.n.nw)
{
param1.n.nw.s = _loc2_;
_loc2_.n = param1.n.nw;
}
}
}
}
public function grow() : void
{
SoundBar.playSound(SoundBar.ENTER_LEVEL_SOUND,mouseX / Game.MIDX - 1);
scaleFraction = 0;
scaleX = scaleY = 0;
addEventListener(Event.ENTER_FRAME,growStep,false,0,true);
}
public function saveToText() : String
{
var _loc2_:Tile = null;
var _loc1_:* = "" + size;
for each(_loc2_ in tiles)
{
if(_loc2_.locked)
{
_loc1_ += "1";
}
else
{
_loc1_ += "0";
}
_loc1_ += slots.lastIndexOf(_loc2_.startSlot) + "_";
_loc1_ += _loc2_.n;
_loc1_ += _loc2_.ne;
_loc1_ += _loc2_.se;
_loc1_ += _loc2_.s;
_loc1_ += _loc2_.sw;
_loc1_ += _loc2_.nw;
}
return Encrypt.encrypt(_loc1_);
}
public function startPositions() : void
{
var _loc1_:Tile = null;
for each(_loc1_ in tiles)
{
_loc1_.slot = _loc1_.startSlot;
_loc1_.slot.tile = _loc1_;
_loc1_.snapToSlot();
}
for each(_loc1_ in tiles)
{
_loc1_.checkNeighbours();
}
}
public function shuffleStart() : void
{
var _loc2_:Tile = null;
var _loc3_:Tile = null;
var _loc1_:int = tiles.length * 2;
while(_loc1_ >= 0)
{
_loc2_ = tiles[int(Math.random() * tiles.length)];
_loc3_ = tiles[int(Math.random() * tiles.length)];
if(!(_loc2_ == _loc3_ || _loc2_.locked || _loc3_.locked))
{
_loc2_.slot.swapTiles(_loc3_.slot);
_loc2_.startSlot = _loc2_.slot;
_loc3_.startSlot = _loc3_.slot;
}
_loc1_--;
}
}
private function shrinkStep(param1:Event) : void
{
scaleFraction = Math.max(0,scaleFraction - 0.1);
scaleX = scaleY = scaleFraction * scaleFraction * fullScale;
if(scaleFraction == 0)
{
removeEventListener(Event.ENTER_FRAME,shrinkStep);
dispatchEvent(new HexEvent(HexEvent.SHRUNK,false,false,this));
}
}
public function randomFill(param1:Number = 0.35, param2:Number = 0.2, param3:int = 4) : void
{
var _loc4_:Tile = null;
var _loc5_:Slot = null;
var _loc9_:int = 0;
randomConnections = param1;
randomLocked = param2;
randomColors = param3;
var _loc6_:int = Math.min(tiles.length - 3,Math.floor(param2 * tiles.length));
while(_loc6_ > 0)
{
if((_loc4_ = tiles[int(Math.random() * tiles.length)]).locked)
{
_loc6_++;
}
else
{
_loc4_.lock();
}
_loc6_--;
}
var _loc7_:int = 0;
var _loc8_:int = 0;
for each(_loc5_ in slots)
{
if(_loc5_.n && !(_loc5_.tile.locked && _loc5_.n.tile.locked) && (Math.random() < param1 || _loc7_ / (_loc7_ + _loc8_) < param1 * 0.8) && _loc7_ / (_loc7_ + _loc8_) < param1 * 1.1)
{
_loc9_ = int(Math.random() * param3) + 1;
_loc5_.tile.setN(_loc9_);
_loc5_.n.tile.setS(_loc9_);
_loc7_++;
}
else
{
_loc8_++;
}
if(_loc5_.ne && !(_loc5_.tile.locked && _loc5_.ne.tile.locked) && (Math.random() < param1 || _loc7_ / (_loc7_ + _loc8_) < param1 * 0.8) && _loc7_ / (_loc7_ + _loc8_) < param1 * 1.1)
{
_loc9_ = int(Math.random() * param3) + 1;
_loc5_.tile.setNE(_loc9_);
_loc5_.ne.tile.setSW(_loc9_);
_loc7_++;
}
else
{
_loc8_++;
}
if(_loc5_.nw && !(_loc5_.tile.locked && _loc5_.nw.tile.locked) && (Math.random() < param1 || _loc7_ / (_loc7_ + _loc8_) < param1 * 0.8) && _loc7_ / (_loc7_ + _loc8_) < param1 * 1.1)
{
_loc9_ = int(Math.random() * param3) + 1;
_loc5_.tile.setNW(_loc9_);
_loc5_.nw.tile.setSE(_loc9_);
_loc7_++;
}
else
{
_loc8_++;
}
}
}
private function growStep(param1:Event) : void
{
scaleFraction = Math.min(1,scaleFraction + 0.1);
scaleX = scaleY = scaleFraction * scaleFraction * fullScale;
if(scaleFraction == 1)
{
removeEventListener(Event.ENTER_FRAME,growStep);
dispatchEvent(new HexEvent(HexEvent.GROWN,false,false,this));
}
}
}
}