56 lines
1.3 KiB
JavaScript
56 lines
1.3 KiB
JavaScript
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;
|
|
|
|
function dojump(event) {
|
|
var modifiers = event.altKey || event.ctrlKey || event.metaKey ||
|
|
event.shiftKey;
|
|
|
|
if (!modifiers) {
|
|
if (event.which >= 8 && event.which < 48) event.preventDefault();
|
|
self.emit("jump");
|
|
}
|
|
}
|
|
|
|
function dojump2(event) {
|
|
event.preventDefault();
|
|
self.emit("jump");
|
|
}
|
|
|
|
document.addEventListener("keydown", dojump);
|
|
var gameContainer = document.querySelector(".game-container");
|
|
gameContainer.addEventListener("click", dojump2);
|
|
gameContainer.addEventListener("touchstart", dojump2);
|
|
};
|
|
|
|
KeyboardInputManager.prototype.restart = function (event) {
|
|
event.preventDefault();
|
|
this.emit("restart");
|
|
};
|
|
|
|
KeyboardInputManager.prototype.keepPlaying = function (event) {
|
|
event.preventDefault();
|
|
this.emit("keepPlaying");
|
|
};
|