-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile-sheet.js
43 lines (41 loc) · 1.25 KB
/
tile-sheet.js
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
41
42
43
export class TileSheet {
constructor(settings, canvas, floorData, image) {
this.canvas = canvas;
this.ctx = canvas.getContext("2d");
// this.map = map;
this.floorData = floorData
this.settings = settings;
this.image = image
this.drawTiles = this.drawTiles.bind(this);
this.renderFloor = this.renderFloor.bind(this);
}
drawTiles() {
this.renderFloor(this.floorData)
}
renderFloor(layer) {
for (let i = 0; i < layer.length; i++) {
const currValue = layer[i] - 1;
let source_x =
(currValue % this.settings.tileSetColumns) * this.settings.tileWidth;
let source_y =
Math.floor(currValue / this.settings.tileSetColumns) *
this.settings.tileHeight;
let destination_x =
(i % this.settings.mapWidth) * this.settings.tileWidth;
let destination_y =
Math.floor(i / this.settings.mapWidth) * this.settings.tileHeight;
// this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
this.ctx.drawImage(
this.image,
source_x,
source_y,
this.settings.tileWidth,
this.settings.tileHeight,
destination_x,
destination_y,
this.settings.tileWidth,
this.settings.tileHeight
);
}
}
}