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

45 lines
1.0 KiB
JavaScript
Raw Normal View History

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);
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();
};
// 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 () {
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);
};
// Sends the updated grid to the actuator
2014-03-08 13:10:54 +01:00
GameManager.prototype.actuate = function () {
this.actuator.actuate(this.grid);
};
// 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();
};