add tile merging
add tile merging

--- a/js/application.js
+++ b/js/application.js
@@ -1,5 +1,7 @@
-// Wait till the browser is ready to render the game (avoids glitches)
-window.requestAnimationFrame(function () {
-  var manager = new GameManager(4, KeyboardInputManager, HTMLActuator);
+document.addEventListener("DOMContentLoaded", function () {
+  // Wait till the browser is ready to render the game (avoids glitches)
+  window.requestAnimationFrame(function () {
+    var manager = new GameManager(4, KeyboardInputManager, HTMLActuator);
+  });
 });
 

--- a/js/game_manager.js
+++ b/js/game_manager.js
@@ -41,10 +41,11 @@
   this.actuator.actuate(this.grid);
 };
 
-// Saves all the current tile positions
-GameManager.prototype.saveTilePositions = function () {
+// Save all tile positions and remove merger info
+GameManager.prototype.prepareTiles = function () {
   this.grid.eachCell(function (x, y, tile) {
     if (tile) {
+      tile.mergedFrom = null;
       tile.savePosition();
     }
   });
@@ -54,8 +55,7 @@
 GameManager.prototype.moveTile = function (tile, cell) {
   this.grid.cells[tile.x][tile.y] = null;
   this.grid.cells[cell.x][cell.y] = tile;
-  tile.x = cell.x;
-  tile.y = cell.y;
+  tile.updatePosition(cell);
 };
 
 // Move tiles on the grid in the specified direction
@@ -67,9 +67,10 @@
 
   var vector     = this.getVector(direction);
   var traversals = this.buildTraversals(vector);
+  var moved      = false;
 
-  // Save the current tile positions (for actuator awareness)
-  this.saveTilePositions();
+  // Save the current tile positions and remove merger information
+  this.prepareTiles();
 
   // Traverse the grid in the right direction and move tiles
   traversals.x.forEach(function (x) {
@@ -78,14 +79,32 @@
       tile = self.grid.cellContent(cell);
 
       if (tile) {
-        var pos = self.findFarthestPosition(cell, vector);
-        self.moveTile(tile, pos);
+        var positions = self.findFarthestPosition(cell, vector);
+        var next      = self.grid.cellContent(positions.next);
+
+        // Only one merger per row traversal?
+        if (next && next.value === tile.value && !next.mergedFrom) {
+          var merged = new Tile(positions.next, tile.value * 2);
+          merged.mergedFrom = [tile, next];
+
+          self.grid.insertTile(merged);
+          self.grid.removeTile(tile);
+
+          // Converge the two tiles' positions
+          tile.updatePosition(positions.next);
+        } else {
+          self.moveTile(tile, positions.farthest);
+        }
+
+        moved = true;
       }
     });
   });
 
-  this.addRandomTile();
-  this.actuate();
+  if (moved) {
+    this.addRandomTile();
+    this.actuate();
+  }
 };
 
 // Get the vector representing the chosen direction
@@ -124,10 +143,12 @@
   do {
     previous = cell;
     cell     = { x: previous.x + vector.x, y: previous.y + vector.y };
-  } while (cell.x >= 0 && cell.x < this.size &&
-           cell.y >= 0 && cell.y < this.size &&
+  } while (this.grid.withinBounds(cell) &&
            this.grid.cellAvailable(cell));
 
-  return previous;
+  return {
+    farthest: previous,
+    next: cell // Used to check if a merge is required
+  };
 };
 

file:a/js/grid.js -> file:b/js/grid.js
--- a/js/grid.js
+++ b/js/grid.js
@@ -62,7 +62,11 @@
 };
 
 Grid.prototype.cellContent = function (cell) {
-  return this.cells[cell.x][cell.y];
+  if (this.withinBounds(cell)) {
+    return this.cells[cell.x][cell.y];
+  } else {
+    return null;
+  }
 };
 
 // Inserts a tile at its position
@@ -70,3 +74,12 @@
   this.cells[tile.x][tile.y] = tile;
 };
 
+Grid.prototype.removeTile = function (tile) {
+  this.cells[tile.x][tile.y] = null;
+};
+
+Grid.prototype.withinBounds = function (position) {
+  return position.x >= 0 && position.x < this.size &&
+         position.y >= 0 && position.y < this.size;
+};
+

--- a/js/html_actuator.js
+++ b/js/html_actuator.js
@@ -36,16 +36,19 @@
 
   this.tileContainer.appendChild(element);
 
-
   if (tile.previousPosition) {
     window.requestAnimationFrame(function () {
       element.classList.remove(element.classList[2]);
       element.classList.add(self.positionClass({ x: tile.x, y: tile.y }));
     });
+  } else if (tile.mergedFrom) {
+    element.classList.add("tile-merged");
+    tile.mergedFrom.forEach(function (merged) {
+      self.addTile(merged);
+    });
   } else {
     element.classList.add("tile-new");
   }
-
 };
 
 HTMLActuator.prototype.normalizePosition = function (position) {

file:a/js/tile.js -> file:b/js/tile.js
--- a/js/tile.js
+++ b/js/tile.js
@@ -4,9 +4,15 @@
   this.value            = value || 2;
 
   this.previousPosition = null;
+  this.mergedFrom       = null; // Tracks tiles that merged together
 }
 
 Tile.prototype.savePosition = function () {
   this.previousPosition = { x: this.x, y: this.y };
 };
 
+Tile.prototype.updatePosition = function (position) {
+  this.x = position.x;
+  this.y = position.y;
+};
+

--- a/style/main.css
+++ b/style/main.css
@@ -89,6 +89,7 @@
   line-height: 116.25px;
   font-size: 55px;
   font-weight: bold;
+  z-index: 10;
   -webkit-transition: 100ms ease-in-out;
   -moz-transition: 100ms ease-in-out;
   -webkit-transition-property: top, left;
@@ -100,109 +101,109 @@
   .tile.tile-position-1-2 {
     position: absolute;
     left: 0px;
-    top: 121.25px; }
+    top: 121px; }
   .tile.tile-position-1-3 {
     position: absolute;
     left: 0px;
-    top: 242.5px; }
+    top: 243px; }
   .tile.tile-position-1-4 {
     position: absolute;
     left: 0px;
-    top: 363.75px; }
+    top: 364px; }
   .tile.tile-position-2-1 {
     position: absolute;
-    left: 121.25px;
+    left: 121px;
     top: 0px; }
   .tile.tile-position-2-2 {
     position: absolute;
-    left: 121.25px;
-    top: 121.25px; }
+    left: 121px;
+    top: 121px; }
   .tile.tile-position-2-3 {
     position: absolute;
-    left: 121.25px;
-    top: 242.5px; }
+    left: 121px;
+    top: 243px; }
   .tile.tile-position-2-4 {
     position: absolute;
-    left: 121.25px;
-    top: 363.75px; }
+    left: 121px;
+    top: 364px; }
   .tile.tile-position-3-1 {
     position: absolute;
-    left: 242.5px;
+    left: 243px;
     top: 0px; }
   .tile.tile-position-3-2 {
     position: absolute;
-    left: 242.5px;
-    top: 121.25px; }
+    left: 243px;
+    top: 121px; }
   .tile.tile-position-3-3 {
     position: absolute;
-    left: 242.5px;
-    top: 242.5px; }
+    left: 243px;
+    top: 243px; }
   .tile.tile-position-3-4 {
     position: absolute;
-    left: 242.5px;
-    top: 363.75px; }
+    left: 243px;
+    top: 364px; }
   .tile.tile-position-4-1 {
     position: absolute;
-    left: 363.75px;
+    left: 364px;
     top: 0px; }
   .tile.tile-position-4-2 {
     position: absolute;
-    left: 363.75px;
-    top: 121.25px; }
+    left: 364px;
+    top: 121px; }
   .tile.tile-position-4-3 {
     position: absolute;
-    left: 363.75px;
-    top: 242.5px; }
+    left: 364px;
+    top: 243px; }
   .tile.tile-position-4-4 {
     position: absolute;
-    left: 363.75px;
-    top: 363.75px; }
+    left: 364px;
+    top: 364px; }
   .tile.tile-2 {
     background: #eee4da;
-    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0), inset 0 0 0 1px rgba(255, 255, 255, 0); }
+    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0); }
   .tile.tile-4 {
     background: #ede0c8;
-    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0), inset 0 0 0 1px rgba(255, 255, 255, 0); }
+    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0); }
   .tile.tile-8 {
     color: #f9f6f2;
     background: #f2b179;
-    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0), inset 0 0 0 1px rgba(255, 255, 255, 0); }
+    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0); }
   .tile.tile-16 {
     color: #f9f6f2;
     background: #f59563;
-    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0), inset 0 0 0 1px rgba(255, 255, 255, 0); }
+    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0); }
   .tile.tile-32 {
     color: #f9f6f2;
     background: #f67c5f;
-    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.07937), inset 0 0 0 1px rgba(255, 255, 255, 0.04762); }
+    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.07937); }
   .tile.tile-64 {
     color: #f9f6f2;
     background: #f65e3b;
-    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.15873), inset 0 0 0 1px rgba(255, 255, 255, 0.09524); }
+    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.15873); }
   .tile.tile-128 {
     color: #f9f6f2;
     background: #edcf72;
-    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.2381), inset 0 0 0 1px rgba(255, 255, 255, 0.14286);
+    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.2381);
     font-size: 45px; }
   .tile.tile-256 {
     color: #f9f6f2;
     background: #edcc61;
-    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.31746), inset 0 0 0 1px rgba(255, 255, 255, 0.19048);
+    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.31746);
     font-size: 45px; }
   .tile.tile-512 {
     color: #f9f6f2;
     background: #edc850;
-    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.39683), inset 0 0 0 1px rgba(255, 255, 255, 0.2381);
+    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.39683);
     font-size: 45px; }
   .tile.tile-1024 {
     color: #f9f6f2;
     background: #edc53f;
-    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.47619), inset 0 0 0 1px rgba(255, 255, 255, 0.28571);
+    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.47619);
     font-size: 35px; }
   .tile.tile-2048 {
     color: #f9f6f2;
     background: #edc22e;
-    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.55556), inset 0 0 0 1px rgba(255, 255, 255, 0.33333);
+    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.55556);
     font-size: 35px; }
 
 @-webkit-keyframes appear {
@@ -238,6 +239,40 @@
   -webkit-animation-fill-mode: both;
   -moz-animation-fill-mode: both; }
 
+@-webkit-keyframes pop {
+  0% {
+    -webkit-transform: scale(0.5);
+    opacity: 0; }
+
+  100% {
+    -webkit-transform: scale(1);
+    opacity: 1; } }
+
+@-moz-keyframes pop {
+  0% {
+    -webkit-transform: scale(0.5);
+    opacity: 0; }
+
+  100% {
+    -webkit-transform: scale(1);
+    opacity: 1; } }
+
+@keyframes pop {
+  0% {
+    -webkit-transform: scale(0.5);
+    opacity: 0; }
+
+  100% {
+    -webkit-transform: scale(1);
+    opacity: 1; } }
+
+.tile-merged {
+  z-index: 20;
+  -webkit-animation: pop 200ms ease 100ms;
+  -moz-animation: pop 200ms ease 100ms;
+  -webkit-animation-fill-mode: both;
+  -moz-animation-fill-mode: both; }
+
 .game-intro {
   margin-bottom: 0; }
 

--- a/style/main.scss
+++ b/style/main.scss
@@ -133,6 +133,7 @@
   line-height: $tile-size + 10px;
   font-size: 55px;
   font-weight: bold;
+  z-index: 10;
 
   @include transition($transition-speed ease-in-out);
   @include transition-property(top, left);
@@ -142,8 +143,8 @@
     @for $y from 1 through $grid-row-cells {
       &.tile-position-#{$x}-#{$y} {
         position: absolute;
-        left: ($tile-size + $grid-spacing) * ($x - 1);
-        top: ($tile-size + $grid-spacing) * ($y - 1);
+        left: round(($tile-size + $grid-spacing) * ($x - 1));
+        top: round(($tile-size + $grid-spacing) * ($y - 1));
       }
     }
   }
@@ -192,8 +193,8 @@
 
       // Add glow
       $glow-opacity: max($exponent - 4, 0) / ($limit - 4);
-      box-shadow: 0 0 30px 10px rgba($tile-gold-glow-color, $glow-opacity / 1.8),
-                  inset 0 0 0 1px rgba(white, $glow-opacity / 3);
+      box-shadow: 0 0 30px 10px rgba($tile-gold-glow-color, $glow-opacity / 1.8); //,
+                  // inset 0 0 0 1px rgba(white, $glow-opacity / 3);
 
       // Adjust font size for bigger numbers
       @if $power >= 100 and $power < 1000 {
@@ -224,6 +225,24 @@
   @include animation-fill-mode(both);
 }
 
+@include keyframes(pop) {
+  0% {
+    -webkit-transform: scale(.5);
+    opacity: 0;
+  }
+
+  100% {
+    -webkit-transform: scale(1);
+    opacity: 1;
+  }
+}
+
+.tile-merged {
+  z-index: 20;
+  @include animation(pop 200ms ease $transition-speed);
+  @include animation-fill-mode(both);
+}
+
 .game-intro {
   margin-bottom: 0;
 }

comments