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
+26 -25
View File
@@ -36,23 +36,24 @@ GameManager.prototype.isGameTerminated = function () {
// Set up the game // Set up the game
GameManager.prototype.setup = function () { GameManager.prototype.setup = function () {
var previousGameState = this.storageManager.getGameState(); var previousState = this.storageManager.getGameState();
if (previousGameState) { if (previousState) {
this.grid = new Grid(previousGameState.grid.size, previousGameState.grid.cells); this.grid = new Grid(previousState.grid.size,
this.score = previousGameState.score; previousState.grid.cells); // Reload grid
this.over = previousGameState.over; this.score = previousState.score;
this.won = previousGameState.won; this.over = previousState.over;
this.keepPlaying = previousGameState.keepPlaying; this.won = previousState.won;
this.keepPlaying = previousState.keepPlaying;
} else { } else {
this.grid = new Grid(this.size); this.grid = new Grid(this.size);
this.score = 0; this.score = 0;
this.over = false; this.over = false;
this.won = false; this.won = false;
this.keepPlaying = false; this.keepPlaying = false;
// Add the initial tiles // Add the initial tiles
this.addStartTiles(); this.addStartTiles();
} }
// Update the actuator // Update the actuator
@@ -82,7 +83,7 @@ GameManager.prototype.actuate = function () {
this.storageManager.setBestScore(this.score); this.storageManager.setBestScore(this.score);
} }
this.storageManager.setGameState(this.serializeGameState()); this.storageManager.setGameState(this.serialize());
this.actuator.actuate(this.grid, { this.actuator.actuate(this.grid, {
score: this.score, score: this.score,
@@ -94,16 +95,16 @@ GameManager.prototype.actuate = function () {
}; };
GameManager.prototype.serializeGameState = function () { GameManager.prototype.serialize = function () {
return { return {
size: this.size, size: this.size,
grid: this.grid.gridState(), grid: this.grid.serialize(),
score: this.score, score: this.score,
over: this.over, over: this.over,
won: this.won, won: this.won,
keepPlaying: this.keepPlaying keepPlaying: this.keepPlaying
}; };
} };
// Save all tile positions and remove merger info // Save all tile positions and remove merger info
GameManager.prototype.prepareTiles = function () { GameManager.prototype.prepareTiles = function () {
+22 -17
View File
@@ -1,11 +1,12 @@
function Grid(size, previousCellState) { function Grid(size, previousState) {
this.size = size; 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 // Build a grid of the specified size
Grid.prototype.buildNew = function () { Grid.prototype.empty = function () {
var cells = []; var cells = [];
for (var x = 0; x < this.size; x++) { for (var x = 0; x < this.size; x++) {
var row = cells[x] = []; var row = cells[x] = [];
@@ -13,20 +14,23 @@ Grid.prototype.buildNew = function () {
row.push(null); row.push(null);
} }
} }
return cells; return cells;
}; };
Grid.prototype.buildFromPreviousState = function (state) { Grid.prototype.fromState = function (state) {
var cells = []; var cells = [];
for (var x = 0; x < this.size; x++) {
var row = cells[x] = [];
for (var y = 0; y < this.size; y++) { for (var x = 0; x < this.size; x++) {
var tileState = state[x][y]; var row = cells[x] = [];
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 // Find the first available random position
@@ -95,18 +99,19 @@ Grid.prototype.withinBounds = function (position) {
position.y >= 0 && position.y < this.size; position.y >= 0 && position.y < this.size;
}; };
Grid.prototype.gridState = function () { Grid.prototype.serialize = function () {
var cellState = []; var cellState = [];
for (var x = 0; x < this.size; x++) { for (var x = 0; x < this.size; x++) {
var row = cellState[x] = []; var row = cellState[x] = [];
for (var y = 0; y < this.size; y++) { 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 { return {
size: this.size, size: this.size,
cells: cellState cells: cellState
} };
}; };
+9 -9
View File
@@ -16,12 +16,12 @@ Tile.prototype.updatePosition = function (position) {
this.y = position.y; this.y = position.y;
}; };
Tile.prototype.tileState = function () { Tile.prototype.serialize = function () {
return { return {
position: { position: {
x: this.x, x: this.x,
y: this.y y: this.y
}, },
value: this.value value: this.value
}; };
} };