1. package
  2. {
  3. import away3d.animators.data.AnimationSequence;
  4. import away3d.cameras.SpringCam;
  5. import away3d.core.base.Mesh;
  6. import away3d.core.math.Number3D;
  7. import away3d.extrusions.ElevationReader;
  8. import away3d.loaders.Md2;
  9.  
  10. import flash.events.KeyboardEvent;
  11.  
  12. import mx.core.Application;
  13.  
  14. public class Player extends MeshObject
  15. {
  16. protected static const MOVESPEED:Number = 100;
  17. protected static const TURNSPEED:Number = 90;
  18. protected var moving:Boolean = false;
  19. protected var forward:Boolean = false;
  20. protected var backward:Boolean = false;
  21. protected var turnLeft:Boolean = false;
  22. protected var turnRight:Boolean = false;
  23. protected var elevationreader:ElevationReader = null;
  24. protected var springCamera:SpringCam = null;
  25. protected var mesh:Mesh = null;
  26.  
  27. public function Player()
  28. {
  29. super();
  30. }
  31.  
  32. public function startupPlayer(engineManager:EngineManager):Player
  33. {
  34. // setup mesh
  35. mesh = Md2.parse(new ResourceManager.Model(), {scaling:0.01});
  36. mesh.material = engineManager.MyResourceManager.ModelTex;
  37.  
  38. super.startupObject3DMeshObject(engineManager, mesh);
  39.  
  40. setAnimation("stand");
  41.  
  42. // setup elevation reader
  43. Application.application.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
  44. Application.application.stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
  45.  
  46. elevationreader = new ElevationReader(0);
  47. elevationreader.traceLevels(engineManager.MyResourceManager.HeightmapTex,
  48. "r",
  49. ApplicationManager.SUBDIVIDE_X,
  50. ApplicationManager.SUBDIVIDE_Y,
  51. ApplicationManager.SCALE_X,
  52. ApplicationManager.SCALE_Y,
  53. ApplicationManager.SCALE_Z);
  54.  
  55. // setup spring camera
  56. springCamera = new SpringCam();
  57. springCamera.positionOffset = new Number3D(150, 20, 0);
  58. springCamera.damping = 10;
  59. springCamera.stiffness = 0.01;
  60. springCamera.target = mesh;
  61. engineManager.view.camera = springCamera;
  62.  
  63. return this;
  64. }
  65.  
  66. protected function keyDown(event:KeyboardEvent):void
  67. {
  68. if (event.keyCode == 38)
  69. {
  70. this.forward = true;
  71. }
  72. else if (event.keyCode == 40)
  73. {
  74. this.backward = true;
  75. }
  76. else if (event.keyCode == 39)
  77. {
  78. this.turnRight = true;
  79. }
  80. else if (event.keyCode == 37)
  81. {
  82. this.turnLeft = true;
  83. }
  84. }
  85.  
  86. protected function keyUp(event:KeyboardEvent):void
  87. {
  88. if (event.keyCode == 38)
  89. {
  90. this.forward = false;
  91. }
  92. else if (event.keyCode == 40)
  93. {
  94. this.backward = false;
  95. }
  96. else if (event.keyCode == 39)
  97. {
  98. this.turnRight = false;
  99. }
  100. else if (event.keyCode == 37)
  101. {
  102. this.turnLeft = false;
  103. }
  104. }
  105.  
  106. public override function enterFrame(dt:Number):void
  107. {
  108. var stillMoving:Boolean = true;
  109.  
  110. if (this.forward)
  111. mesh.moveLeft(MOVESPEED * dt);
  112. else if (this.backward)
  113. mesh.moveRight(MOVESPEED * dt);
  114. else
  115. stillMoving = false;
  116.  
  117. if (moving && !stillMoving)
  118. setAnimation("stand");
  119. else if (!moving && stillMoving)
  120. setAnimation("run");
  121.  
  122. moving = stillMoving;
  123.  
  124. if (this.turnLeft)
  125. mesh.rotationY -= TURNSPEED * dt;
  126. else if (this.turnRight)
  127. mesh.rotationY += TURNSPEED * dt;
  128.  
  129. mesh.y = elevationreader.getLevel(mesh.x, -mesh.z, -100);
  130. }
  131.  
  132. protected function setAnimation(animation:String):void
  133. {
  134. mesh.play(new AnimationSequence (animation, true, true, 10));
  135. }
  136.  
  137. }
  138. }