press space to restart
[2048.git] / js / keyboard_input_manager.js
blob:a/js/keyboard_input_manager.js -> blob:b/js/keyboard_input_manager.js
--- a/js/keyboard_input_manager.js
+++ b/js/keyboard_input_manager.js
@@ -27,7 +27,11 @@
     38: 0, // Up
     39: 1, // Right
     40: 2, // Down
-    37: 3  // Left
+    37: 3, // Left
+    75: 0, // vim keybindings
+    76: 1,
+    74: 2,
+    72: 3
   };
 
   document.addEventListener("keydown", function (event) {
@@ -47,6 +51,23 @@
 
   var retry = document.getElementsByClassName("retry-button")[0];
   retry.addEventListener("click", this.restart.bind(this));
+
+  // Listen to swipe events
+  var gestures = [Hammer.DIRECTION_UP, Hammer.DIRECTION_RIGHT,
+                  Hammer.DIRECTION_DOWN, Hammer.DIRECTION_LEFT];
+
+  var gameContainer = document.getElementsByClassName("game-container")[0];
+  var handler       = Hammer(gameContainer, {
+    drag_block_horizontal: true,
+    drag_block_vertical: true
+  });
+  
+  handler.on("swipe", function (event) {
+    event.gesture.preventDefault();
+    mapped = gestures.indexOf(event.gesture.direction);
+
+    if (mapped !== -1) self.emit("move", mapped);
+  });
 };
 
 KeyboardInputManager.prototype.restart = function (event) {

comments