flash on 2012-5-14
import flash.media.Camera;
http://alternativaplatform.com/en/docs/7.7.0/index.html
♥0 |
Line 144 |
Modified 2012-10-23 16:11:54 |
MIT License
archived:2017-03-30 22:58:19
ActionScript3 source code
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/AfHr
*/
package {
import flash.text.TextField;
import flash.display.BlendMode;
import flash.display.BitmapData;
import flash.events.KeyboardEvent;
import flash.events.Event;
//import flash.media.Camera;
//http://alternativaplatform.com/en/docs/7.7.0/index.html
import alternativ7.engine3d.containers.ConflictContainer;
import alternativ7.engine3d.containers.BSPContainer;
import alternativ7.engine3d.core.Object3DContainer;
import alternativ7.engine3d.core.Camera3D;
import alternativ7.engine3d.core.EllipsoidCollider;
import alternativ7.engine3d.objects.Mesh;
import alternativ7.engine3d.primitives.Plane;
import alternativ7.engine3d.primitives.Box;
import alternativ7.engine3d.primitives.Sphere;
import alternativ7.engine3d.primitives.GeoSphere;
import alternativ7.engine3d.core.View;
import alternativ7.engine3d.materials.Material;
import alternativ7.engine3d.materials.FillMaterial;
import alternativ7.engine3d.materials.TextureMaterial;
import alternativ7.engine3d.materials.NormalMapMaterial;
import alternativ7.engine3d.materials.FlatShadingMaterial;
import alternativ7.engine3d.materials.VertexLightMaterial;
import alternativ7.engine3d.materials.AverageLightMaterial;
import alternativ7.engine3d.materials.SphericalEnvironmentMaterial;
import alternativ7.types.Texture;
import alternativ7.engine3d.lights.AmbientLight;
import alternativ7.engine3d.lights.DirectionalLight;
import alternativ7.engine3d.lights.OmniLight;
import alternativ7.engine3d.lights.SpotLight;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public var cam:Camera3D;
public var cont:BSPContainer = new BSPContainer();
//public var gBox:Box;
public var debug:TextField;
public var test:Box;
public var player:Box;
public var px:Number = 0;
public var py:Number = 0;
public var pz:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var vz:Number = 0;
public function FlashTest() {
// write as3 code here..
cam = new Camera3D();
cam.view = new View(500,500);
cam.z = -1000;
addChild(cam.view);
cont.addChild(cam);
var bm:BitmapData;
bm = new BitmapData(64,64,false,0);
bm.noise(123213,32,2,3,true);
var p:Plane;
p = new Plane(10000, 10000, 16, 16);
p.setMaterialToAllFaces( new TextureMaterial( bm ) );
p.rotationX = 1.57;
p.y = 200;
cont.addChild(p);
bm = new BitmapData(64,64,false,0);
bm.noise(123123);
var amb:AmbientLight;
amb = new AmbientLight(0xFF0f0F0f);
cont.addChild(amb);
var light:OmniLight;
light = new OmniLight(0xffff0000,500,1200);
//light.intensity = 1000;
light.x =-400
// light.y = -40
light.z = -400;
cont.addChild(light);
player = new Box(64,64,64,1,1,1);
player.setMaterialToAllFaces( new TextureMaterial( bm ) );
cont.addChild(player);
test = new Box(600,30,30,1,1,1);
test.setMaterialToAllFaces(new FillMaterial(0xFF0000,0.5));
cont.addChild(test);
debug = new TextField();
debug.x = 10;
debug.y = 10;
addChild(debug);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
stage.addEventListener(KeyboardEvent.KEY_DOWN, kdown);
stage.addEventListener(KeyboardEvent.KEY_UP, kup);
}//ctor
public var pang:Number = 0;
public function onEnter(e:Event):void
{
//ref
//http://www.dakmm.com/?p=272
if (cKeyMan.isKeyDown(39) ) { vx += 1; }
if (cKeyMan.isKeyDown(37) ) { vx -= 1; }
if (cKeyMan.isKeyDown(38) ) { vz += 1; }
if (cKeyMan.isKeyDown(40) ) { vz -= 1;}
vx *= 0.95; vz *= 0.95;
if (Math.abs(vx) <= 0.1) { vx = 0; }
if (Math.abs(vz) <= 0.1) { vz = 0; }
vy += 0.1;
if (py >= (200-34) && vy > 0) { vy = 0; py = (200-34); }
px += vx;
py += vy;
pz += vz;
player.x = px;
player.y = py;
player.z = pz;
var ang:Number;
ang = Math.atan2(pz-test.z, px-test.x);
//ang *= (3.1415/180.0);
if (ang <0) { ang += 2 * 3.1415;}
//ang *= (180.0/3.1415);
// player.rotationY = ang; // * (3.1415 / 180.0);
// cam.rotationY = -ang + 180;
debug.text = ang.toString();
// gBox.rotationY += 0.1;
pang += 0.01;
// cam.rotationY = -ang;
test.x = player.x + Math.cos(pang) * 500;
test.y = player.y-50;
test.z = player.z + Math.sin(pang) * 500;
test.rotationY = -ang;
cam.rotationY = test.rotationY + 1.57;
cam.x = test.x;
cam.y = test.y-17;
cam.z = test.z;
// cam.y = py - 100;
debug.appendText( "\n " + cam.rotationY );
cam.render();
}//onenter
public function kdown(e:KeyboardEvent):void
{
cKeyMan.setKey(e.keyCode, true);
}//kdown
public function kup(e:KeyboardEvent):void
{
cKeyMan.setKey(e.keyCode, false);
}//kup
}//flashtest
}//package
internal class cKeyMan
{
public function cKeyMan() {}//ctor (unused)
public static var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,true);
public static function setKey(k:int, b:Boolean):void
{
if (k < 0) return;
if (k >= 512) return;
vecKey[k] = b;
}//setkey
public static function isKeyDown(k:int):Boolean
{
if (k < 0) return false;
if (k >= 512) return false;
return vecKey[k];
}//iskey
}//keyman