/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/MQHv
*/
package {
import flash.events.MouseEvent;
import flash.display.StageQuality;
import flash.text.TextField;
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;
addChild(deb);
stage.quality= StageQuality.LOW; //MEDIUM;
var i:int; var num:int;
num=mw*mh;
vecHeight = new Vector.<Number>(num,false);
for (i=0;i<num;i+=1)
{ vecHeight[i] = Math.random()*32; }
for (i=0;i<32;i+=1)
{
addBump(Math.random()*mw*cw,Math.random()*mw*ch, 64,32);
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMdown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMup);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public function onMdown(e:MouseEvent):void { mbutton=1;}
public function onMup(e:MouseEvent):void {mbutton=0;}
public var mbutton:int = 0;
public var deb:TextField;
public var mw:int = 32;
public var mh:int = 32;
public var cw:Number= 32;
public var ch:Number = 32;
public var vecHeight:Vector.<Number>;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2,0);
// drawBox2(0,0,0, 256,16,256);
drawBox2(0,0,0, mw*cw,16,mh*ch);
drawBox3(camx,camy,camz,4,4,4);
var mx:Number; var my:Number;
mx = stage.mouseX; my = stage.mouseY;
var wmx:Number; var wmy:Number; var wmz:Number;
var ax:Number; var ay:Number; var az:Number;
ay = (2*my - (mx-rendw)) * 0.5 ;
ax = (mx-rendw) + ay ;
ax+=camx; ay+=camz;
wmx = ax; wmz = ay; wmy = 0;
// drawBox3(128,0,128,4,4,4);
//drawBox3(128,64,128,4,4,4);
//drawBox3(64,0,64,8,8,8);
var ux:Number; var uy:Number; var uz:Number;
ux=0.5774; uy=0.5774; uz=0.5774;
/*
var d:Number;
ux =64; uy=64;uz=64;
d = getMag3(ux,uy,uz);
deb.text = ""+d.toFixed(4);
deb.text = ""+(64/d).toFixed(4);
*/
drawBox3(wmx,wmy,wmz, 4,1,4);
ax = wmx +ux*-1024;
ay = wmy +uy*-1024;
az = wmz +uz*-1024;
drawBox3(ax,ay,az, 8,1,8);
graphics.lineStyle(2,0x808080);
var h:Number; var h2:Number;
var i:int; var k:int; var yt:int;
for (i=0;i<mh-1;i+=1)
{ yt = i*mw; az = i*ch;
for (k=0;k<mw-1;k+=1)
{
ax = k*cw;
h=vecHeight[yt+k];
h2=vecHeight[yt+k+1];
drawLine(ax,h,az, ax+cw,h2,az);
h2=vecHeight[yt+k+mw];
drawLine(ax,h,az, ax,h2,az+ch);
}//nextk
}//nexti
ax = wmx +ux*1024;
ay = wmy +uy*1024;
az = wmz +uz*1024;
for (i=0;i<1024;i+=1)
{
ax += ux*-8;
ay += uy*-8;
az += uz*-8;
ky = getHeight(ax,az);
if (ky > ay) { break; }
}//nexti
deb.text = ""+i+" "+ay+" "+ky;
wmx=ax; wmy=ky; wmz=az;
if (mbutton>0){ mode=1; }
else if (mode>0){ mode=0; mode2=1-mode2;}
if (mbutton>0)
{ addBump(wmx,wmz, 64,mode2==1?-8:8); }
graphics.lineStyle(2,mode2==1?0xFF:0xFF0000);
var kx:Number; var ky:Number; var kz:Number;
var ta:Number; var ms:Number;
ms=32;
for (ta=0;ta<6.28;ta+=0.314)
{
ax = wmx+Math.cos(ta)*ms;
az = wmz+Math.sin(ta)*ms;
ay = getHeight(ax,az);
kx = wmx+Math.cos(ta+0.314)*ms;
kz = wmz+Math.sin(ta+0.314)*ms;
ky = getHeight(kx,kz);
drawLine(ax,ay,az, kx,ky,kz);
}//nextta
}//onenter
public var mode:int = 0;
public var mode2:int = 0;
public function getMag(ax:Number,ay:Number):Number
{ return Math.sqrt(ax*ax+ay*ay); }
public function getMag3(ax:Number,ay:Number,az:Number):Number
{ return Math.sqrt(ax*ax+ay*ay+az*az); }
public function setHeight(tx:int, ty:int, h:Number):void
{ if (tx<0||tx>=mw||ty<0||ty>=mh) {return;} vecHeight[tx+(ty*mw)] = h; }
public function getHeight(ax:Number, ay:Number):Number
{
var h0:Number; var h1:Number; var h2:Number; var h3:Number;
var tx:int; var ty:int; var yt:int;
var u:Number; var v:Number;
tx = Math.floor(ax/cw); if (tx < 0) { tx = 0; } if (tx >= mw-2) { return 0.0; }
ty = Math.floor(ay/ch); if (ty < 0) { ty = 0; } if (ty >= mw-2) { return 0.0; }
yt = ty*mw;
h0 = vecHeight[yt+tx]; h1 = vecHeight[yt+tx+1];
h2 = vecHeight[yt+tx+mh]; h3 = vecHeight[yt+tx+mh+1];
u = (ax/cw) - tx; v = (ay/ch) - ty;
h0 = h0 + (h1-h0) * u;
h2 = h2 + (h3-h2) * u;
h3 = h0 + (h2-h0) * v;
return h3;
}//getheight
public function addBump(ax:Number,ay:Number, r:Number, h:Number):void
{
var kx:Number; var ky:Number;
var h2:Number; var h3:Number; var d:Number;
var k:int; var i:int; var t:Number;
var tx:int; var ty:int; var tw:int; var th:int;
tx = (ax-r)/cw; ty=(ay-r)/ch;
tw=(r/cw)*2; th=(r/ch)*2;
for(i=0;i<th;i+=1) { for (k=0;k<tw;k+=1) {
kx= (tx+k)*cw; ky=(ty+i)*ch;
d = getMag(kx-ax,ky-ay);
h3 = getHeight(kx,ky);
t = (((r-d)/r));
if (t<0){t=0;} else if (t>1){t=1;}
h2 = t*h;
h2 = h3 + t*h;
setHeight(tx+k,ty+i, h2);
}}
}//addbump
public var camx:Number = 0; public var camy:Number = 0; public var camz:Number = 0;
public var rendw:Number = 465 * 0.5; public var rendh:Number = 465 * 0.5;
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 + rendw; sy = (ax * 0.5) + (az * 0.5) - ay;
graphics.moveTo(sx,sy);
sx = bx - bz + rendw; sy = (bx * 0.5) + (bz * 0.5) - by;
graphics.lineTo(sx, sy);
}//drawline
public function drawBox3(ax:Number,ay:Number,az:Number,
rx:Number,ry:Number,rz:Number):void
{ drawBox(ax-rx,ay-ry,az-rz,ax+rx,ay+ry,az+rz); }
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
}