Skip to content

Commit

Permalink
Works on the first click if not touching any edge
Browse files Browse the repository at this point in the history
  • Loading branch information
ksgy committed Mar 29, 2015
1 parent c8cc4aa commit 8e92590
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
12 changes: 7 additions & 5 deletions app/javascript/grid.js
Expand Up @@ -46,6 +46,10 @@ class BlockGrid {
id = `block_${x}x${y}`,
blockEl = document.createElement('div');

if(block.colour != 'grey'){
block.checked = false;
}
// blockEl.innerHTML = block.checked;
blockEl.id = id;
blockEl.className = 'block';
blockEl.style.background = block.colour;
Expand All @@ -58,7 +62,7 @@ class BlockGrid {
}

blockClicked (e, block) {
console.log('v13');
console.log('v14');

this.neighbours = [block];
this.getNeighBour(block);
Expand Down Expand Up @@ -91,18 +95,16 @@ class BlockGrid {
}
}
};

console.log('-- all neighbours', this.neighbours);


}

applyGravity () {
// TODO set checked = false to non-grey blocks
this.neighbours.sort(function(a,b) {return (a.y > b.y) ? 1 : ((b.y > a.y) ? -1 : 0);} );
for (let i = this.neighbours.length -1; i >= 0; i--){
let x = this.neighbours[i].x;
let y = this.neighbours[i].y;
this.neighbours[i].colour = 'grey';
this.grid[x].move(y, MAX_Y - 1);
}

this.render();
Expand Down
27 changes: 27 additions & 0 deletions app/javascript/proto.js
@@ -0,0 +1,27 @@
// http://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another
Array.prototype.move = function(pos1, pos2) {
// local variables
var i, tmp;
// cast input parameters to integers
pos1 = parseInt(pos1, 10);
pos2 = parseInt(pos2, 10);
// if positions are different and inside array
if (pos1 !== pos2 && 0 <= pos1 && pos1 <= this.length && 0 <= pos2 && pos2 <= this.length) {
// save element from position 1
tmp = this[pos1];
// move element down and shift other elements up
if (pos1 < pos2) {
for (i = pos1; i < pos2; i++) {
this[i] = this[i + 1];
}
}
// move element up and shift other elements down
else {
for (i = pos1; i > pos2; i--) {
this[i] = this[i - 1];
}
}
// put element from position 1 to destination
this[pos2] = tmp;
}
}
1 change: 1 addition & 0 deletions index.html
Expand Up @@ -5,6 +5,7 @@
<script type="text/javascript" src="bower_components/underscore/underscore.js"></script>

<script src="dist/javascript/traceur-runtime.js"></script>
<script type="text/javascript" src="dist/javascript/proto.js"></script>
<script type="text/javascript" src="dist/javascript/grid.js"></script>
</head>
<body>
Expand Down

0 comments on commit 8e92590

Please sign in to comment.