In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

pv3d screen projection

@author Ryan Liu | www.ryan-liu.com
@since  2009-4-10 8:32
Get Adobe Flash player
by Kazshliu 31 Dec 2009
    Embed
/**
 * Copyright Kazshliu ( http://wonderfl.net/user/Kazshliu )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/9x2l
 */

package {
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.StaticText;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import org.papervision3d.core.math.Sphere3D;
	import org.papervision3d.events.RendererEvent;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
	
	[SWF(backgroundColor="0")]
	/**
	 * @author Ryan Liu | www.ryan-liu.com
	 * @since  2009-4-10 8:32
	 */
	public class ScreeLocation extends BasicView {
		
		private var mat:FlatShadeMaterial
		private var matList:MaterialsList
		private var light:PointLight3D
		private var cross:Shape;
		private var ang:Number = 90
		private var camTarget:DisplayObject3D;
		private var sw:int = stage.stageWidth;
		private var sh:int = stage.stageHeight;
		private var enemyNum:int = 5;
		private var enemyArr:Array = [];
		private var uiColor:int = 0x009922;
		private var logColor:int = 0x00cc44;
		private var targetEnemyID:int = 0;
		private var targetPos:Point = new Point();
		private var crossPos:Point = new Point(sw / 2, sh / 2);
		private var logField:TextField
		private var defaultLog:String = "Press 1-5 to switch target"
		private var warningLog:String = "Target out of sight"
		private var logFormat:TextFormat = new TextFormat('arial', 14, logColor, false, false, false,null, null, 'center');
		private var logTimer:Timer
		private var logTimerOn:Boolean = false;
		private var logTimerDelay:int = 300;
		private var logTimerBuffer:String = "";
		
		public function ScreeLocation() {
			init3D();
			initUI();			
			startRendering();			
			addEventListener(Event.ENTER_FRAME, loop)
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
		}
		private function initUI():void {
			var screenui:Shape = new Shape();
			var g:Graphics = screenui.graphics;
			g.lineStyle(1, uiColor);
			g.drawRect(15, 15, sw - 30 ,sh - 30);
			addChild(screenui)
			cross = new Shape();
			addChild(cross);
			
			logField = new TextField();
			logField.width = 300;
			logField.height = 30;
			logField.x = (sw - 300) * .5;
			logField.y = sh - 40;
			logField.text = defaultLog;
			logField.setTextFormat(logFormat);
			addChild(logField);
			
			setLogInfo(defaultLog);
			
			logTimer = new Timer(logTimerDelay);
			logTimer.addEventListener(TimerEvent.TIMER, onTimer);
		}
		private function init3D():void {
			camera.focus = 10;
			camera.zoom = 35;
			light = new PointLight3D()
			light.x = 8000;
			light.y = 8000
			light.z = -8000
			scene.addChild(light)
			//build cubes
			for (var i:int = 0; i < enemyNum; i++) {
				mat = new FlatShadeMaterial(light, 0xffffff, Math.random() * 0xffffff);
				matList = new MaterialsList({all:mat});
				var enemy:Cube = new Cube(matList, 60, 60, 60, 2, 2, 2);
				enemy.rotationX = Math.random() * 360;
				enemy.rotationY = Math.random() * 360;
				enemy.rotationZ = Math.random() * 360;
				enemy.x = (Math.random() - Math.random()) * 600;
				enemy.y = (Math.random() - Math.random()) * 300;
				enemy.z = (Math.random() - Math.random()) * 300;
				enemy.autoCalcScreenCoords = true;
				scene.addChild(enemy);
				enemyArr.push(enemy);
			}
			//set camera's target			
			camTarget = new DisplayObject3D();
			scene.addChild(camTarget);			
			camera.target = camTarget;
		}
		private function setLogInfo(info:String):void {
			logField.text = info;
			logField.setTextFormat(logFormat);
		}
		private function drawCross():void {
			var curTarget:Cube = enemyArr[targetEnemyID] as Cube;
			var targetX:Number = curTarget.screen.x + sw * .5;
			var targetY:Number = curTarget.screen.y + sh * .5;
			var g:Graphics = cross.graphics;
			g.clear();
			
			if (targetX < 0 || targetX > sw || targetY < 0 || targetY > sh) {
				switchWarning(true);
			} else {
				switchWarning(false);
				crossPos.x += (targetX  - crossPos.x) * .4;
				crossPos.y += (targetY  - crossPos.y) * .4;
				g.lineStyle(1, uiColor);
				g.moveTo(crossPos.x, 0);
				g.lineTo(crossPos.x, sh);
				g.moveTo(0, crossPos.y);
				g.lineTo(sw, crossPos.y);				
				g.drawRect(crossPos.x - 30, crossPos.y - 30, 60, 60);
			}
		}
		
		private function loop(e:Event):void {
			for (var i:int = 0; i < enemyNum; i++) {
				var enemy:Cube = enemyArr[i] as Cube;
				enemy.yaw(i+1);
			}
			camTarget.x += (targetPos.x - camTarget.x) * .2;	
			camTarget.y += (targetPos.y - camTarget.y) * .2;
			
			drawCross();
		}
		
		private function onMouseMove(e:MouseEvent):void {
			var tx:Number = 300*(e.stageX - sw * .5) / (sw * .5);
			var ty:Number = -300*(e.stageY - sh * .5) / (sh * .5);
			targetPos.x = tx;
			targetPos.y = ty;
		}
		
		private function onKeyPress(e:KeyboardEvent):void {
			var keycode:uint = e.keyCode
			if (keycode >= 49 && keycode <= 53) {
				targetEnemyID = keycode - 49;
			}
		}
		
		private function switchWarning(isOn:Boolean):void {
			if (logTimerOn == isOn) {
				return;
			} else {
				logTimerOn = isOn;
			}
			if (isOn) {
				setLogInfo(warningLog);
				logTimerBuffer = warningLog;
				logTimer.start();
			} else {
				setLogInfo(defaultLog);
				logTimer.stop();
			}
		}
		private function onTimer(e:TimerEvent):void {
			if (logField.text == "") {
				setLogInfo(logTimerBuffer);
			} else {
				setLogInfo("");
			}
		}

	}
	
}