press space to restart
press space to restart

function KeyboardInputManager() { function KeyboardInputManager() {
this.events = {}; this.events = {};
   
this.listen(); this.listen();
} }
   
KeyboardInputManager.prototype.on = function (event, callback) { KeyboardInputManager.prototype.on = function (event, callback) {
if (!this.events[event]) { if (!this.events[event]) {
this.events[event] = []; this.events[event] = [];
} }
this.events[event].push(callback); this.events[event].push(callback);
}; };
   
KeyboardInputManager.prototype.emit = function (event, data) { KeyboardInputManager.prototype.emit = function (event, data) {
var callbacks = this.events[event]; var callbacks = this.events[event];
if (callbacks) { if (callbacks) {
callbacks.forEach(function (callback) { callbacks.forEach(function (callback) {
callback(data); callback(data);
}); });
} }
}; };
   
KeyboardInputManager.prototype.listen = function () { KeyboardInputManager.prototype.listen = function () {
var self = this; var self = this;
   
var map = { var map = {
38: 0, // Up 38: 0, // Up
39: 1, // Right 39: 1, // Right
40: 2, // Down 40: 2, // Down
37: 3 // Left 37: 3 // Left
}; };
   
document.addEventListener("keydown", function (event) { document.addEventListener("keydown", function (event) {
var modifiers = event.altKey && event.ctrlKey && event.metaKey && var modifiers = event.altKey && event.ctrlKey && event.metaKey &&
event.shiftKey; event.shiftKey;
var mapped = map[event.which]; var mapped = map[event.which];
   
if (!modifiers && mapped !== undefined) { if (!modifiers) {
event.preventDefault(); if (mapped !== undefined) {
self.emit("move", mapped); event.preventDefault();
  self.emit("move", mapped);
  }
   
  if (event.which === 32) self.restart.bind(self)(event);
} }
}); });
   
var retry = document.getElementsByClassName("retry-button")[0]; var retry = document.getElementsByClassName("retry-button")[0];
retry.addEventListener("click", function (event) { retry.addEventListener("click", this.restart.bind(this));
event.preventDefault();  
self.emit("restart");  
});  
}; };
   
  KeyboardInputManager.prototype.restart = function (event) {
  event.preventDefault();
  this.emit("restart");
  };
   
comments