PageRenderTime 354ms CodeModel.GetById 161ms app.highlight 10ms RepoModel.GetById 181ms 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
  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
 10    RealtimeMultiplayerGame.Input.Keyboard.prototype = {
 11        keyCodes: { '16': 'shift', '32': 'space', '37': 'left', '38': 'up', '39': 'right', '40': 'down', '9': 'tab'},
 12        keyPressed: 0,
 13
 14        dealloc: function () {
 15            // TODO: remove keyup/keydown events
 16        },
 17
 18        keyDown: function (e) {
 19            if (e.keyCode in this.keyCodes) {
 20                // if we're already pressing down on the same key, then we don't want to increment
 21                // our key pressed count
 22                if (!this.keys[ this.keyCodes[ e.keyCode ] ]) {
 23                    this.keyPressed++;
 24                }
 25
 26                this.handler(e.keyCode, true);
 27                e.preventDefault();
 28            }
 29        },
 30        keyUp: function (e) {
 31            if (e.keyCode in this.keyCodes) {
 32                this.handler(e.keyCode, false);
 33                this.keyPressed--;
 34                e.preventDefault();
 35            }
 36        },
 37
 38        /**
 39         * Attach events to the HTML element
 40         * We don't care about a time clock here, we attach events, we only want
 41         * to know if something's happened.
 42         */
 43        attachEvents: function () {
 44            var that = this;
 45            document.addEventListener('keydown', function (e) {
 46                that.keyDown(e);
 47            }, false);
 48            document.addEventListener('keyup', function (e) {
 49                that.keyUp(e);
 50            }, false);
 51        },
 52
 53        isKeyPressed: function () {
 54            return this.keyPressed > 0;
 55        },
 56
 57        /**
 58         * Map it to something useful so we know what it is
 59         */
 60        handler: function (keyCode, enabled) {
 61            this.keys[ this.keyCodes[ keyCode] ] = enabled;
 62        },
 63
 64        /**
 65         * Constructs a bitmask based on current keyboard state
 66         * @return A bitfield containing input states
 67         */
 68        constructInputBitmask: function () {
 69            var input = 0;
 70
 71            // Check each key
 72            if (this.keys['up']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.UP;
 73            if (this.keys['down']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.DOWN;
 74            if (this.keys['left']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.LEFT;
 75            if (this.keys['right']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.RIGHT;
 76            if (this.keys['space']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.SPACE;
 77            if (this.keys['shift']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.SHIFT;
 78            if (this.keys['tab']) input |= RealtimeMultiplayerGame.Constants.INPUT_BITMASK.TAB;
 79
 80            return input;
 81        },
 82
 83
 84        /**
 85         * Sets the 'key down' properties based on an input mask
 86         * @param inputBitmask    A bitfield containing input flags
 87         */
 88        deconstructInputBitmask: function (inputBitmask) {
 89            this.keys['up'] = (inputBitmask & RealtimeMultiplayerGame.Constants.INPUT_BITMASK.UP);
 90            this.keys['down'] = (inputBitmask & RealtimeMultiplayerGame.Constants.INPUT_BITMASK.DOWN);
 91            this.keys['left'] = (inputBitmask & RealtimeMultiplayerGame.Constants.INPUT_BITMASK.LEFT);
 92            this.keys['right'] = (inputBitmask & RealtimeMultiplayerGame.Constants.INPUT_BITMASK.RIGHT);
 93            this.keys['space'] = (inputBitmask & RealtimeMultiplayerGame.Constants.INPUT_BITMASK.SPACE);
 94            this.keys['shift'] = (inputBitmask & RealtimeMultiplayerGame.Constants.INPUT_BITMASK.SHIFT);
 95        },
 96
 97        /**
 98         * Accessors
 99         */
100        // Some helper methods to find out if we're going in a specific direction
101        isLeft: function () {
102            return this.keys['left'];
103        },
104        isUp: function () {
105            return this.keys['up'];
106        },
107        isRight: function () {
108            return this.keys['right'];
109        },
110        isDown: function () {
111            return this.keys['down'];
112        },
113        isSpace: function () {
114            return this.keys['space'];
115        },
116        isShift: function () {
117            return this.keys['shift'];
118        },
119        isTab: function () {
120            return this.keys['tab'];
121        }
122    };
123})();