/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/s2Fx
*/
package {
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
deb = new TextField();
deb.width=320;
deb.height=240;
deb.mouseEnabled=false;
addChild(deb);
var i:int; var k:int; var num:int;
var a:xTile; var yt:int;
mw=13;mh=13;
num=mw*mh;
vecGrid = new Vector.<xTile>(num,false);
for(i=0;i<num;i+=1){ vecGrid[i] = new xTile(); }
for(i=0;i<32;i+=1)
{ setTile(Math.random()*mw, Math.random()*mh, 0x808080); }
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMdown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMup);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public function onMdown(e:MouseEvent):void { mbutton = 1; }
public function onMup(e:MouseEvent):void { mbutton = 0; }
public var deb:TextField;
public var mbutton:int = 0;
public var mx:Number = 0;
public var my:Number = 0;
public var mhold:int = 0;
public var vecGrid:Vector.<xTile>// = new Vector.<int>(16*16,false);
public var mw:int = 16;
public var mh:int = 16;
public var cw:Number = 32;
public var ch:Number = 32;
public function setTile(ax:int, ay:int, t:int):void
{ if (ax<0||ax>=mw||ay<0||ay>=mh){return;} vecGrid[ax+(ay*mw)].col = t; }
public function getTile(ax:int, ay:int):xTile
{ if (ax<0||ax>=mw||ay<0||ay>=mh){return null;} return vecGrid[ax+(ay*mw)]; }
public function isZone(ax:int, ay:int):Boolean
{ var a:xTile; a = getTile(ax,ay); if (a==null){return false;} if(a.col!=0){return true;} return false; }
public var cx:int = -32;
public var cy:int = -32;
public var wx:int = 1;
public var wy:int = 0;
public var curTest:int = 0;
public var mode:int = 0;
public var vecPnt:Vector.<xPnt> = new Vector.<xPnt>(0,false);
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(1,0);
graphics.drawRect(0,0, mw*cw, mh*ch);
mx = stage.mouseX;
my = stage.mouseY;
var tx:int; var ty:int;
tx = Math.floor(mx/cw);
ty = Math.floor(my/ch);
var a:xTile;
var yt:int; var t:int;
var i:int; var k:int;
for (i=0;i<mh;i+=1)
{
yt = mw * i;
for (k=0;k<mw;k+=1)
{
//graphics.drawRect(k*cw, i*ch, cw, ch);
a = getTile(k,i);
if (a.col>0)
{
graphics.lineStyle();
graphics.beginFill(a.test == curTest?0x909090 : a.col,1);
graphics.drawRect(k*cw, i*ch, cw, ch);
graphics.endFill();
}//endif
}//nextk
}//nexti
graphics.lineStyle(4, 0xFF);
graphics.drawRect(tx*cw,ty*ch,cw,ch);
if (mbutton > 0) { mhold += 1; }
else { mhold = 0; }
a = getTile(tx,ty);
if (a != null && mhold == 1)
{
mhold = 3;
if (a.col == 0)
{
mode =0x808080;
a.col = 0x808080 ;
for (i=0;i<mh;i+=1)
{ a=getTile(tx,ty);
if (a==null || a.col == 0){ty-=1; break;}
ty +=1;
}//nexti
cx=tx; cy=ty;
wx=1;wy=0;
a = getTile(tx-1,ty);
if (a != null && a.col != 0) { wx=-1; wy =0; }
if (a == null || a.col ==0) { wx=0;wy=-1;}
curTest += 1;
vecPnt = new Vector.<xPnt>(0,false);
}
else
{
a.col=0;
mode =0;
}//endif2
}//endif
if (a!=null && mhold>3)
{ a.col=mode; }
graphics.drawCircle(cx*cw+cw*0.5,cy*ch+ch*0.5, 8);
// graphics.drawCircle(cx*cw+cw*0.5+wy*cw,cy*ch+ch*0.5+-wx*ch, 4);
var makePnt:Boolean;
var tmp:int;
makePnt = false;
a = getTile(cx+wx, cy+wy);
if (a == null || a.col == 0)
{ tmp = wx; wx = -wy; wy = tmp; }
else
{
if (a.test != curTest) {makePnt=true;}
a.test = curTest;
/*
var f:int;
f = 0;
a = getTile(cx-1, cy-1); if (a == null || a.col != 0) { f |= 1; }
a = getTile(cx-1, cy+1); if (a == null || a.col != 0) { f |= 2; }
a = getTile(cx+1, cy-1); if (a == null || a.col != 0) { f |= 4; }
a = getTile(cx+1, cy+1); if (a == null || a.col != 0) { f |= 8; }
if (wx>0)
if ((f&8) == 0 && vecPnt.length < 1024)
{ p = new xPnt(); vecPnt.push(p); p.cx = cx*cw+cw; p.cy = cy*ch+ch; }
if (wy>0)
if ((f&2) == 0 && vecPnt.length < 1024)
{ p = new xPnt(); vecPnt.push(p); p.cx = cx*cw; p.cy = cy*ch+ch; }
if (wy<0)
if ((f&1) == 0 && vecPnt.length < 1024)
{ p = new xPnt(); vecPnt.push(p); p.cx = cx*cw; p.cy = cy*ch; }
if (wx<0)
if ((f&4) == 0 && vecPnt.length < 1024)
{ p = new xPnt(); vecPnt.push(p); p.cx = cx*cw+cw; p.cy = cy*ch; }
*/
/*
if (vecPnt.length < 1024)
{
p = new xPnt();
p.cx = cx*cw+cw*0.5; p.cy =cy*ch+ch*0.5;
p.cx += wy *cw; p.cy+=-wx*ch;
vecPnt.push(p);
}//endif
*/
cx+=wx; cy+=wy;
a = getTile(cx+wy,cy-wx);
if (a!=null || a.col!=0)
{ tmp= wx; wx=wy; wy=-tmp; }
}//endif
var p:xPnt;
var num:int;
num = vecPnt.length;
for (i=0;i<num;i+=1)
{
p = vecPnt[i];
graphics.drawCircle(p.cx, p.cy, 4);
}//nexti
for (i=0;i<num;i+=1)
{
p = vecPnt[i];
if (i==0) { graphics.moveTo(p.cx, p.cy); }
graphics.lineTo(p.cx, p.cy);
}//nexti
}//onenter
}//classend
}
internal class xTile
{
public var col:int = 0;
public var test:int = 0;
}//xtile
internal class xPnt
{
public var cx:Number = 0;
public var cy:Number = 0;
}//xpnt