47 lines
1.0 KiB
JavaScript
47 lines
1.0 KiB
JavaScript
class GameMap {
|
|
constructor(tiles, spawn) {
|
|
this.width = tiles.length;
|
|
this.height = tiles[0].length;
|
|
this.tiles = tiles;
|
|
this.spawn = spawn;
|
|
|
|
this.init();
|
|
}
|
|
|
|
init() {
|
|
this.tiles.forEach(row => {
|
|
row.forEach(tile => {
|
|
if(tile.solid) {
|
|
tile.lookAtNeighbours(this.getSurroundingTiles(tile.x, tile.y));
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
draw(ctx, conf) {
|
|
this.tiles.forEach(col => {
|
|
col.forEach(tile => {
|
|
tile.draw(ctx, conf);
|
|
});
|
|
});
|
|
}
|
|
|
|
getTile(x, y) {
|
|
try {
|
|
return this.tiles[x][y];
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
getSurroundingTiles(x, y) {
|
|
const left = this.getTile(x - 1, y);
|
|
const right = this.getTile(x + 1, y);
|
|
const top = this.getTile(x, y - 1);
|
|
const bottom = this.getTile(x, y + 1);
|
|
|
|
return {
|
|
left, top, right, bottom
|
|
};
|
|
}
|
|
} |