1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import Rect from "./Rect.js";
- import assets from "./assets.js";
-
- export default class Texture {
- constructor(nestedArr) {
- this.nestedArr = nestedArr;
- this.can = document.createElement("canvas");
- this.ctx = this.can.getContext("2d");
- this.width = 0;
- this.height = 0;
-
- assets.tiles.whenReady(() => this.init());
- }
-
- findWidthHeight(arr) {
- arr.forEach(e => {
- if (e instanceof Array) {
- this.findWidthHeight(e);
- } else {
- let right = (e.x + 1) * assets.tiles.tileWidth;
- let bottom = (e.y + 1) * assets.tiles.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 {
- assets.tiles.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);
- }
- }
|