1
0
mirror of https://github.com/danog/2048.git synced 2024-12-13 17:57:42 +01:00
2048/js/keyboard_input_manager.js

104 lines
2.6 KiB
JavaScript
Raw Normal View History

2014-03-09 14:32:30 +01:00
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
2014-03-10 18:33:58 +01:00
37: 3, // Left
75: 0, // vim keybindings
76: 1,
74: 2,
2014-03-11 16:31:39 +01:00
72: 3,
87: 0, // W
68: 1, // D
83: 2, // S
65: 3 // A
2014-03-09 14:32:30 +01:00
};
document.addEventListener("keydown", function (event) {
2014-03-10 20:42:03 +01:00
var modifiers = event.altKey || event.ctrlKey || event.metaKey ||
2014-03-09 14:32:30 +01:00
event.shiftKey;
var mapped = map[event.which];
2014-03-10 12:29:35 +01:00
if (!modifiers) {
if (mapped !== undefined) {
event.preventDefault();
self.emit("move", mapped);
}
if (event.which === 32) self.restart.bind(self)(event);
2014-03-09 14:32:30 +01:00
}
});
2014-03-10 11:31:10 +01:00
var retry = document.querySelector(".retry-button");
2014-03-10 12:29:35 +01:00
retry.addEventListener("click", this.restart.bind(this));
retry.addEventListener("touchend", this.restart.bind(this));
2014-03-10 18:47:10 +01:00
var keepPlaying = document.querySelector(".keep-playing-button");
keepPlaying.addEventListener("click", this.keepPlaying.bind(this));
2014-03-10 18:47:10 +01:00
// Listen to swipe events
var touchStartClientX, touchStartClientY;
2014-03-10 18:47:10 +01:00
var gameContainer = document.getElementsByClassName("game-container")[0];
2014-03-12 12:15:57 +01:00
gameContainer.addEventListener("touchstart", function (event) {
2014-03-11 22:18:56 +01:00
if (event.touches.length > 1) return;
touchStartClientX = event.touches[0].clientX;
2014-03-11 22:18:56 +01:00
touchStartClientY = event.touches[0].clientY;
event.preventDefault();
2014-03-10 18:47:10 +01:00
});
2014-03-12 12:15:57 +01:00
gameContainer.addEventListener("touchmove", function (event) {
event.preventDefault();
});
2014-03-12 12:15:57 +01:00
gameContainer.addEventListener("touchend", function (event) {
if (event.touches.length > 0) return;
var dx = event.changedTouches[0].clientX - touchStartClientX;
var absDx = Math.abs(dx);
2014-03-11 22:18:56 +01:00
var dy = event.changedTouches[0].clientY - touchStartClientY;
var absDy = Math.abs(dy);
2014-03-11 22:18:56 +01:00
if (Math.max(absDx, absDy) > 10) {
2014-03-11 22:18:56 +01:00
// (right : left) : (down : up)
self.emit("move", absDx > absDy ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0));
}
2014-03-10 18:47:10 +01:00
});
2014-03-10 12:29:35 +01:00
};
KeyboardInputManager.prototype.restart = function (event) {
event.preventDefault();
this.emit("restart");
2014-03-09 14:32:30 +01:00
};
KeyboardInputManager.prototype.keepPlaying = function (event) {
event.preventDefault();
this.emit("keepPlaying");
};