1. package
  2. {
  3. import away3d.extrusions.ElevationReader;
  4.  
  5. import flash.events.KeyboardEvent;
  6.  
  7. import mx.core.Application;
  8.  
  9. public class MoveableCamera extends BaseObject
  10. {
  11. protected static const MOVESPEED:Number = 100;
  12. protected static const TURNSPEED:Number = 90;
  13. protected var forward:Boolean = false;
  14. protected var backward:Boolean = false;
  15. protected var turnLeft:Boolean = false;
  16. protected var turnRight:Boolean = false;
  17. protected var elevationreader:ElevationReader = null;
  18.  
  19. public function MoveableCamera()
  20. {
  21. super();
  22. }
  23.  
  24. public function startupMoveableCamera(engineManager:EngineManager):MoveableCamera
  25. {
  26. super.startupBaseObject(engineManager);
  27.  
  28. Application.application.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
  29. Application.application.stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
  30.  
  31. elevationreader = new ElevationReader(0);
  32. elevationreader.traceLevels(engineManager.MyResourceManager.HeightmapTex,
  33. "r",
  34. ApplicationManager.SUBDIVIDE_X,
  35. ApplicationManager.SUBDIVIDE_Y,
  36. ApplicationManager.SCALE_X,
  37. ApplicationManager.SCALE_Y,
  38. ApplicationManager.SCALE_Z);
  39.  
  40. return this;
  41. }
  42.  
  43. protected function keyDown(event:KeyboardEvent):void
  44. {
  45. if (event.keyCode == 38)
  46. {
  47. this.forward = true;
  48. }
  49. else if (event.keyCode == 40)
  50. {
  51. this.backward = true;
  52. }
  53. else if (event.keyCode == 39)
  54. {
  55. this.turnRight = true;
  56. }
  57. else if (event.keyCode == 37)
  58. {
  59. this.turnLeft = true;
  60. }
  61. }
  62.  
  63. protected function keyUp(event:KeyboardEvent):void
  64. {
  65. if (event.keyCode == 38)
  66. {
  67. this.forward = false;
  68. }
  69. else if (event.keyCode == 40)
  70. {
  71. this.backward = false;
  72. }
  73. else if (event.keyCode == 39)
  74. {
  75. this.turnRight = false;
  76. }
  77. else if (event.keyCode == 37)
  78. {
  79. this.turnLeft = false;
  80. }
  81. }
  82.  
  83. public override function enterFrame(dt:Number):void
  84. {
  85. if (this.forward)
  86. this.engineManager.cam.moveForward(MOVESPEED * dt);
  87. else if (this.backward)
  88. this.engineManager.cam.moveBackward(MOVESPEED * dt);
  89.  
  90. if (this.turnLeft)
  91. this.engineManager.cam.rotationY -= TURNSPEED * dt;
  92. else if (this.turnRight)
  93. this.engineManager.cam.rotationY += TURNSPEED * dt;
  94.  
  95. this.engineManager.cam.y = elevationreader.getLevel(this.engineManager.cam.x, -this.engineManager.cam.z, 0);
  96. }
  97. }
  98. }