refactor grid, gamemanager, tile
This commit is contained in:
+26
-25
@@ -36,23 +36,24 @@ 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;
|
||||
this.over = false;
|
||||
this.won = false;
|
||||
this.keepPlaying = false;
|
||||
this.grid = new Grid(this.size);
|
||||
this.score = 0;
|
||||
this.over = false;
|
||||
this.won = false;
|
||||
this.keepPlaying = false;
|
||||
|
||||
// Add the initial tiles
|
||||
this.addStartTiles();
|
||||
// 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 () {
|
||||
|
||||
+22
-17
@@ -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 y = 0; y < this.size; y++) {
|
||||
var tileState = state[x][y];
|
||||
row.push(tileState ? new Tile(tileState.position, tileState.value) : null);
|
||||
}
|
||||
for (var x = 0; x < this.size; x++) {
|
||||
var row = cells[x] = [];
|
||||
|
||||
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
|
||||
};
|
||||
};
|
||||
|
||||
+9
-9
@@ -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
|
||||
};
|
||||
}
|
||||
Tile.prototype.serialize = function () {
|
||||
return {
|
||||
position: {
|
||||
x: this.x,
|
||||
y: this.y
|
||||
},
|
||||
value: this.value
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user