12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import assets from './assets.js';
- import Rect from './Rect.js';
- import Vec2 from './Vec2.js';
-
- function findBounds(ctx, arr, bounds) {
- arr.forEach(e => {
- if (e instanceof Array) {
- drawArr(ctx, e);
- } else {
- let right = (e.x + 1) * assets.tiles.tileWidth;
- let bottom = (e.y + 1) * assets.tiles.tileHeight;
-
- if (bounds.size.x < right)
- bounds.size.x = right;
- if (bounds.size.y < bottom)
- bounds.size.y = bottom;
- }
- });
- }
-
- function drawArr(ctx, arr) {
- arr.forEach(e => {
- if (e instanceof Array) {
- drawArr(ctx, e);
- } else {
- assets.tiles.drawTile(ctx, e.tile, e.x, e.y);
- }
- });
- }
-
- export default class Structure {
- constructor(x, y, arr) {
- console.log(arr);
- this.can = document.createElement("canvas");
- this.bounds = new Rect(new Vec2(
- x * assets.tiles.tileWidth, y * assets.tiles.tileHeight));
-
- let ctx = this.can.getContext("2d");
- findBounds(ctx, arr, this.bounds);
- this.can.width = this.bounds.size.x;
- this.can.height = this.bounds.size.y;
-
- assets.tiles.whenReady(() => drawArr(ctx, arr));
- console.log(this.bounds.pos, this.bounds.size);
- }
-
- draw(ctx) {
- ctx.drawImage(
- this.can, this.bounds.pos.x, this.bounds.pos.y,
- this.bounds.size.x, this.bounds.size.y);
- }
- }
|