/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ilZd
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
initEmpty(32,16);
var i:int; var k:int;
// for (i=0;i<16;i+=1) { setTile(i, 14, 1); }
for (i=0;i<8;i+=1)for(k=0;k<10;k+=1)
{ setTile(2+k,3+i,1); }
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var gx:Number = 0;
public var gy:Number = 430;
public var cx:Number = 230;
public var cy:Number = 230;
public var vx:Number = 0;
public var vy:Number = 0;
public var ang:Number=6.28*0.75;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2,0);
var ax:Number; var ay:Number;
var i:int; var k:int; var yt:int; var t:int;
for (i=0;i<mh;i+=1)
{
yt = i * mw;
for (k=0;k<mw;k+=1)
{
t = vecGrid[k+yt];
if (t<=0) { continue; }
ax=k*cw;ay=i*ch;
graphics.beginFill(0x404040, 1);
graphics.drawRect(ax,ay,cw,ch);
graphics.endFill();
continue;
}//nextk
}//nexti
gx = stage.mouseX;
if (gx<32){gx=32;}
if (gx>432){gx=432;}
graphics.drawRect(gx-32,gy-16,64,32);
vx = Math.cos(ang)*6;
vy = Math.sin(ang)*6;
var wallhit:int; wallhit=0;
if (vx<0&&cx<32){vx=-vx; wallhit=1;}
if (vx>0&&cx>432){vx=-vx; wallhit=1;}
if (vy<0&&cy<32){ vy=-vy;wallhit=1;}
if (vy>0&&cy>460){cx=230;cy=230; vy=-4; ang =6.28*0.75;}
if (isWall(cx+vx,cy)) { wallhit=1; setTile2(cx+vx,cy,0); vx=-vx; }
if (isWall(cx,cy+vy)) { wallhit=1; setTile2(cx,cy+vy,0); vy=-vy; }
if (isWall(cx+vx,cy+vy)) { wallhit=1; setTile2(cx+vx,cy+vy,0); vx=-vx; vy=-vy; }
/*
if (vx <0 && isWall(cx+8,cy)) {vx=-vx; wallhit=1; setTile2(cx+8,cy,0); }
if (vx >0 && isWall(cx-8,cy)) {vx=-vx; wallhit=1; setTile2(cx-8,cy,0); }
if (vy <0 && isWall(cx,cy+8)) {vy=-vy; wallhit=1; setTile2(cx,cy+8,0); }
if (vy >0 && isWall(cx,cy-8)) {vy=-vy; wallhit=1; setTile2(cx,cy-8,0); }
*/
if ( (cx+8<gx-32 || cy+8<gy-16 || cx-8 >gx+32 || cy-8>gy+16)==false)
if (vy > 0)
{ vy=-vy; wallhit=1; if (cx<gx-8){vx-=1;}else if (cx>gx+8){vx+=1;} }
if (wallhit>0)
{ ang = Math.atan2(vy,vx); ang+=Math.random()*0.1-0.05; }
cx+=vx;
cy+=vy;
graphics.drawCircle(cx,cy,8);
graphics.drawRect(0,0,32,465);
graphics.drawRect(432,0,32,465);
}//onenter
public var vecGrid:Vector.<int>;
public var mw:int = 0; public var mh:int = 0;
public var cw:Number = 32; public var ch:Number = 16;
public function initEmpty(aw:int, ah:int):void
{ mw=aw; mh=ah; vecGrid = new Vector.<int>(aw*ah, false); }
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 0;} return vecGrid[ay*mw+ax]; }
public function getTile2(ax:Number, ay:Number ):int
{ return getTile(ax/cw, ay/ch); }
public function setTile2(ax:Number, ay:Number, t:int ):void
{ setTile(ax/cw, ay/ch, t); }
public function isWall(ax:Number,ay:Number):Boolean
{ return getTile2(ax,ay)>0; }
}//classend
}