You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Structure.js 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import assets from './assets.js';
  2. import Rect from './Rect.js';
  3. import Vec2 from './Vec2.js';
  4. function findBounds(ctx, arr, bounds) {
  5. arr.forEach(e => {
  6. if (e instanceof Array) {
  7. drawArr(ctx, e);
  8. } else {
  9. let right = (e.x + 1) * assets.tiles.tileWidth;
  10. let bottom = (e.y + 1) * assets.tiles.tileHeight;
  11. if (bounds.size.x < right)
  12. bounds.size.x = right;
  13. if (bounds.size.y < bottom)
  14. bounds.size.y = bottom;
  15. }
  16. });
  17. }
  18. function drawArr(ctx, arr) {
  19. arr.forEach(e => {
  20. if (e instanceof Array) {
  21. drawArr(ctx, e);
  22. } else {
  23. assets.tiles.drawTile(ctx, e.tile, e.x, e.y);
  24. }
  25. });
  26. }
  27. export default class Structure {
  28. constructor(x, y, arr) {
  29. console.log(arr);
  30. this.can = document.createElement("canvas");
  31. this.bounds = new Rect(new Vec2(
  32. x * assets.tiles.tileWidth, y * assets.tiles.tileHeight));
  33. let ctx = this.can.getContext("2d");
  34. findBounds(ctx, arr, this.bounds);
  35. this.can.width = this.bounds.size.x;
  36. this.can.height = this.bounds.size.y;
  37. assets.tiles.whenReady(() => drawArr(ctx, arr));
  38. console.log(this.bounds.pos, this.bounds.size);
  39. }
  40. draw(ctx) {
  41. ctx.drawImage(
  42. this.can, this.bounds.pos.x, this.bounds.pos.y,
  43. this.bounds.size.x, this.bounds.size.y);
  44. }
  45. }