PageRenderTime 34ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/js/input/Keyboard.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 123 lines | 83 code | 13 blank | 27 comment | 10 complexity | d1e63cfc88279562e1dc213c004b0433 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. (function () {
  2. RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.Input");
  3. /**
  4. * A helper class to detect the current state of the controls of the game.
  5. */
  6. RealtimeMultiplayerGame.Input.Keyboard = function () {
  7. this.keys = {'tab': false, 'shift': false, 'space': false, 'up': false, 'down': false, 'left': false, "right": false }
  8. };
  9. RealtimeMultiplayerGame.Input.Keyboard.prototype = {
  10. keyCodes: { '16': 'shift', '32': 'space', '37': 'left', '38': 'up', '39': 'right', '40': 'down', '9': 'tab'},
  11. keyPressed: 0,
  12. dealloc: function () {
  13. // TODO: remove keyup/keydown events
  14. },
  15. keyDown: function (e) {
  16. if (e.keyCode in this.keyCodes) {
  17. // if we're already pressing down on the same key, then we don't want to increment
  18. // our key pressed count
  19. if (!this.keys[ this.keyCodes[ e.keyCode ] ]) {
  20. this.keyPressed++;
  21. }
  22. this.handler(e.keyCode, true);
  23. e.preventDefault();
  24. }
  25. },
  26. keyUp: function (e) {
  27. if (e.keyCode in this.keyCodes) {
  28. this.handler(e.keyCode, false);
  29. this.keyPressed--;
  30. e.preventDefault();
  31. }
  32. },
  33. /**
  34. * Attach events to the HTML element
  35. * We don't care about a time clock here, we attach events, we only want
  36. * to know if something's happened.
  37. */
  38. attachEvents: function () {
  39. var that = this;
  40. document.addEventListener('keydown', function (e) {
  41. that.keyDown(e);
  42. }, false);
  43. document.addEventListener('keyup', function (e) {
  44. that.keyUp(e);
  45. }, false);
  46. },
  47. isKeyPressed: function () {
  48. return this.keyPressed > 0;
  49. },
  50. /**
  51. * Map it to something useful so we know what it is
  52. */
  53. handler: function (keyCode, enabled) {
  54. this.keys[ this.keyCodes[ keyCode] ] = enabled;
  55. },
  56. /**
  57. * Constructs a bitmask based on current keyboard state
  58. * @return A bitfield containing input states
  59. */
  60. constructInputBitmask: function () {
  61. var input = 0;
  62. // Check each key
  63. if (this.keys['up']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.UP;
  64. if (this.keys['down']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.DOWN;
  65. if (this.keys['left']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.LEFT;
  66. if (this.keys['right']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.RIGHT;
  67. if (this.keys['space']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.SPACE;
  68. if (this.keys['shift']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.SHIFT;
  69. if (this.keys['tab']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.TAB;
  70. return input;
  71. },
  72. /**
  73. * Sets the 'key down' properties based on an input mask
  74. * @param inputBitmask A bitfield containing input flags
  75. */
  76. deconstructInputBitmask: function (inputBitmask) {
  77. this.keys['up'] = (inputBitmask & RealtimeMultiplayerGame.Constants.INPUT_BITMASK.UP);
  78. this.keys['down'] = (inputBitmask & RealtimeMultiplayerGame.Constants.INPUT_BITMASK.DOWN);
  79. this.keys['left'] = (inputBitmask & RealtimeMultiplayerGame.Constants.INPUT_BITMASK.LEFT);
  80. this.keys['right'] = (inputBitmask & RealtimeMultiplayerGame.Constants.INPUT_BITMASK.RIGHT);
  81. this.keys['space'] = (inputBitmask & RealtimeMultiplayerGame.Constants.INPUT_BITMASK.SPACE);
  82. this.keys['shift'] = (inputBitmask & RealtimeMultiplayerGame.Constants.INPUT_BITMASK.SHIFT);
  83. },
  84. /**
  85. * Accessors
  86. */
  87. // Some helper methods to find out if we're going in a specific direction
  88. isLeft: function () {
  89. return this.keys['left'];
  90. },
  91. isUp: function () {
  92. return this.keys['up'];
  93. },
  94. isRight: function () {
  95. return this.keys['right'];
  96. },
  97. isDown: function () {
  98. return this.keys['down'];
  99. },
  100. isSpace: function () {
  101. return this.keys['space'];
  102. },
  103. isShift: function () {
  104. return this.keys['shift'];
  105. },
  106. isTab: function () {
  107. return this.keys['tab'];
  108. }
  109. };
  110. })();