2014-03-08 12:50:45 +01:00
|
|
|
function GameManager(size, actuator) {
|
|
|
|
this.size = size; // Grid size
|
|
|
|
this.actuator = actuator;
|
|
|
|
|
|
|
|
this.startTiles = 2;
|
2014-03-08 13:10:54 +01:00
|
|
|
this.grid = new Grid(this.size);
|
2014-03-08 12:50:45 +01:00
|
|
|
|
|
|
|
this.setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the game
|
|
|
|
GameManager.prototype.setup = function () {
|
|
|
|
this.addStartTiles();
|
|
|
|
|
|
|
|
// Update the actuator
|
2014-03-08 13:10:54 +01:00
|
|
|
this.actuate();
|
2014-03-08 12:50:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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();
|
2014-03-08 12:50:45 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Adds a tile in a random position
|
2014-03-08 13:10:54 +01:00
|
|
|
GameManager.prototype.addRandomTile = function () {
|
2014-03-08 13:32:37 +01:00
|
|
|
var value = Math.random() < 0.9 ? 2 : 4;
|
|
|
|
var tile = new Tile(this.grid.randomAvailableCell(), value);
|
|
|
|
|
2014-03-08 13:10:54 +01:00
|
|
|
this.grid.insertTile(tile);
|
2014-03-08 12:50:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Sends the updated grid to the actuator
|
2014-03-08 13:10:54 +01:00
|
|
|
GameManager.prototype.actuate = function () {
|
|
|
|
this.actuator.actuate(this.grid);
|
2014-03-08 12:50:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Move the grid in the specified direction
|
|
|
|
GameManager.prototype.move = function (direction) {
|
|
|
|
// 0: up, 1: right, 2:down, 3: left
|
|
|
|
|
2014-03-08 13:10:54 +01:00
|
|
|
this.actuate();
|
2014-03-08 12:50:45 +01:00
|
|
|
};
|