/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/yhLf
*/
package {
import flash.events.MouseEvent;
import flash.display.Bitmap;
import flash.geom.Matrix;
import flash.display.BitmapData;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
map = new BitmapData(32,32, false, 0);
canvas = new BitmapData(32,32, false, 0);
var b:Bitmap; var i:int; var a:xThing;
for (i=0;i<128;i+=1)
{
map.setPixel(Math.random()*32,Math.random()*32, grass);
}//nexti
for (i=0;i<64;i+=1)
{
map.setPixel(Math.random()*32,Math.random()*32, wall);
}//nexti
var ax:int; var ay:int; var k:int;
vecThing = new Vector.<xThing>(0, false);
for (i=0;i<16;i+=1)
{
for (k=0;k<64;k+=1)
{
ax = Math.random()*32;
ay = Math.random()*32;
if (isWall(ax,ay)) { continue; }
if (isGrass(ax,ay)) { continue; }
a = new xThing();
a.tx = ax;
a.ty = ay;
vecThing.push(a);
break;
}//nextk
}//nexti
b = new Bitmap(canvas);
b.scaleX = 8;
b.scaleY = 8;
addChild(b);
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 mbutton:int = 0;
public var map:BitmapData;
public var canvas:BitmapData;
public var grass:uint = 0x008800;
public var wall:uint = 0x0000cc;
public var vecThing:Vector.<xThing>;
public function isGrass(ax:int, ay:int):Boolean
{ return map.getPixel(ax,ay) == grass; }
public function isWall(ax:int, ay:int):Boolean
{ if (ax<0||ay<0||ax>=32||ay>=32){return true; }
return map.getPixel(ax,ay) == wall; }
public function setTile(ax:int, ay:int, t:uint):void
{ map.setPixel(ax,ay, t); }
public function upThing(a:xThing):void
{
if (a.wait>0){a.wait-=1; return; }
// if (isGrass(a.tx, a.ty)) { a.wait = 50; }
var dx:int; var dy:int;
var i:int; var k:int;
var d:int;
dx=0; dy=0;
for(i=-1;i<=1;i+=1)
for(k=-1;k<=1;k+=1)
{
if (isGrass(k+a.tx,i+a.ty))
{ dx=k; dy=i; i=999999; break;
}//endif
}//nextki
if(dx==0&&dy==0)
{
d= 4;
for(i=-d;i<=d;i+=1)
for(k=-d;k<=d;k+=1)
{
if (isGrass(k+a.tx,i+a.ty))
{ dx=k; dy=i;
if (dx>0&&dx>1){dx=1;}
else if(dx<0&&dx<-1){dx=-1;}
if (dy>0&&dy>1){dy=1;}
else if(dy<0&&dy<-1){dy=-1;}
if (isWall(a.tx+dx, a.ty+dy)) { dx = 0; dy=0; continue; }
i=999999; d=-1; break;
}//endif
}//nextki
}//endif
if(dx!=0||dy!=0)
{
setTile(a.tx, a.ty, 0);
a.tx+=dx; a.ty+=dy;
if (isGrass(a.tx,a.ty)) { a.wait=20;}
else { a.wait = 8; }
setTile(a.tx, a.ty, wall);
return;
}//endif
d = Math.random()*16;
if ((d&1)>0) {dx+=1;}
if ((d&2)>0) {dx+=-1;}
if ((d&4)>0) {dy+=1;}
if ((d&8)>0) {dy+=-1;}
if (isWall(a.tx+dx, a.ty+dy) == false)
{
setTile(a.tx, a.ty, 0);
a.tx+=dx; a.ty+=dy;
setTile(a.tx, a.ty, wall);
a.wait =8;
}//endif
}//upthing
public function onEnter(e:Event):void
{
canvas.lock();
canvas.draw(map,null,null,null,null);
var i:int; var num:int; var a:xThing;
num = vecThing.length;
for(i=0;i<num;i+=1)
{
a = vecThing[i];
upThing(a);
canvas.setPixel(a.tx, a.ty, 0xFFffFF);
}
canvas.unlock();
// if (Math.random() < 0.1)
// { map.setPixel(Math.random()*32,Math.random()*32, grass); }
if (mbutton > 0)
{ map.setPixel(stage.mouseX/8, stage.mouseY/8, grass); }
}//onenter
}//classend
}
internal class xThing
{
public var tx:int = 0;
public var ty:int = 0;
public var wait:int = 0;
}//xthing