1
0
mirror of https://github.com/danog/2048.git synced 2024-12-02 17:27:57 +01:00
2048/js/game_manager.js

273 lines
7.4 KiB
JavaScript
Raw Normal View History

function GameManager(size, InputManager, Actuator, StorageManager) {
2014-03-22 17:06:37 +01:00
this.size = size; // Size of the grid
this.inputManager = new InputManager;
this.storageManager = new StorageManager;
2014-03-22 17:06:37 +01:00
this.actuator = new Actuator;
2014-03-22 17:06:37 +01:00
this.startTiles = 2;
2014-03-09 23:03:13 +01:00
2014-03-09 14:32:30 +01:00
this.inputManager.on("move", this.move.bind(this));
2014-03-10 11:31:10 +01:00
this.inputManager.on("restart", this.restart.bind(this));
this.inputManager.on("keepPlaying", this.keepPlaying.bind(this));
this.setup();
}
2014-03-10 11:31:10 +01:00
// Restart the game
GameManager.prototype.restart = function () {
this.storageManager.clearGameState();
2014-03-23 19:51:29 +01:00
this.actuator.continueGame(); // Clear the game won/lost message
2014-03-10 11:31:10 +01:00
this.setup();
};
2014-03-22 18:45:48 +01:00
// Keep playing after winning (allows going over 2048)
GameManager.prototype.keepPlaying = function () {
this.keepPlaying = true;
2014-03-23 19:51:29 +01:00
this.actuator.continueGame(); // Clear the game won/lost message
};
2014-03-22 18:41:47 +01:00
// Return true if the game is lost, or has won and the user hasn't kept playing
GameManager.prototype.isGameTerminated = function () {
2014-04-16 10:54:51 +02:00
return this.over || (this.won && !this.keepPlaying);
2014-03-14 13:27:57 +01:00
};
// Set up the game
GameManager.prototype.setup = function () {
2014-03-22 16:24:11 +01:00
var previousState = this.storageManager.getGameState();
2014-03-22 18:41:47 +01:00
// Reload the game from a previous game if present
2014-03-22 16:24:11 +01:00
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 {
2014-03-22 16:24:11 +01:00
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
2014-03-08 13:10:54 +01:00
this.actuate();
};
// Set up the initial tiles to start the game with
GameManager.prototype.addStartTiles = function () {
for (var i = 0; i < this.startTiles; i++) {
2014-03-08 13:10:54 +01:00
this.addRandomTile();
}
};
// Adds a tile in a random position
2014-03-08 13:10:54 +01:00
GameManager.prototype.addRandomTile = function () {
2014-03-09 14:32:30 +01:00
if (this.grid.cellsAvailable()) {
var value = Math.random() < 0.9 ? 2 : 4;
var tile = new Tile(this.grid.randomAvailableCell(), value);
this.grid.insertTile(tile);
}
};
// Sends the updated grid to the actuator
2014-03-08 13:10:54 +01:00
GameManager.prototype.actuate = function () {
if (this.storageManager.getBestScore() < this.score) {
this.storageManager.setBestScore(this.score);
}
2014-03-22 18:33:24 +01:00
// Clear the state when the game is over (game over only, not win)
if (this.over) {
this.storageManager.clearGameState();
} else {
this.storageManager.setGameState(this.serialize());
}
2014-03-09 23:03:13 +01:00
this.actuator.actuate(this.grid, {
score: this.score,
over: this.over,
won: this.won,
bestScore: this.storageManager.getBestScore(),
terminated: this.isGameTerminated()
2014-03-09 23:03:13 +01:00
});
};
2014-03-22 18:41:47 +01:00
// Represent the current game as an object
2014-03-22 16:24:11 +01:00
GameManager.prototype.serialize = function () {
return {
grid: this.grid.serialize(),
score: this.score,
over: this.over,
won: this.won,
keepPlaying: this.keepPlaying
};
};
2014-03-09 20:03:04 +01:00
// Save all tile positions and remove merger info
GameManager.prototype.prepareTiles = function () {
2014-03-09 14:32:30 +01:00
this.grid.eachCell(function (x, y, tile) {
if (tile) {
2014-03-09 20:03:04 +01:00
tile.mergedFrom = null;
2014-03-09 14:32:30 +01:00
tile.savePosition();
}
});
};
// Move a tile and its representation
GameManager.prototype.moveTile = function (tile, cell) {
this.grid.cells[tile.x][tile.y] = null;
this.grid.cells[cell.x][cell.y] = tile;
2014-03-09 20:03:04 +01:00
tile.updatePosition(cell);
2014-03-09 14:32:30 +01:00
};
// Move tiles on the grid in the specified direction
GameManager.prototype.move = function (direction) {
2014-03-22 18:41:47 +01:00
// 0: up, 1: right, 2: down, 3: left
2014-03-09 14:32:30 +01:00
var self = this;
if (this.isGameTerminated()) return; // Don't do anything if the game's over
2014-03-09 23:03:13 +01:00
2014-03-09 14:32:30 +01:00
var cell, tile;
var vector = this.getVector(direction);
var traversals = this.buildTraversals(vector);
2014-03-09 20:03:04 +01:00
var moved = false;
2014-03-09 14:32:30 +01:00
2014-03-09 20:03:04 +01:00
// Save the current tile positions and remove merger information
this.prepareTiles();
2014-03-09 14:32:30 +01:00
// Traverse the grid in the right direction and move tiles
traversals.x.forEach(function (x) {
traversals.y.forEach(function (y) {
cell = { x: x, y: y };
tile = self.grid.cellContent(cell);
if (tile) {
2014-03-09 20:03:04 +01:00
var positions = self.findFarthestPosition(cell, vector);
var next = self.grid.cellContent(positions.next);
// Only one merger per row traversal?
if (next && next.value === tile.value && !next.mergedFrom) {
var merged = new Tile(positions.next, tile.value * 2);
merged.mergedFrom = [tile, next];
self.grid.insertTile(merged);
self.grid.removeTile(tile);
// Converge the two tiles' positions
tile.updatePosition(positions.next);
2014-03-09 23:03:13 +01:00
// Update the score
self.score += merged.value;
2014-03-09 23:24:17 +01:00
2014-03-09 23:43:56 +01:00
// The mighty 2048 tile
if (merged.value === 2048) self.won = true;
2014-03-09 20:03:04 +01:00
} else {
self.moveTile(tile, positions.farthest);
}
2014-03-09 23:24:17 +01:00
if (!self.positionsEqual(cell, tile)) {
moved = true; // The tile moved from its original cell!
}
2014-03-09 14:32:30 +01:00
}
});
});
2014-03-09 20:03:04 +01:00
if (moved) {
this.addRandomTile();
2014-03-09 23:03:13 +01:00
if (!this.movesAvailable()) {
this.over = true; // Game over!
}
2014-03-09 20:03:04 +01:00
this.actuate();
}
};
2014-03-09 14:32:30 +01:00
// Get the vector representing the chosen direction
GameManager.prototype.getVector = function (direction) {
// Vectors representing tile movement
var map = {
2014-03-22 18:41:47 +01:00
0: { x: 0, y: -1 }, // Up
1: { x: 1, y: 0 }, // Right
2: { x: 0, y: 1 }, // Down
3: { x: -1, y: 0 } // Left
2014-03-09 14:32:30 +01:00
};
return map[direction];
};
// Build a list of positions to traverse in the right order
GameManager.prototype.buildTraversals = function (vector) {
var traversals = { x: [], y: [] };
for (var pos = 0; pos < this.size; pos++) {
traversals.x.push(pos);
traversals.y.push(pos);
}
// Always traverse from the farthest cell in the chosen direction
if (vector.x === 1) traversals.x = traversals.x.reverse();
if (vector.y === 1) traversals.y = traversals.y.reverse();
return traversals;
};
GameManager.prototype.findFarthestPosition = function (cell, vector) {
var previous;
// Progress towards the vector direction until an obstacle is found
do {
previous = cell;
cell = { x: previous.x + vector.x, y: previous.y + vector.y };
2014-03-09 20:03:04 +01:00
} while (this.grid.withinBounds(cell) &&
2014-03-09 14:32:30 +01:00
this.grid.cellAvailable(cell));
2014-03-09 20:03:04 +01:00
return {
farthest: previous,
next: cell // Used to check if a merge is required
};
2014-03-09 14:32:30 +01:00
};
2014-03-09 23:03:13 +01:00
GameManager.prototype.movesAvailable = function () {
return this.grid.cellsAvailable() || this.tileMatchesAvailable();
};
// Check for available matches between tiles (more expensive check)
GameManager.prototype.tileMatchesAvailable = function () {
var self = this;
var tile;
for (var x = 0; x < this.size; x++) {
for (var y = 0; y < this.size; y++) {
tile = this.grid.cellContent({ x: x, y: y });
if (tile) {
for (var direction = 0; direction < 4; direction++) {
var vector = self.getVector(direction);
var cell = { x: x + vector.x, y: y + vector.y };
var other = self.grid.cellContent(cell);
if (other && other.value === tile.value) {
return true; // These two tiles can be merged
}
}
}
}
}
return false;
};
2014-03-09 23:24:17 +01:00
GameManager.prototype.positionsEqual = function (first, second) {
return first.x === second.x && first.y === second.y;
};