Path: blob/main/projects/doge2048/js/keyboard_input_manager.js
4626 views
function KeyboardInputManager() {1this.events = {};23this.listen();4}56KeyboardInputManager.prototype.on = function (event, callback) {7if (!this.events[event]) {8this.events[event] = [];9}10this.events[event].push(callback);11};1213KeyboardInputManager.prototype.emit = function (event, data) {14var callbacks = this.events[event];15if (callbacks) {16callbacks.forEach(function (callback) {17callback(data);18});19}20};2122KeyboardInputManager.prototype.listen = function () {23var self = this;2425var map = {2638: 0, // Up2739: 1, // Right2840: 2, // Down2937: 3, // Left3075: 0, // vim keybindings3176: 1,3274: 2,3372: 3,3487: 0, // W3568: 1, // D3683: 2, // S3765: 3 // A38};3940document.addEventListener("keydown", function (event) {41var modifiers = event.altKey || event.ctrlKey || event.metaKey ||42event.shiftKey;43var mapped = map[event.which];4445if (!modifiers) {46if (mapped !== undefined) {47event.preventDefault();48self.emit("move", mapped);49}5051if (event.which === 32) self.restart.bind(self)(event);52}53});5455var retry = document.querySelector(".retry-button");56retry.addEventListener("click", this.restart.bind(this));57retry.addEventListener("touchend", this.restart.bind(this));5859var keepPlaying = document.querySelector(".keep-playing-button");60keepPlaying.addEventListener("click", this.keepPlaying.bind(this));61keepPlaying.addEventListener("touchend", this.keepPlaying.bind(this));6263var showInfo = document.querySelector(".info-container");64showInfo.addEventListener("click", this.showInfo.bind(this));65showInfo.addEventListener("touchend", this.showInfo.bind(this));6667// var hideInfo = document.querySelector(".hide-info");68// hideInfo.addEventListener("click", this.hideInfo.bind(this));69// hideInfo.addEventListener("touchend", this.hideInfo.bind(this));707172// Listen to swipe events73var touchStartClientX, touchStartClientY;74var gameContainer = document.getElementsByClassName("game-container")[0];7576gameContainer.addEventListener("touchstart", function (event) {77if (event.touches.length > 1) return;7879touchStartClientX = event.touches[0].clientX;80touchStartClientY = event.touches[0].clientY;81event.preventDefault();82});8384gameContainer.addEventListener("touchmove", function (event) {85event.preventDefault();86});8788gameContainer.addEventListener("touchend", function (event) {89if (event.touches.length > 0) return;90var dx = event.changedTouches[0].clientX - touchStartClientX;91var absDx = Math.abs(dx);92var dy = event.changedTouches[0].clientY - touchStartClientY;93var absDy = Math.abs(dy);94if (Math.max(absDx, absDy) > 10) {95// (right : left) : (down : up)96self.emit("move", absDx > absDy ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0));97}98});99};100KeyboardInputManager.prototype.restart = function (event) {101event.preventDefault();102this.emit("restart");103};104KeyboardInputManager.prototype.keepPlaying = function (event) {105event.preventDefault();106this.emit("keepPlaying");107};108KeyboardInputManager.prototype.showInfo = function (event) {109event.preventDefault();110this.emit("showInfo");111};112KeyboardInputManager.prototype.hideInfo = function (event) {113event.preventDefault();114this.emit("hideInfo");115};116117118