/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/wpzE
*/
package {
import flash.events.MouseEvent;
import flash.display.StageQuality;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
stage.quality = StageQuality.LOW; //MEDIUM;
var i:int; var num:int;
mw=128; mh=128;
num=mw*mh;
vecHeight = new Vector.<Number>(num,false);
for (i=0;i<num;i+=1)
{ vecHeight[i] = 0; } //Math.random()*32; }
for (i=0;i<128;i+=1)
{ addBump(Math.random()*mw*cw,Math.random()*mw*ch, 128+Math.random(), 64+Math.random()*64); }
for (i=0;i<128;i+=1)
{ addBump(Math.random()*mw*cw,Math.random()*mw*ch, 128+Math.random(), -64); }
cy =getHeight(cx,cz)+16;
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 function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
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;
var ta:Number; var ms:Number;
ms= 8;
ta = Math.atan2(wmz-cz, wmx-cx);
if (mbutton>0)
{
cx+= Math.cos(ta) * ms;
cz+= Math.sin(ta) * ms;
cy =getHeight(cx,cz)+16;
}
camx = cx-230;
camz = cz-230;
var h:Number; var h2:Number;
var i:int; var k:int; var yt:int;
var tx:int; var ty:int;
tx = camx/cw; ty = camz/ch;
tx-=2;ty-=2;
graphics.lineStyle(1, 0x808080);
for (i=0;i<20;i+=1)
{ yt = (i+ty) * mw; az = (i+ty)*ch;
if (i+ty <0){continue;}
if (i+ty >= mh-1){break;}
for (k=0;k<20;k+=1)
{
ax = (k+tx)*cw;
if (k+tx<0){continue;}
if (k+tx>=mw-1){continue;}
h = vecHeight[yt+(k+tx)];
h2 = vecHeight[yt+(k+tx)+1];
drawLine(ax,h,az, ax+cw,h2,az);
h2=vecHeight[yt+(k+tx)+mw];
drawLine(ax,h,az, ax,h2,az+ch);
}//nextk
}//nexti
graphics.lineStyle(2, 0);
var kx:Number; var ky:Number; var kz:Number;
ms=16;
for (ta=0;ta<6.28;ta+=0.314)
{
ax = cx+Math.cos(ta)*ms;
az = cz+Math.sin(ta)*ms;
ay = getHeight(ax,az);
kx = cx+Math.cos(ta+0.314)*ms;
kz = cz+Math.sin(ta+0.314)*ms;
ky = getHeight(kx,kz);
drawLine(ax,ay,az, kx,ky,kz);
}//nextta
var t:Number;
for (t=0;t<1;t+=0.05)
{
kx = cx +(wmx-cx)*t;
kz = cz +(wmz-cz)*t;
ax = cx +(wmx-cx)*(t+0.05);
az = cz +(wmz-cz)*(t+0.05);
ay = getHeight(ax,az);
ky = getHeight(kx,kz);
drawLine(ax,ay,az, kx,ky,kz);
}//nextt
drawLine(ax,ay,az, ax,0,az);
graphics.drawRect(0,0,64,64);
graphics.drawCircle(cx/(cw*2),cz/(ch*2),2);
graphics.drawCircle(wmx/(cw*2),wmz/(ch*2),2);
drawBox3(wmx,wmy,wmz, 4,1,4);
//drawBox3(camx,camy,camz, 8,8,8);
// drawBox3(camx+rendw,camy,camz+rendh, 8,8,8);
graphics.lineStyle(2, 0);
drawBox3(cx,cy,cz, 8,16,8);
}//onEnter
public var cx:Number =2048;
public var cy:Number =64;
public var cz:Number =2048;
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 getMag(ax:Number,ay:Number):Number
{ return Math.sqrt(ax*ax+ay*ay); }
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
}