/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/WZGM
*/
package {
import flash.text.TextField;
import flash.display.StageQuality;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
stage.quality = StageQuality.MEDIUM;
initEmpty(16,16);
var i:int;
for (i=0;i<16;i+=1) { setTile(i, 14, 1); }
for (i=0;i<16;i+=1) { setTile(0, i, 1); }
for (i=0;i<16;i+=1) { setTile(14, i, 1); }
for (i=0;i<16;i+=1) { if(Math.random()<0.75) { setTile(i, 4, 1);} }
//for (i=0;i<16;i+=1) { if(Math.random()<0.75) { setTile(i, 7, 1);} }
for (i=0;i<16;i+=1) { if(Math.random()<0.75) { setTile(i, 10, 1);} }
// for (i=0;i<16;i+=1) { if(Math.random()<0.75) { setTile(i, 13, 1);} }
for (i=0;i<16;i+=1) { if(Math.random()<0.5) { setTile(5, i, 1);} }
// for (i=0;i<16;i+=1) { if(Math.random()<0.5) { setTile(10, i, 1);} }
var a:xAct;
a = new xAct();
a.cx=230; a.cy=230;
vecAct.push(a);
deb =new TextField();deb.width=320;deb.height=240;
deb.mouseEnabled=false; addChild(deb);
curAct = vecAct[0];
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKdown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKup);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMdown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMup);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var deb:TextField;
public var mbutton:int = 0;
public function onMdown(e:MouseEvent):void { mbutton = 1; }
public function onMup(e:MouseEvent):void { mbutton =0; }
public function onKdown(e:KeyboardEvent):void { vecKey[e.keyCode] = true; }
public function onKup(e:KeyboardEvent):void { vecKey[e.keyCode] = false; }
public var curAct:xAct = null;
public function onEnter(e:Event):void
{
graphics.clear();
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
deb.text= " "+curAct.djump;
graphics.lineStyle(2,0);
var a:xAct; var num:int; var vec:Vector.<xAct>;
vec= vecAct; num = vec.length;
for (i=0;i<num;i+=1)
{
a = vecAct[i];
a.update();
graphics.drawCircle(a.cx,a.cy,8);
}//nexti
if (curAct != null)
if (mbutton>0) { a = curAct; a.cx=stage.mouseX; a.cy=stage.mouseY; a.vx=0; a.vy=-1; a.hangMode=0; }
}//onenter
}//classend
}
import flash.ui.Keyboard;
var vecGrid:Vector.<int>;
var mw:int = 0; var mh:int = 0;
var cw:Number = 32; var ch:Number = 32;
function initEmpty(aw:int, ah:int):void
{ mw=aw; mh=ah; vecGrid = new Vector.<int>(aw*ah, false); }
function setTile(ax:int, ay:int, t:int):void
{ if (ax<0||ax>=mw||ay<0||ay>=mh) {return;} vecGrid[ay*mw+ax]=t; }
function getTile(ax:int, ay:int ):int
{ if (ax<0||ax>=mw||ay<0||ay>=mh) {return 0;} return vecGrid[ay*mw+ax]; }
function isWall(ax:Number,ay:Number):Boolean
{
if (getTile(ax/cw,ay/ch)>0){return true;}
return (ax<0||ay<0||ax>=465||ay>=465);
}//iswall
var vecAct:Vector.<xAct> = new Vector.<xAct>(0,false);
var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,false);
function isKeyDown(k:int):Boolean { return vecKey[k] == true; }
internal class xAct
{
public var cx:Number = 0; public var cy:Number = 0;
public var vx:Number = 0; public var vy:Number = 0;
public var gx:Number = 0; public var gy:Number = 0;
public var canJump:Boolean = false;
public var djump:int =0;
public var holdJump:int = 0;
public var hangMode:int = 0;
public var dir:int = 0;
public function onMove():void
{
var keyJump:Boolean; var keyLeft:Boolean; var keyRight:Boolean;
var keyUp:Boolean; var keyDown:Boolean;
var ms:Number; ms =0.5; var ws:Number;ws=4;
keyJump = isKeyDown(Keyboard.UP);
keyLeft = isKeyDown(Keyboard.LEFT);
keyRight = isKeyDown(Keyboard.RIGHT);
keyUp = isKeyDown(Keyboard.UP);
keyDown = isKeyDown(Keyboard.DOWN);
if (keyJump) { holdJump +=1; } else { holdJump = 0; }
if (canJump) { djump = 0; }
if (keyLeft) { vx-=ms; if (vx<-ws){vx=-ws;} dir = 16;}
if (keyRight) { vx+=ms; if (vx>ws){vx=ws;} dir=0; }
if (keyJump && canJump) { vy=-7; canJump=false; return; }
//if (keyJump==false &&vy<0){vy+=0.3;}
if (canJump&&(isKeyDown(Keyboard.LEFT) || isKeyDown(Keyboard.RIGHT))==false) { vx*=0.8; }
if (isWall(cx-8-4, cy) && vy>=0 /*&& keyLeft*/ ) { hangMode = 1; vy=0; vx=0; }
if (isWall(cx+8+4, cy) && vy>=0 ) { hangMode = 2; vy=0; vx=0; }
if (hangMode > 0)
{
if (hangMode==1 && keyRight) { vx=ws; vy=-2; hangMode=0; if (keyUp) {vy=-5; } }
if (hangMode==1 && isWall(cx-8-4, cy)==false) { hangMode = 0; vy=-4; vx=0; }
if (hangMode==2 && keyLeft) { vx=-ws; vy=-2; hangMode=0; if (keyUp) {vy=-5; } }
if (hangMode==2 && isWall(cx+8+4, cy)==false) { hangMode = 0; vy=-4; vx=0; }
if (keyUp) {vy=-ws;}
if (keyDown) {vy=ws;}
if (keyUp==false && keyDown==false) {vy=0;}
if (vy>0&&isWall(cx,cy+8+vy)) { vy=0; }
else if (vy<0&&isWall(cx,cy-8)) { vy=0;}
cy+=vy;
if (hangMode==1)
{ if (isWall(cx-8,cy)) { cx+=1; } else if (isWall(cx-8-1,cy)==false) { cx-=1; } }
if (hangMode==2)
{ if (isWall(cx+8,cy)) { cx-=1; } else if (isWall(cx+8+1,cy)==false) { cx+=1; } }
}//endif
}//onmove
public function update():void
{
onMove();
if (hangMode > 0) { return; }
vy+=0.3; vx*=0.98;
if (vy>1||vy<-1) { canJump=false;}
if (vx>0&&isWall(cx+8,cy)) { vx=0;}
else if (vx<0&&isWall(cx-8,cy)) { vx=0;}
if (vy>0&&isWall(cx,cy+8+vy)) { vy=0; canJump=true; }
else if (vy<0&&isWall(cx,cy-8)) { vy=0;}
cx+=vx; cy+=vy;
var i:int;
if (isWall(cx,cy+8)) { cy= Math.floor(cy); cy-=1; }
else { if (vy>=0&&vy<1&&isWall(cx,cy+9)==false) { cy+=1; } }
}//update
}//xact