properly ignore meta keys
[2048.git] / js / game_manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function GameManager(size, actuator) {
  this.size       = size; // Grid size
  this.actuator   = actuator;
 
  this.startTiles = 2;
  this.grid       = [];
 
  this.setup();
}
 
// Set up the game
GameManager.prototype.setup = function () {
  this.buildGrid();
  this.addStartTiles();
 
  // Update the actuator
  this.update();
};
 
// Build a grid of the specified size
GameManager.prototype.buildGrid = function () {
  for (var y = 0; y < this.size; y++) {
    this.grid[y] = [];
    for (var x = 0; x < this.size; x++) {
      this.grid[y].push(null);
    }
  }
};
 
// Set up the initial tiles to start the game with
GameManager.prototype.addStartTiles = function () {
  for (var i = 0; i < this.startTiles; i++) {
    this.addTile();
  }
};
 
// Adds a tile in a random position
GameManager.prototype.addTile = function () {
  this.insertTile(new Tile(this.randomCell()));
};
 
// Find the first available random position
GameManager.prototype.randomCell = function () {
  // TODO: build a map of available positions and choose from it
  var self = this;
 
  var position;
 
  function randomPosition() {
    return Math.floor(Math.random() * self.size);
  }
 
  do {
    position = {
      x: randomPosition(),
      y: randomPosition()
    };
  } while (this.cellOccupied(position));
 
  return position;
};
 
// Check if the specified cell is taken
GameManager.prototype.cellOccupied = function (cell) {
  return !!this.grid[cell.x][cell.y];
};
 
// Insert a tile at the specified position
GameManager.prototype.insertTile = function (tile) {
  this.grid[tile.x][tile.y] = tile;
};
 
// Sends the updated grid to the actuator
GameManager.prototype.update = function () {
  this.actuator.update(this.grid);
};
 
// Move the grid in the specified direction
GameManager.prototype.move = function (direction) {
  // 0: up, 1: right, 2:down, 3: left
 
  this.update();
};
 
comments