/src/GameInput.as

https://bitbucket.org/HopeSky/mars_nd2d · ActionScript · 253 lines · 182 code · 34 blank · 37 comment · 8 complexity · 8228813f73b2b7c6dcda514ead5b5aff MD5 · raw file

  1. // Stage3d game input routines version 1.2
  2. //
  3. package
  4. {
  5. import flash.display.Stage;
  6. import flash.ui.Keyboard;
  7. import flash.events.*;
  8. public class GameInput
  9. {
  10. // the current state of the mouse
  11. public var mouseIsDown:Boolean = false;
  12. public var mouseClickX:int = 0;
  13. public var mouseClickY:int = 0;
  14. public var mouseX:int = 0;
  15. public var mouseY:int = 0;
  16. // the current state of the keyboard controls
  17. public var pressing:Object =
  18. { up:0, down:0, left:0, right:0, fire:0,
  19. strafeLeft:0, strafeRight:0,
  20. key0:0, key1:0, key2:0, key3:0, key4:0,
  21. key5:0, key6:0, key7:0, key8:0, key9:0 };
  22. // if mouselook is on, this is added to the chase camera
  23. public var cameraAngleX:Number = 0;
  24. public var cameraAngleY:Number = 0;
  25. public var cameraAngleZ:Number = 0;
  26. // if this is true, dragging the mouse changes the camera angle
  27. public var mouseLookMode:Boolean = true;
  28. // the game's main stage
  29. public var stage:Stage;
  30. // for click events to be sent to the game
  31. private var _clickfunc:Function = null;
  32. // class constructor
  33. public function GameInput(
  34. theStage:Stage,
  35. clickfunc:Function = null)
  36. {
  37. stage = theStage;
  38. // get keypresses and detect the game losing focus
  39. stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
  40. stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
  41. stage.addEventListener(Event.DEACTIVATE, lostFocus);
  42. stage.addEventListener(Event.ACTIVATE, gainFocus);
  43. stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
  44. stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
  45. stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
  46. // handle clicks in game
  47. _clickfunc = clickfunc;
  48. }
  49. private function mouseMove(e:MouseEvent):void
  50. {
  51. mouseX = e.stageX;
  52. mouseY = e.stageY;
  53. if (mouseIsDown && mouseLookMode)
  54. {
  55. cameraAngleY = 90 * ((mouseX - mouseClickX)
  56. / stage.width);
  57. cameraAngleX = 90 * ((mouseY - mouseClickY)
  58. / stage.height);
  59. }
  60. }
  61. private function mouseDown(e:MouseEvent):void
  62. {
  63. trace('mouseDown at '+e.stageX+','+e.stageY);
  64. mouseClickX = e.stageX;
  65. mouseClickY = e.stageY;
  66. mouseIsDown = true;
  67. if (_clickfunc != null) _clickfunc();
  68. }
  69. private function mouseUp(e:MouseEvent):void
  70. {
  71. trace('mouseUp at '+e.stageX+','+e.stageY+' drag distance:'+
  72. (e.stageX-mouseClickX)+','+(e.stageY-mouseClickY));
  73. mouseIsDown = false;
  74. if (mouseLookMode)
  75. { // reset camera angle
  76. cameraAngleX = cameraAngleY = cameraAngleZ = 0;
  77. }
  78. }
  79. private function keyPressed(event:KeyboardEvent):void
  80. {
  81. // qwer 81 87 69 82
  82. // asdf 65 83 68 70
  83. // left right 37 39
  84. // up down 38 40
  85. // 0123456789 = 48 to 57
  86. // zxcv = 90 88 67 86
  87. // trace("keyPressed " + event.keyCode);
  88. if (event.ctrlKey ||
  89. event.altKey ||
  90. event.shiftKey)
  91. pressing.fire = true;
  92. switch(event.keyCode)
  93. {
  94. /*
  95. case 81: // Q
  96. pressing.strafeLeft = true;
  97. break;
  98. case 69: // E
  99. pressing.strafeRight = true;
  100. break;
  101. */
  102. case Keyboard.UP:
  103. case 87: // W
  104. case 90: // Z
  105. pressing.up = true;
  106. break;
  107. case Keyboard.DOWN:
  108. case 83: // S
  109. pressing.down = true;
  110. break;
  111. case Keyboard.LEFT:
  112. case 65: // A
  113. case 81: // Q
  114. pressing.left = true;
  115. break;
  116. case Keyboard.RIGHT:
  117. case 68: // D
  118. pressing.right = true;
  119. break;
  120. case Keyboard.SPACE:
  121. case Keyboard.SHIFT:
  122. case Keyboard.CONTROL:
  123. case Keyboard.ENTER:
  124. // case 90: // z
  125. case 88: // x
  126. case 67: // c
  127. pressing.fire = true;
  128. break;
  129. case 48: pressing.key0 = true; break;
  130. case 49: pressing.key1 = true; break;
  131. case 50: pressing.key2 = true; break;
  132. case 51: pressing.key3 = true; break;
  133. case 52: pressing.key4 = true; break;
  134. case 53: pressing.key5 = true; break;
  135. case 54: pressing.key6 = true; break;
  136. case 55: pressing.key7 = true; break;
  137. case 56: pressing.key8 = true; break;
  138. case 57: pressing.key9 = true; break;
  139. }
  140. }
  141. private function gainFocus(event:Event):void
  142. {
  143. trace("Game received keyboard focus.");
  144. }
  145. // if the game loses focus, don't keep keys held down
  146. private function lostFocus(event:Event):void
  147. {
  148. trace("Game lost keyboard focus.");
  149. pressing.up = false;
  150. pressing.down = false;
  151. pressing.left = false;
  152. pressing.right = false;
  153. pressing.strafeLeft = false;
  154. pressing.strafeRight = false;
  155. pressing.fire = false;
  156. pressing.key0 = false;
  157. pressing.key1 = false;
  158. pressing.key2 = false;
  159. pressing.key3 = false;
  160. pressing.key4 = false;
  161. pressing.key5 = false;
  162. pressing.key6 = false;
  163. pressing.key7 = false;
  164. pressing.key8 = false;
  165. pressing.key9 = false;
  166. }
  167. private function keyReleased(event:KeyboardEvent):void
  168. {
  169. switch(event.keyCode)
  170. {
  171. /*
  172. case 81: // Q
  173. pressing.strafeLeft = false;
  174. break;
  175. case 69: // E
  176. pressing.strafeRight = false;
  177. break;
  178. */
  179. case Keyboard.UP:
  180. case 87: // W
  181. case 90: // Z
  182. pressing.up = false;
  183. break;
  184. case Keyboard.DOWN:
  185. case 83: // S
  186. pressing.down = false;
  187. break;
  188. case Keyboard.LEFT:
  189. case 65: // A
  190. case 81: // Q
  191. pressing.left = false;
  192. break;
  193. case Keyboard.RIGHT:
  194. case 68: // D
  195. pressing.right = false;
  196. break;
  197. case Keyboard.SPACE:
  198. case Keyboard.SHIFT:
  199. case Keyboard.CONTROL:
  200. case Keyboard.ENTER:
  201. // case 90: // z
  202. case 88: // x
  203. case 67: // c
  204. pressing.fire = false;
  205. break;
  206. case 48: pressing.key0 = false; break;
  207. case 49: pressing.key1 = false; break;
  208. case 50: pressing.key2 = false; break;
  209. case 51: pressing.key3 = false; break;
  210. case 52: pressing.key4 = false; break;
  211. case 53: pressing.key5 = false; break;
  212. case 54: pressing.key6 = false; break;
  213. case 55: pressing.key7 = false; break;
  214. case 56: pressing.key8 = false; break;
  215. case 57: pressing.key9 = false; break;
  216. }
  217. }
  218. } // end class
  219. } // end package