/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/nw2e
*/
package {
import flash.text.TextField;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.display.BitmapData;
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;
deb.textColor=0xFFffFFff;
addChild(deb);
pic = new BitmapData(128,128,false,0);
pic.fillRect(new Rectangle(4,4,51,51),0x808080);
pic.fillRect(new Rectangle(12,12,31,31),0);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKdown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKup);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,false);
public function onKdown(e:KeyboardEvent):void { vecKey[e.keyCode] = true; }
public function onKup(e:KeyboardEvent):void { vecKey[e.keyCode] = false; }
public function isKeyDown(k:int):Boolean { return vecKey[k]; }
public var deb:TextField;
public var ht:Number = 8;
public var cx:Number = 10;
public var cy:Number = 20;
public var vx:Number = 0;
public var vy:Number = 0;
public var cw:Number = 8;
public var ch:Number = 8;
public var pic:BitmapData;
public var mat:Matrix = new Matrix(16,0,0,16,0,0);
public function isWall(ax:Number, ay:Number):Boolean
{ return pic.getPixel(ax,ay)<255; }
public var gt:int = 0;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
pic.unlock();
pic.lock();
mat.a = cw; mat.d = ch;
graphics.beginBitmapFill(pic, mat, false,false);
graphics.drawRect(0,0,465,465);
graphics.endFill();
graphics.lineStyle(2,0xFFffFFff);
graphics.drawRect(cx*cw,cy*ch,cw,ch);
graphics.lineStyle(2,0xFFffFFff, 0.3);
graphics.drawRect(cx*cw,cy*ch-ht*ch,cw,ht*ch);
//vx = 0; vy = 0;
vy += 0.2;
if (isKeyDown(Keyboard.LEFT)) { vx = -3; }
if (isKeyDown(Keyboard.RIGHT)) { vx = 3; }
if (isKeyDown(Keyboard.UP)) { vy = -1; }
if (isKeyDown(Keyboard.DOWN)) { vy = 1; }
if (isKeyDown(Keyboard.SHIFT)) { vy = -2; }
if (isKeyDown(Keyboard.CONTROL)) { pic.setPixel(cx,cy+1,0x808080); }
vx *= 0.6; vy*=0.99;
if (Math.abs(vx)<0.1){vx=0;}
var i:int; var num:int; var ax:int; var ay:int;
// var kx:int; var ky:int;
// kx = Math.floor(cx); ky = Math.floor(cy);
num = Math.round(Math.abs(vx));
ax = vx > 0 ? 1 : -1;
for (i = 0; i < num; i++)
{ if (isWall(cx+ax,cy)) { vx =0; break;} cx+=ax; }
if (vy > 0 && isWall(cx,cy+1)) { vy = 0; }
if (vy < 0 && isWall(cx,cy-ht)) { vy = 0;}
num = Math.round(Math.abs(vy));
ay = vy > 0 ? 1 : -1;
for (i = 0; i < num; i++)
{ if (isWall(cx,cy+ay)) { vy =0; break;} cy+=ay; }
deb.text = ""+vx+"\n"+vy;
// if (vx >0 && isWall(cx+1,cy)==false) { cx += 1 ; }
// if (vx <0 && isWall(cx-1,cy)==false) { cx -= 1 ; }
}//onenter
}//classend
}