* PlacedObject.js
*
* Lightweight value class for an object placed on the grid.
*/
export class PlacedObject {
constructor({ id, assetId, gx, gy, footprint, flipH = false, flipV = false }) {
this.id = id;
this.assetId = assetId;
this.gx = gx;
this.gy = gy;
this.footprint = footprint;
this.flipH = !!flipH;
this.flipV = !!flipV;
}
occupies(gx, gy) {
return gx >= this.gx && gx < this.gx + this.footprint.w
&& gy >= this.gy && gy < this.gy + this.footprint.d;
}
cells() {
const out = [];
for (let ix = 0; ix < this.footprint.w; ix++)
for (let iy = 0; iy < this.footprint.d; iy++) {
out.push({ gx: this.gx + ix, gy: this.gy + iy });
}
return out;
}
sortKey() {
return (this.gx + this.footprint.w - 1) + (this.gy + this.footprint.d - 1);
}
serialize() {
return {
id: this.id,
assetId: this.assetId,
gx: this.gx,
gy: this.gy,
footprint: { ...this.footprint },
flipH: this.flipH,
flipV: this.flipV,
};
}
}