- package
- {
- import away3d.extrusions.ElevationReader;
- import flash.events.KeyboardEvent;
- import mx.core.Application;
- public class MoveableCamera extends BaseObject
- {
- protected static const MOVESPEED:Number = 100;
- protected static const TURNSPEED:Number = 90;
- protected var forward:Boolean = false;
- protected var backward:Boolean = false;
- protected var turnLeft:Boolean = false;
- protected var turnRight:Boolean = false;
- protected var elevationreader:ElevationReader = null;
- public function MoveableCamera()
- {
- super();
- }
- public function startupMoveableCamera(engineManager:EngineManager):MoveableCamera
- {
- super.startupBaseObject(engineManager);
- Application.application.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
- Application.application.stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
- elevationreader = new ElevationReader(0);
- elevationreader.traceLevels(engineManager.MyResourceManager.HeightmapTex,
- "r",
- ApplicationManager.SUBDIVIDE_X,
- ApplicationManager.SUBDIVIDE_Y,
- ApplicationManager.SCALE_X,
- ApplicationManager.SCALE_Y,
- ApplicationManager.SCALE_Z);
- return this;
- }
- protected function keyDown(event:KeyboardEvent):void
- {
- if (event.keyCode == 38)
- {
- this.forward = true;
- }
- else if (event.keyCode == 40)
- {
- this.backward = true;
- }
- else if (event.keyCode == 39)
- {
- this.turnRight = true;
- }
- else if (event.keyCode == 37)
- {
- this.turnLeft = true;
- }
- }
- protected function keyUp(event:KeyboardEvent):void
- {
- if (event.keyCode == 38)
- {
- this.forward = false;
- }
- else if (event.keyCode == 40)
- {
- this.backward = false;
- }
- else if (event.keyCode == 39)
- {
- this.turnRight = false;
- }
- else if (event.keyCode == 37)
- {
- this.turnLeft = false;
- }
- }
- public override function enterFrame(dt:Number):void
- {
- if (this.forward)
- this.engineManager.cam.moveForward(MOVESPEED * dt);
- else if (this.backward)
- this.engineManager.cam.moveBackward(MOVESPEED * dt);
- if (this.turnLeft)
- this.engineManager.cam.rotationY -= TURNSPEED * dt;
- else if (this.turnRight)
- this.engineManager.cam.rotationY += TURNSPEED * dt;
- this.engineManager.cam.y = elevationreader.getLevel(this.engineManager.cam.x, -this.engineManager.cam.z, 0);
- }
- }
- }