import Rect from "./Rect.js"; import assets from "./assets.js"; export default class Texture { constructor(sheet, nestedArr) { this.sheet = sheet; this.nestedArr = nestedArr; this.can = document.createElement("canvas"); this.ctx = this.can.getContext("2d"); this.width = 0; this.height = 0; this.sheet.whenReady(() => this.init()); } findWidthHeight(arr) { arr.forEach(e => { if (e instanceof Array) { this.findWidthHeight(e); } else { let right = (e.x + 1) * this.sheet.tileWidth; let bottom = (e.y + 1) * this.sheet.tileHeight; if (right > this.width) this.width = right; if (bottom > this.height) this.height = bottom; } }); } fillCanvas(arr) { arr.forEach(e => { if (e instanceof Array) { this.fillCanvas(e); } else { this.sheet.drawTile(this.ctx, e.name, e.x, e.y); } }); } init() { this.findWidthHeight(this.nestedArr); this.can.width = this.width; this.can.height = this.height; this.fillCanvas(this.nestedArr); } draw(ctx, x, y) { ctx.drawImage(this.can, x, y, this.width, this.height); } drawAt(ctx, vec) { this.draw(ctx, vec.pixelX, vec.pixelY); } }