flash on 2013-7-2
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/e5oM
*/
package {
import flash.display.Graphics;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
map = new BitmapData(mwidth, mheight,false, 0);
pic = new Bitmap(map);
pic.scaleX = cw;
pic.scaleY = ch;
setRect((mwidth/2)-4,(mheight/2)-4, 8,8, 0xFFffFF);
basex = cw*(mwidth*0.5);
basey = ch*(mheight*0.5);
addChild(pic);
layerDeb = new Sprite();
addChild(layerDeb);
testAct = new xActor();
testAct.cx = basex;
testAct.cy = basey;
vecAct = new Vector.<xActor>(0, false);
var i:int;
var a:xActor;
for (i = 0; i < 16; i++)
{
a = new xActor();
a.cx = basex;
a.cy = basey;
a.ang = Math.random()*6.28;
vecAct.push(a);
//vecAct.push(new xActor() );
}
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var basex:Number = 0;
public var basey:Number = 0;
public var pic:Bitmap;
public var map:BitmapData;
public var mwidth:int = 25;
public var mheight:int = 25;
public var cw:Number = 16;
public var ch:Number = 16;
public var layerDeb:Sprite;
public var tempRect:Rectangle = new Rectangle();
public function setRect(ax:Number, ay:Number, aw:Number, ah:Number, c:uint):void
{
tempRect.x = ax;
tempRect.y = ay;
tempRect.width = aw;
tempRect.height = ah;
map.fillRect(tempRect, c);
}//setrect
public function isWall(wx:Number, wy:Number):Boolean
{
var tx:int;
var ty:int;
tx = Math.floor(wx/cw);
ty = Math.floor(wy/ch);
if (tx < 0) { return true; }
if (ty < 0) { return true; }
if (tx >= mwidth) { return true; }
if (ty >= mheight) { return true; }
return (map.getPixel(tx, ty) < 0xFFffFF);
}//iswall
public function crackMap(wx:Number, wy:Number, m:uint=0x111111):void
{
var tx:int;
var ty:int;
var c:uint;
tx = Math.floor(wx/cw);
ty = Math.floor(wy/ch);
if (tx < 0) { return ; }
if (ty < 0) { return ; }
if (tx >= mwidth) { return ; }
if (ty >= mheight) { return ; }
c = map.getPixel(tx, ty) ;
if (c < (0xFFffFF-m))
{
c += m;
} else { c = 0xFFffFF;}
map.setPixel(tx, ty, c);
}///crackmap
public function setMap(wx:Number, wy:Number, c:uint):void
{
var tx:int;
var ty:int;
tx = Math.floor(wx/cw);
ty = Math.floor(wy/ch);
if (tx < 0) { return ; }
if (ty < 0) { return ; }
if (tx >= mwidth) { return ; }
if (ty >= mheight) { return ; }
map.setPixel(tx,ty,c);
}//setmap
public var vecAct:Vector.<xActor>;
public var testAct:xActor;
public function onEnter(e:Event):void
{
var g:Graphics;
var a:xActor;
var num:int;
var i:int;
g = layerDeb.graphics;
g.clear();
g.lineStyle(2, 0xFF0000);
a = testAct;
var paxe:uint;
paxe= 0x111111*6;
num = vecAct.length;
for (i = 0; i < num; i++)
{
a =vecAct[i];
g.drawCircle(a.cx, a.cy, 4);
a.vx = Math.cos(a.ang)*4;
a.vy = Math.sin(a.ang)*4;
if (a.cargo > 0)
{
var dx:Number;
var dy:Number;
dx = basex - a.cx;
dy = basey - a.cy;
a.ang = Math.atan2(dy, dx);
if (Math.abs(dx) <8 && Math.abs(dy)<8)
{ a.cargo = 0; a.vx = 0; a.vy = 0; a.ang = Math.random()*6.28; }
}
if (a.cargo <= 0)
{
if (a.vx > 0 && isWall(a.cx+4, a.cy)) { a.vx = 0; a.cargo = 1; crackMap(a.cx+4,a.cy,paxe); }
else if (a.vx < 0 && isWall(a.cx-4, a.cy)) { a.vx = 0; a.cargo = 1; crackMap(a.cx-4,a.cy,paxe); }
else if (a.vy > 0 && isWall(a.cx, a.cy+4)) { a.vy = 0; a.cargo = 1; crackMap(a.cx,a.cy+4,paxe); }
else if (a.vy < 0 && isWall(a.cx, a.cy-4)) { a.vy = 0; a.cargo = 1; crackMap(a.cx,a.cy-4,paxe); }
}
if (a.vx > 0 && isWall(a.cx+4, a.cy)) { a.vx = 0; }
else if (a.vx < 0 && isWall(a.cx-4, a.cy)) { a.vx = 0; }
if (a.vy > 0 && isWall(a.cx, a.cy+4)) { a.vy = 0; }
else if (a.vy < 0 && isWall(a.cx, a.cy-4)) { a.vy = 0; }
a.cx += a.vx;
a.cy += a.vy;
if (a.cargo > 0)
{
g.beginFill(0,0.2);
//g.drawCircle(a.cx, a.cy, 6);
g.drawRect(a.cx-cw*0.5,a.cy-ch*0.5,cw,ch);
g.endFill();
}
}//nexti
}//onenter
}//classend
}
internal class xActor
{
public var cx:Number = 0;
public var cy:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var ang:Number = 0;
public var cargo:int = 0;
public var mode:int = 0;
}//xactor