|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import assets from "./assets.js";
- import Rect from "./Rect.js";
- import Vec2 from "./Vec2.js";
- import Texture from "./Texture.js";
-
- function findBounds(arr, bounds) {
- arr.forEach(e => {
- let right = (e.x + 1) * assets.tiles.tileWidth;
- let bottom = (e.y + 1) * assets.tiles.tileHeight;
-
- if (right > bounds.size.x)
- bounds.size.pixelX = right;
- if (bottom > bounds.size.y)
- bounds.size.pixelY = bottom;
- });
- }
-
- function rightOrBelow(r1, r2) {
- // r2 right of r1
- if (
- (r1.top === r2.top && r1.bottom == r2.bottom) &&
- (r1.right + 1 >= r2.left && r1.left <= r2.left))
- return true;
-
- // r2 below r1
- if (
- (r1.right === r2.right && r1.left == r2.left) &&
- (r1.bottom + 1 >= r2.top && r1.top <= r2.top))
- return true;
-
- return false;
- }
-
- function continuous(r1, r2) {
- return rightOrBelow(r1, r2) || rightOrBelow(r2, r1);
- }
-
- function flattened(arr, newArr = []) {
- arr.forEach(e => {
- if (e instanceof Array)
- flattened(e, newArr);
- else
- newArr.push(e);
- });
- return newArr;
- }
-
- export default class Structure {
- constructor(attrs, nestedArr) {
- this.texture = new Texture(assets.tiles, nestedArr);
-
- this.attrs = {
- wall: false,
- platform: false,
- }
-
- for (let a of attrs)
- this.setAttr(a);
-
- this.nestedArr = nestedArr;
- this.pos = new Vec2();
- this.bounds = new Rect(this.pos);
- }
-
- init() {
- var arr = flattened(this.nestedArr);
-
- findBounds(arr, this.bounds);
- }
-
- draw(ctx) {
- this.texture.draw(ctx, this.pos.pixelX, this.pos.pixelY);
- }
-
- setAttr(attr) {
- if (this.attrs[attr] == null)
- throw new Error("Invalid attribute:", attr);
-
- this.attrs[attr] = true;
- return this;
- }
- }
|