From 8e92590317489ad8451cccaed4f04af98cd46428 Mon Sep 17 00:00:00 2001 From: ksgy Date: Mon, 30 Mar 2015 00:34:22 +0100 Subject: [PATCH] Works on the first click if not touching any edge --- app/javascript/grid.js | 12 +++++++----- app/javascript/proto.js | 27 +++++++++++++++++++++++++++ index.html | 1 + 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 app/javascript/proto.js diff --git a/app/javascript/grid.js b/app/javascript/grid.js index 3f2c5e4..12c9fb8 100644 --- a/app/javascript/grid.js +++ b/app/javascript/grid.js @@ -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; @@ -58,7 +62,7 @@ class BlockGrid { } blockClicked (e, block) { - console.log('v13'); + console.log('v14'); this.neighbours = [block]; this.getNeighBour(block); @@ -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(); diff --git a/app/javascript/proto.js b/app/javascript/proto.js new file mode 100644 index 0000000..632b630 --- /dev/null +++ b/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; + } +} diff --git a/index.html b/index.html index f77ca00..571c07e 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,7 @@ +