Created
March 10, 2016 21:55
-
-
Save timetocode/c2640a1b227cd7e31323 to your computer and use it in GitHub Desktop.
Example of an input manager to go w/ nengi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EventEmitter = require('../../nengi/external/EventEmitter') | |
function InputManager() { | |
EventEmitter.call(this) | |
// tracks the real time state of W A S D | |
this.W = false | |
this.A = false | |
this.S = false | |
this.D = false | |
this.currentKeyState = new KeyState() | |
this.previousKeyState = new KeyState() | |
var self = this | |
/* | |
* If W A S D are ever tapped, then they count as pressed | |
* for a whole frame. | |
*/ | |
document.addEventListener('keydown', function(e) { | |
// W A S D or arrow keys | |
if (e.keyCode === 87 || e.keyCode === 38) { | |
self.W = true | |
self.currentKeyState.W = true | |
} | |
if (e.keyCode === 65 || e.keyCode === 37) { | |
self.A = true | |
self.currentKeyState.A = true | |
} | |
if (e.keyCode === 83 || e.keyCode === 40) { | |
self.S = true | |
self.currentKeyState.S = true | |
} | |
if (e.keyCode === 68 || e.keyCode === 39) { | |
self.D = true | |
self.currentKeyState.D = true | |
} | |
}) | |
document.addEventListener('keyup', function(e) { | |
// W or up arrow | |
if (e.keyCode === 87 || e.keyCode === 38) { self.W = false } | |
// A or left arrow | |
if (e.keyCode === 65 || e.keyCode === 37) { self.A = false } | |
// S or down arrow | |
if (e.keyCode === 83 || e.keyCode === 40) { self.S = false } | |
// D or right arrow | |
if (e.keyCode === 68 || e.keyCode === 39) { self.D = false } | |
}) | |
document.addEventListener('mousedown', function(e) { | |
var mouseX = 0 | |
var mouseY = 0 | |
if (typeof e.offsetX !== 'undefined' && typeof e.offsetY !== 'undefined') { | |
mouseX = e.offsetX | |
mouseY = e.offsetY | |
} else { | |
mouseX = e.layerX | |
mouseY = e.layerY | |
} | |
var rightclick | |
if (!e) var e = window.e | |
if (e.which) rightclick = (e.which == 3) | |
else if (e.button) rightclick = (e.button == 2) | |
if (rightclick) { | |
// right click detected | |
//console.log('right click') | |
//e.preDefault() | |
} else { | |
self.emit('mouseDown', mouseX, mouseY) | |
} | |
}) | |
document.addEventListener('mouseup', function(e) { | |
var mouseX = 0 | |
var mouseY = 0 | |
if (typeof e.offsetX !== 'undefined' && typeof e.offsetY !== 'undefined') { | |
mouseX = e.offsetX | |
mouseY = e.offsetY | |
} else { | |
mouseX = e.layerX | |
mouseY = e.layerY | |
} | |
self.emit('mouseUp', mouseX, mouseY) | |
}) | |
} | |
InputManager.prototype = Object.create(EventEmitter.prototype) | |
InputManager.prototype.constructor = InputManager | |
function KeyState() { | |
this.W = false | |
this.A = false | |
this.S = false | |
this.D = false | |
} | |
InputManager.prototype.update = function() { | |
this.emit('keyState', this.previousKeyState) | |
this.previousKeyState = this.currentKeyState | |
this.currentKeyState = new KeyState() | |
// if a key is still held down, count it as pressed again | |
if (this.W) { | |
this.currentKeyState.W = true | |
} | |
if (this.A) { | |
this.currentKeyState.A = true | |
} | |
if (this.S) { | |
this.currentKeyState.S = true | |
} | |
if (this.D) { | |
this.currentKeyState.D = true | |
} | |
} | |
module.exports = InputManager |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment