treehouse/static/js/components/tairu/tilemap.js

43 lines
1 KiB
JavaScript
Raw Normal View History

2024-02-14 23:31:39 +01:00
export class Tilemap {
constructor(width, height) {
this.width = width;
this.height = height;
this.tiles = new Uint8Array(width * height);
this.default = 0;
}
tileIndex(x, y) {
return x + y * this.width;
}
inBounds(x, y) {
return x >= 0 && y >= 0 && x < this.width && y < this.height;
}
at(x, y) {
if (this.inBounds(x, y)) {
return this.tiles[this.tileIndex(x, y)];
} else {
return this.default;
}
}
setAt(x, y, tile) {
if (this.inBounds(x, y)) {
this.tiles[this.tileIndex(x, y)] = tile;
}
}
2024-02-18 23:37:31 +01:00
static parse(alphabet, lineArray) {
let tilemap = new Tilemap(lineArray[0].length, lineArray.length);
for (let y in lineArray) {
let line = lineArray[y];
for (let x = 0; x < line.length; ++x) {
let char = line.charAt(x);
tilemap.setAt(x, y, alphabet.indexOf(char));
}
}
return tilemap;
}
2024-02-14 23:31:39 +01:00
}