refactor grid, gamemanager, tile
This commit is contained in:
+12
-11
@@ -36,14 +36,15 @@ 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;
|
||||||
@@ -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 () {
|
||||||
|
|||||||
+14
-9
@@ -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,19 +14,22 @@ 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++) {
|
for (var x = 0; x < this.size; x++) {
|
||||||
var row = cells[x] = [];
|
var row = cells[x] = [];
|
||||||
|
|
||||||
for (var y = 0; y < this.size; y++) {
|
for (var y = 0; y < this.size; y++) {
|
||||||
var tileState = state[x][y];
|
var tile = state[x][y];
|
||||||
row.push(tileState ? new Tile(tileState.position, tileState.value) : null);
|
row.push(tile ? new Tile(tile.position, tile.value) : null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cells;
|
return cells;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -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
|
||||||
}
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
+2
-2
@@ -16,7 +16,7 @@ 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,
|
||||||
@@ -24,4 +24,4 @@ Tile.prototype.tileState = function () {
|
|||||||
},
|
},
|
||||||
value: this.value
|
value: this.value
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user