/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/im5M
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
initEmpty(32, 32);
var i:int;
for (i=0;i<256;i+=1)
{ setTile(Math.random()*mw, Math.random()*mh, 1); }
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var vecGrid:Vector.<int>;
public var vecTest:Vector.<int>;
public var mw:int = 0; public var mh:int = 0;
public var cw:Number = 16; public var ch:Number = 16;
public function initEmpty(aw:int, ah:int):void
{ mw=aw; mh=ah; vecGrid = new Vector.<int>(aw*ah, false);
vecTest = new Vector.<int>(aw*ah, false);
}//initempty
public function setTile(ax:int, ay:int, t:int):void
{ if (ax<0||ax>=mw||ay<0||ay>=mh) {return;} vecGrid[ay*mw+ax]=t; }
public function getTile(ax:int, ay:int):int
{ if (ax<0||ax>=mw||ay<0||ay>=mh) {return 1;} return vecGrid[ay*mw+ax]; }
public function setTest(ax:int, ay:int, t:int):void
{ if (ax<0||ax>=mw||ay<0||ay>=mh) {return;} vecTest[ay*mw+ax]=t; }
public function getTest(ax:int, ay:int):int
{ if (ax<0||ax>=mw||ay<0||ay>=mh) {return curTest;} return vecTest[ay*mw+ax]; }
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
var i:int; var k:int; var yt:int; var t:int;
var dx:Number; var dy:Number;
for ( i = 0; i < mh; i+=1)
{ yt = i * mw;
for (k = 0; k < mw; k+=1)
{ t = vecGrid[yt+k]; if (t <= 0) { continue; }
dx = (k*cw); dy = (i*ch);
graphics.beginFill(0x808080,1);
graphics.drawRect(dx, dy, cw, ch);
graphics.endFill();
}//nextk
}//nexti
var mx:Number; var my:Number;
mx = stage.mouseX; my = stage.mouseY;
floodZone(mx, my);
}//onenter
public var vecNode:Vector.<xNode>;
public var curTest:int = 1;
public function floodZone(ax:Number, ay:Number):void
{
var tx:int; var ty:int;
var i:int; var num:int;
var a:xNode; var b:xNode;
var it:int; var d:int; var prev:int;
if (vecNode == null)
{ vecNode = new Vector.<xNode>(4096,false);
num = vecNode.length;
for(i=0;i<num;i+=1) { vecNode[i] = new xNode(); }
}//endif
a = vecNode[0];
a.tx = Math.floor(ax/cw);
a.ty = Math.floor(ay/ch);
a.d = 16;
a.prev = 0;
graphics.lineStyle(4, 0);
graphics.drawRect(a.tx*cw,a.ty*ch,cw,ch);
graphics.lineStyle();
if (getTile(a.tx,a.ty) > 0) { return; }
curTest += 1;
setTest(a.tx, a.ty, curTest);
it = 1;
while (it > 0)
{
it -=1;
a = vecNode[it];
tx = a.tx; ty = a.ty;
prev = a.prev;
d = a.d - 1;
if (d <= 0) { continue;}
graphics.beginFill(0x800000, 0.3);
graphics.drawRect(tx*cw, ty*ch, cw, ch);
graphics.endFill();
if (it >= 4090) { continue; } //hard limit on fill
if ((prev & 2) == 0)
if (getTile(tx + 1, ty) <= 0 && getTest(tx+1,ty)!=curTest)
{ b = vecNode[it];it += 1; b.d = d; b.tx = tx + 1; b.ty = ty; b.prev = prev|1; setTest(tx + 1, ty, curTest); }
if ((prev & 1) == 0)
if (getTile(tx - 1, ty) <= 0 && getTest(tx-1,ty)!=curTest)
{ b = vecNode[it];it += 1; b.d = d; b.tx = tx - 1; b.ty = ty; b.prev = prev|2; setTest(tx - 1, ty, curTest); }
if ((prev & 8) == 0)
if (getTile(tx, ty+1) <= 0 && getTest(tx,ty+1)!=curTest)
{ b = vecNode[it];it += 1; b.d = d; b.tx = tx; b.ty = ty+1; b.prev = prev|4; setTest(tx , ty+1, curTest); }
if ((prev & 4) == 0)
if (getTile(tx, ty-1) <= 0 && getTest(tx,ty-1)!=curTest)
{ b = vecNode[it];it += 1; b.d = d; b.tx = tx; b.ty = ty-1; b.prev = prev|8; setTest(tx , ty-1, curTest); }
}//wend
}//floodzone
}//classend
}
internal class xNode
{
public var tx:int = 0;
public var ty:int = 0;
public var prev:int = 0;
public var d:int = 0;
}//xnode