implement tile movement

This commit is contained in:
Gabriele Cirulli
2014-03-09 14:32:30 +01:00
parent b7476f1f62
commit b20e26e3bd
10 changed files with 286 additions and 36 deletions
+23 -9
View File
@@ -29,26 +29,40 @@ Grid.prototype.randomAvailableCell = function () {
Grid.prototype.availableCells = function () {
var cells = [];
for (var x = 0; x < this.size; x++) {
for (var y = 0; y < this.size; y++) {
var cell = { x: x, y: y };
if (this.cellAvailable(cell)) {
cells.push(cell);
}
this.eachCell(function (x, y, tile) {
if (!tile) {
cells.push({ x: x, y: y });
}
}
});
return cells;
};
// Call callback for every cell
Grid.prototype.eachCell = function (callback) {
for (var x = 0; x < this.size; x++) {
for (var y = 0; y < this.size; y++) {
callback(x, y, this.cells[x][y]);
}
}
};
// Check if there are any cells available
Grid.prototype.cellsAvailable = function () {
return !!this.availableCells().length;
};
// Check if the specified cell is taken
Grid.prototype.cellAvailable = function (cell) {
return !this.cellOccupied(cell);
};
Grid.prototype.cellOccupied = function (cell) {
return !!this.cells[cell.x][cell.y];
return !!this.cellContent(cell);
};
Grid.prototype.cellContent = function (cell) {
return this.cells[cell.x][cell.y];
};
// Inserts a tile at its position