UNPKG

@spearwolf/twopoint5d

Version:

a library to create 2.5d realtime graphics and pixelart with three.js

128 lines 4.1 kB
import { Object3D, Vector2, Vector3 } from 'three'; import { Map2DTile } from './Map2DTile.js'; import { Map2DTileCoordsUtil } from './Map2DTileCoordsUtil.js'; export class Map2DLayer { #centerX; #centerY; #tileCoords; #needsUpdate; #resetTilesOnNextUpdate; get needsUpdate() { return this.#needsUpdate || this.visibilitor?.needsUpdate; } set needsUpdate(update) { this.#needsUpdate = update; if (this.visibilitor) { this.visibilitor.needsUpdate = update; } } get centerX() { return this.#centerX; } set centerX(x) { if (this.#centerX !== x) { this.#centerX = x; this.#needsUpdate = true; } } get centerY() { return this.#centerY; } set centerY(y) { if (this.#centerY !== y) { this.#centerY = y; this.#needsUpdate = true; } } get tileWidth() { return this.#tileCoords.tileWidth; } set tileWidth(width) { if (this.#tileCoords.tileWidth !== width) { this.#tileCoords.tileWidth = width; this.#needsUpdate = true; } } get tileHeight() { return this.#tileCoords.tileHeight; } set tileHeight(height) { if (this.#tileCoords.tileHeight !== height) { this.#tileCoords.tileHeight = height; this.#needsUpdate = true; } } get xOffset() { return this.#tileCoords.xOffset; } set xOffset(offset) { if (this.#tileCoords.xOffset !== offset) { this.#tileCoords.xOffset = offset; this.#needsUpdate = true; } } get yOffset() { return this.#tileCoords.yOffset; } set yOffset(offset) { if (this.#tileCoords.yOffset !== offset) { this.#tileCoords.yOffset = offset; this.#needsUpdate = true; } } constructor(tileWidth = 0, tileHeight = 0, xOffset = 0, yOffset = 0) { this.#centerX = 0; this.#centerY = 0; this.#needsUpdate = true; this.#resetTilesOnNextUpdate = false; this.tiles = []; this.renderers = new Set(); this.#tileCoords = new Map2DTileCoordsUtil(tileWidth, tileHeight, xOffset, yOffset); } addTileRenderer(renderer) { this.renderers.add(renderer); } removeTileRenderer(renderer) { this.renderers.delete(renderer); } update(node) { if (this.renderers.size === 0 || this.visibilitor == null) return; if (this.#resetTilesOnNextUpdate) { for (const tileRenderer of this.renderers) { tileRenderer.resetTiles(); } this.tiles.length = 0; this.needsUpdate = true; this.#resetTilesOnNextUpdate = false; } if (this.needsUpdate) { node.updateWorldMatrix(true, false); const visible = this.visibilitor.computeVisibleTiles(this.tiles, [this.centerX, this.centerY], this.#tileCoords, node); if (visible) { this.tiles = visible?.tiles; this.needsUpdate = false; const offset = visible.offset ?? new Vector2(); const translate = visible.translate ?? new Vector3(); for (const tileRenderer of this.renderers) { tileRenderer.beginUpdate(offset, translate); visible.removeTiles?.forEach((tile) => { tileRenderer.removeTile(tile); }); visible.createTiles?.forEach((tile) => { tileRenderer.addTile(tile); }); visible.reuseTiles?.forEach((tile) => { tileRenderer.reuseTile(tile); }); tileRenderer.endUpdate(); } } } } resetTiles() { this.#resetTilesOnNextUpdate = true; this.needsUpdate = true; } } //# sourceMappingURL=Map2DLayer.js.map