From c48b92689dd232656b669e13c982e1d74bf753bf Mon Sep 17 00:00:00 2001 From: Gabriele Cirulli Date: Sat, 22 Mar 2014 16:24:11 +0100 Subject: [PATCH] refactor grid, gamemanager, tile --- js/game_manager.js | 55 +++++++++++++++++++++++----------------------- js/grid.js | 39 ++++++++++++++++++-------------- js/tile.js | 18 +++++++-------- 3 files changed, 59 insertions(+), 53 deletions(-) diff --git a/js/game_manager.js b/js/game_manager.js index da55aba17a..49b903ae66 100644 --- a/js/game_manager.js +++ b/js/game_manager.js @@ -36,23 +36,24 @@ GameManager.prototype.isGameTerminated = function () { // Set up the game GameManager.prototype.setup = function () { - var previousGameState = this.storageManager.getGameState(); - - if (previousGameState) { - this.grid = new Grid(previousGameState.grid.size, previousGameState.grid.cells); - this.score = previousGameState.score; - this.over = previousGameState.over; - this.won = previousGameState.won; - this.keepPlaying = previousGameState.keepPlaying; + var previousState = this.storageManager.getGameState(); + + if (previousState) { + this.grid = new Grid(previousState.grid.size, + previousState.grid.cells); // Reload grid + this.score = previousState.score; + this.over = previousState.over; + this.won = previousState.won; + this.keepPlaying = previousState.keepPlaying; } else { - this.grid = new Grid(this.size); - this.score = 0; - this.over = false; - this.won = false; - this.keepPlaying = false; - - // Add the initial tiles - this.addStartTiles(); + this.grid = new Grid(this.size); + this.score = 0; + this.over = false; + this.won = false; + this.keepPlaying = false; + + // Add the initial tiles + this.addStartTiles(); } // Update the actuator @@ -82,7 +83,7 @@ GameManager.prototype.actuate = function () { this.storageManager.setBestScore(this.score); } - this.storageManager.setGameState(this.serializeGameState()); + this.storageManager.setGameState(this.serialize()); this.actuator.actuate(this.grid, { score: this.score, @@ -94,16 +95,16 @@ GameManager.prototype.actuate = function () { }; -GameManager.prototype.serializeGameState = function () { - return { - size: this.size, - grid: this.grid.gridState(), - score: this.score, - over: this.over, - won: this.won, - keepPlaying: this.keepPlaying - }; -} +GameManager.prototype.serialize = function () { + return { + size: this.size, + grid: this.grid.serialize(), + score: this.score, + over: this.over, + won: this.won, + keepPlaying: this.keepPlaying + }; +}; // Save all tile positions and remove merger info GameManager.prototype.prepareTiles = function () { diff --git a/js/grid.js b/js/grid.js index a2215a775b..29f0821e50 100644 --- a/js/grid.js +++ b/js/grid.js @@ -1,11 +1,12 @@ -function Grid(size, previousCellState) { +function Grid(size, previousState) { this.size = size; - this.cells = previousCellState ? this.buildFromPreviousState(previousCellState) : this.buildNew(); + this.cells = previousState ? this.fromState(previousState) : this.empty(); } // Build a grid of the specified size -Grid.prototype.buildNew = function () { +Grid.prototype.empty = function () { var cells = []; + for (var x = 0; x < this.size; x++) { var row = cells[x] = []; @@ -13,20 +14,23 @@ Grid.prototype.buildNew = function () { row.push(null); } } + return cells; }; -Grid.prototype.buildFromPreviousState = function (state) { - var cells = []; - for (var x = 0; x < this.size; x++) { - var row = cells[x] = []; +Grid.prototype.fromState = function (state) { + var cells = []; + + for (var x = 0; x < this.size; x++) { + var row = cells[x] = []; - for (var y = 0; y < this.size; y++) { - var tileState = state[x][y]; - row.push(tileState ? new Tile(tileState.position, tileState.value) : null); - } + for (var y = 0; y < this.size; y++) { + var tile = state[x][y]; + row.push(tile ? new Tile(tile.position, tile.value) : null); } - return cells; + } + + return cells; }; // Find the first available random position @@ -95,18 +99,19 @@ Grid.prototype.withinBounds = function (position) { position.y >= 0 && position.y < this.size; }; -Grid.prototype.gridState = function () { +Grid.prototype.serialize = function () { var cellState = []; + for (var x = 0; x < this.size; x++) { var row = cellState[x] = []; for (var y = 0; y < this.size; y++) { - row.push(this.cells[x][y] ? this.cells[x][y].tileState() : null); + row.push(this.cells[x][y] ? this.cells[x][y].serialize() : null); } } return { - size: this.size, - cells: cellState - } + size: this.size, + cells: cellState + }; }; diff --git a/js/tile.js b/js/tile.js index f12407347b..92a670a5a6 100644 --- a/js/tile.js +++ b/js/tile.js @@ -16,12 +16,12 @@ Tile.prototype.updatePosition = function (position) { this.y = position.y; }; -Tile.prototype.tileState = function () { - return { - position: { - x: this.x, - y: this.y - }, - value: this.value - }; -} \ No newline at end of file +Tile.prototype.serialize = function () { + return { + position: { + x: this.x, + y: this.y + }, + value: this.value + }; +};