big commit
This commit is contained in:
parent
aff885cf17
commit
b506f5a219
22 changed files with 692 additions and 556 deletions
42
static/js/components/tairu/tilemap.js
Normal file
42
static/js/components/tairu/tilemap.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue