implement tile movement
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
function KeyboardInputManager() {
|
||||
this.events = {};
|
||||
|
||||
this.listen();
|
||||
}
|
||||
|
||||
KeyboardInputManager.prototype.on = function (event, callback) {
|
||||
if (!this.events[event]) {
|
||||
this.events[event] = [];
|
||||
}
|
||||
this.events[event].push(callback);
|
||||
};
|
||||
|
||||
KeyboardInputManager.prototype.emit = function (event, data) {
|
||||
var callbacks = this.events[event];
|
||||
if (callbacks) {
|
||||
callbacks.forEach(function (callback) {
|
||||
callback(data);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
KeyboardInputManager.prototype.listen = function () {
|
||||
var self = this;
|
||||
|
||||
var map = {
|
||||
38: 0, // Up
|
||||
39: 1, // Right
|
||||
40: 2, // Down
|
||||
37: 3 // Left
|
||||
};
|
||||
|
||||
document.addEventListener("keydown", function (event) {
|
||||
var modifiers = event.altKey && event.ctrlKey && event.metaKey &&
|
||||
event.shiftKey;
|
||||
var mapped = map[event.which];
|
||||
|
||||
if (!modifiers && mapped !== undefined) {
|
||||
event.preventDefault();
|
||||
self.emit("move", mapped);
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user