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
| // Loop over the cluster tiles and execute a function
function loopClusters(func) {
for (var i=0; i<clusters.length; i++) {
// { column, row, length, horizontal }
var cluster = clusters[i];
var coffset = 0;
var roffset = 0;
for (var j=0; j<cluster.length; j++) {
func(i, cluster.column+coffset, cluster.row+roffset, cluster);
if (cluster.horizontal) {
coffset++;
} else {
roffset++;
}
}
}
}
// Remove the clusters
function removeClusters() {
// Change the type of the tiles to -1, indicating a removed tile
loopClusters(function(index, column, row, cluster) { level.tiles[column][row].type = -1; });
// Calculate how much a tile should be shifted downwards
for (var i=0; i<level.columns; i++) {
var shift = 0;
for (var j=level.rows-1; j>=0; j--) {
// Loop from bottom to top
if (level.tiles[i][j].type == -1) {
// Tile is removed, increase shift
shift++;
level.tiles[i][j].shift = 0;
} else {
// Set the shift
level.tiles[i][j].shift = shift;
}
}
}
} |
Partager