/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/9rEh
*/
package {
import flash.ui.Keyboard;
import flash.text.TextField;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
deb = new TextField();
deb.width=320; deb.height=240;
deb.mouseEnabled=false;
addChild(deb);
heightPic = new BitmapData(mw,mh, false, 0);
vecFloor = new Vector.<xVecFloor>(0,false);
var i:int;
for (i = 0; i < 8; i+=1) { vecFloor.push(new xVecFloor()); }
addBox(4,4+3, 3,7, 1);
addBox(4,4, 3,3, 2);
addBox(4+3,4, 6,3, 3);
addBox(12,12, 3,3, 4);
addBox(12-1,12, 1,2, 3);
addBox(12-2,12, 1,2, 2);
addBox(12-3,12, 1,2, 1);
addBox(7,7, 5,5, 2);
addBox(1,15, 12,1, 6);
cx=128; cz=128;
//deb.text = ""+vecFloor[1].vec;
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 function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
camx=230; camz=-120;
drawFloor(0,0,0, 256,256, 0xff504080);
var i:int; var num:int; var a:xBox;
num = vecBox.length;
for (i = 0; i < num; i+=1)
{
a = vecBox[i];
//drawFloor(a.cx,a.cy,a.cz, a.cw,a.cd, 0xA0B0F0);
drawBox2(a.cx,a.cy,a.cz, a.cw,a.ch,a.cd);
}//nexti
var k:int; var nk:int; var vec:Vector.<xBox>;
nk = vecFloor.length;
for (k = 0; k < nk; k++)
{
vec = vecFloor[k].vec;
num = vec.length;
for (i = 0; i < num; i+=1)
{
a = vec[i];
drawFloor(a.cx,a.cy+a.ch,a.cz, a.cw,a.cd, 0x606060+ (0x0F0F1F * k));
//drawBox2(a.cx,a.cy,a.cz, a.cw,a.ch,a.cd);
}//nexti
if (k == int(cy/chh2))
{ drawBox2(cx,cy,cz, 8, 16, 8); }
}//nextk
var kd:int;
var kx:Number; var ky:Number; var kz:Number;
var ms:Number; ms = 8;
kx = cx; ky = cy; kz =cz; kd =0;
if (isKeyDown(Keyboard.LEFT)) { kx-=ms; kd=1; }
if (isKeyDown(Keyboard.RIGHT)) { kx+=ms; kd=1; }
if (isKeyDown(Keyboard.UP)) { kz-=ms;kd=1; }
if (isKeyDown(Keyboard.DOWN)) { kz+=ms;kd=1; }
var wy:Number;
ky = getHeightWorld(kx,kz);
wy = getHeightWorld(cx,cz);
//cx=kx; cy=ky; cz=kz;
if (ky-wy < 20)
{cx=kx; cy=ky; cz=kz; }
else
{
//hack to slide at walls
ky = getHeightWorld(cx,kz);
if (ky-wy < 20)
{ cy=ky; cz=kz; }
else
{
ky = getHeightWorld(kx,cz);
if (ky-wy < 20)
{cx=kx; cy=ky; }
}
}//endif
//drawBox2(cx,cy,cz, 8, 16, 8);
}//onenter
public var deb:TextField;
public var rendw:Number = 465 * 0.5; public var rendh:Number = 465 * 0.5;
public var camx:Number = 0; public var camy:Number = 0; public var camz:Number = 0;
public var vecFloor:Vector.<xVecFloor>;
public var vecBox:Vector.<xBox> = new Vector.<xBox>(0,false);
public var heightPic:BitmapData;
public var cw:Number = 16; public var ch:Number = 16;
public var chh2:Number = 16;
public var mw:int = 16; public var mh:int = 16;
public var cx:Number = 0; public var cy:Number = 0; public var cz:Number = 0;
public function getHeightWorld(ax:Number, az:Number):Number
{ return heightPic.getPixel(ax/cw,az/ch); }
public var rect:Rectangle = new Rectangle();
public function setHeight(ax:Number,ay:Number,aw:Number,ah:Number, h:Number):void
{ rect.x=ax;rect.y=ay;rect.width=aw;rect.height=ah; heightPic.fillRect(rect,h); }
public function addBox(ax:Number, az:Number, aw:Number, ad:Number, h:int):void
{
if (h < 0) { h=0; } if (h >= vecFloor.length) { h = vecFloor.length-1;}
var a:xBox; a = new xBox();
vecBox.push(a); vecFloor[h].vec.push(a);
a.cx = ax*cw; a.cy=0; a.cz = az*ch;
a.cw = aw*cw; a.ch= h * chh2; a.cd = ad*ch;
setHeight(ax,az, aw,ad, a.ch);
}//addbox
public function drawFloor(ax:Number, ay:Number, az:Number, aw:Number, ad:Number, c:uint):void
{
graphics.beginFill(c, 1);
addPlot(ax,ay,az, false);
addPlot(ax+aw,ay,az, true);
addPlot(ax+aw,ay,az+ad, true);
addPlot(ax,ay,az+ad, true);
addPlot(ax,ay,az, true);
graphics.endFill();
}//drawfloor
public function addPlot(ax:Number, ay:Number, az:Number, c:Boolean=false):void
{
var sx:Number; var sy:Number;
ax -= camx; ay -= camy; az -= camz;
sx = ax +az*0.5 + rendw; sy = (ax * 0) + (az * 1) - ay;
if (c) {graphics.lineTo(sx, sy);} else { graphics.moveTo(sx,sy); }
}//addplot
public function drawLine(ax:Number, ay:Number, az:Number, bx:Number, by:Number, bz:Number):void
{
var sx:Number; var sy:Number;
ax -= camx; ay -= camy; az -= camz; bx -= camx; by -= camy; bz -= camz;
sx = ax +az*0.5 + rendw; sy = (ax * 0) + (az * 1) - ay;
graphics.moveTo(sx,sy);
sx = bx +bz*0.5 + rendw; sy = (bx * 0) + (bz * 1) - by;
graphics.lineTo(sx, sy);
}//drawline
public function drawBox2(ax:Number, ay:Number, az:Number,
aw:Number, ah:Number, ad:Number):void
{ drawBox(ax,ay,az,ax+aw,ay+ah,az+ad); }
public function drawBox(ax:Number, ay:Number, az:Number,
bx:Number, by:Number, bz:Number):void
{
drawLine(ax, ay, az, bx, ay, az);
drawLine(ax, by, az, bx, by, az);
drawLine(ax, ay, bz, bx, ay, bz);
drawLine(ax, by, bz, bx, by, bz);
drawLine(ax, ay, az, ax, by, az);
drawLine(bx, ay, az, bx, by, az);
drawLine(ax, ay, bz, ax, by, bz);
drawLine(bx, ay, bz, bx, by, bz);
drawLine(ax, ay, az, ax, ay, bz);
drawLine(bx, ay, az, bx, ay, bz);
drawLine(ax, by, az, ax, by, bz);
drawLine(bx, by, az, bx, by, bz);
}//drawbox
}//classend
}
internal class xVecFloor
{
public var vec:Vector.<xBox> = new Vector.<xBox>(0,false);
}
internal class xBox
{
public var cx:Number =0; public var cy:Number =0; public var cz:Number =0;
public var cw:Number =0; public var ch:Number =0; public var cd:Number =0;
}//xbox