@spearwolf/twopoint5d
Version:
Create 2.5D realtime graphics and pixelart with WebGL and three.js
61 lines • 2.37 kB
JavaScript
export class Map2DTileCoordsUtil {
#left = (tileLeft) => tileLeft * this.tileWidth + this.xOffset;
#top = (tileTop) => tileTop * this.tileHeight + this.yOffset;
#tileLeft = (left) => {
return Math.floor((left - this.xOffset) / this.tileWidth);
};
#tileColumns = (tileLeft, width) => {
return Math.ceil(tileLeft + width / this.tileWidth) - tileLeft;
};
#tileTop = (top) => {
return Math.floor((top - this.yOffset) / this.tileHeight);
};
#tileRows = (tileTop, height) => {
return Math.ceil(tileTop + height / this.tileHeight) - tileTop;
};
constructor(tileWidth = 1, tileHeight = 1, xOffset = 0, yOffset = 0) {
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.xOffset = xOffset;
this.yOffset = yOffset;
}
copy(source) {
this.tileWidth = source.tileWidth;
this.tileHeight = source.tileHeight;
this.xOffset = source.xOffset;
this.yOffset = source.yOffset;
return this;
}
clone() {
return new Map2DTileCoordsUtil(this.tileWidth, this.tileHeight, this.xOffset, this.yOffset);
}
equals(other) {
return (this.tileWidth === other.tileWidth &&
this.tileHeight === other.tileHeight &&
this.xOffset === other.xOffset &&
this.yOffset === other.yOffset);
}
getTileCoords(left, top, width, height) {
const tileLeft = this.#tileLeft(left);
const w = width + left - this.#left(tileLeft);
const tileTop = this.#tileTop(top);
const h = height + top - this.#top(tileTop);
return [tileLeft, tileTop, this.#tileColumns(tileLeft, w), this.#tileRows(tileTop, h)];
}
computeTilesWithinCoords(left, top, width, height) {
const [tileLeft, tileTop, tileColumns, tileRows] = this.getTileCoords(left, top, width, height);
return {
tileTop,
tileLeft,
top: this.#top(tileTop) - this.yOffset,
left: this.#left(tileLeft) - this.xOffset,
height: tileRows * this.tileHeight,
width: tileColumns * this.tileWidth,
tileHeight: this.tileHeight,
tileWidth: this.tileWidth,
rows: tileRows,
columns: tileColumns,
};
}
}
//# sourceMappingURL=Map2DTileCoordsUtil.js.map