- package
- {
- import away3d.animators.data.AnimationSequence;
- import away3d.cameras.SpringCam;
- import away3d.core.base.Mesh;
- import away3d.core.math.Number3D;
- import away3d.extrusions.ElevationReader;
- import away3d.loaders.Md2;
- import flash.events.KeyboardEvent;
- import mx.core.Application;
- public class Player extends MeshObject
- {
- protected static const MOVESPEED:Number = 100;
- protected static const TURNSPEED:Number = 90;
- protected var moving:Boolean = false;
- 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;
- protected var springCamera:SpringCam = null;
- protected var mesh:Mesh = null;
- public function Player()
- {
- super();
- }
- public function startupPlayer(engineManager:EngineManager):Player
- {
- // setup mesh
- mesh = Md2.parse(new ResourceManager.Model(), {scaling:0.01});
- mesh.material = engineManager.MyResourceManager.ModelTex;
- super.startupObject3DMeshObject(engineManager, mesh);
- setAnimation("stand");
- // setup elevation reader
- 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);
- // setup spring camera
- springCamera = new SpringCam();
- springCamera.positionOffset = new Number3D(150, 20, 0);
- springCamera.damping = 10;
- springCamera.stiffness = 0.01;
- springCamera.target = mesh;
- engineManager.view.camera = springCamera;
- 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
- {
- var stillMoving:Boolean = true;
- if (this.forward)
- mesh.moveLeft(MOVESPEED * dt);
- else if (this.backward)
- mesh.moveRight(MOVESPEED * dt);
- else
- stillMoving = false;
- if (moving && !stillMoving)
- setAnimation("stand");
- else if (!moving && stillMoving)
- setAnimation("run");
- moving = stillMoving;
- if (this.turnLeft)
- mesh.rotationY -= TURNSPEED * dt;
- else if (this.turnRight)
- mesh.rotationY += TURNSPEED * dt;
- mesh.y = elevationreader.getLevel(mesh.x, -mesh.z, -100);
- }
- protected function setAnimation(animation:String):void
- {
- mesh.play(new AnimationSequence (animation, true, true, 10));
- }
- }
- }