refactor grid, gamemanager, tile

This commit is contained in:
Gabriele Cirulli
2014-03-22 16:24:11 +01:00
parent 4d294cf9b9
commit c48b92689d
3 changed files with 57 additions and 51 deletions
+12 -11
View File
@@ -36,14 +36,15 @@ GameManager.prototype.isGameTerminated = function () {
// Set up the game
GameManager.prototype.setup = function () {
var previousGameState = this.storageManager.getGameState();
var previousState = 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;
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;
@@ -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 () {
GameManager.prototype.serialize = function () {
return {
size: this.size,
grid: this.grid.gridState(),
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 () {
+14 -9
View File
@@ -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,19 +14,22 @@ Grid.prototype.buildNew = function () {
row.push(null);
}
}
return cells;
};
Grid.prototype.buildFromPreviousState = function (state) {
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);
var tile = state[x][y];
row.push(tile ? new Tile(tile.position, tile.value) : null);
}
}
return cells;
};
@@ -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
}
};
};
+2 -2
View File
@@ -16,7 +16,7 @@ Tile.prototype.updatePosition = function (position) {
this.y = position.y;
};
Tile.prototype.tileState = function () {
Tile.prototype.serialize = function () {
return {
position: {
x: this.x,
@@ -24,4 +24,4 @@ Tile.prototype.tileState = function () {
},
value: this.value
};
}
};