UNPKG

@spearwolf/twopoint5d

Version:

Create 2.5D realtime graphics and pixelart with WebGL and three.js

64 lines 1.89 kB
import { Object3D } from 'three/webgpu'; export class Map2DTileRenderer { #tiles; #dataSerial; #updateDataSerial; constructor(tileFactory) { this.tileFactory = tileFactory; this.#tiles = new Map(); this.#dataSerial = 0; this.#updateDataSerial = -1; this.node = new Object3D(); this.node.name = 'twopoint5d.Map2DTileRenderer'; this.tileFactory.addToNode(this.node); } beginUpdatingTiles(position) { this.node.position.copy(position); } addTile(tileCoords) { const tile = this.tileFactory.createTile(tileCoords); if (tile == null) return; this.#tiles.set(tileCoords.id, tile); ++this.#dataSerial; } reuseTile(tileCoords) { const tile = this.#tiles.get(tileCoords.id); if (tile) { this.tileFactory.updateTile(tile, tileCoords); ++this.#dataSerial; } else { this.addTile(tileCoords); } } removeTile(tileCoords) { const tile = this.#tiles.get(tileCoords.id); if (tile) { this.#tiles.delete(tileCoords.id); this.tileFactory.destroyTile(tile); ++this.#dataSerial; } } clearTiles() { for (const tile of this.#tiles.values()) { this.tileFactory.destroyTile(tile); } this.#tiles.clear(); ++this.#dataSerial; } endUpdatingTiles() { if (this.#updateDataSerial >= this.#dataSerial) return; this.#updateDataSerial = this.#dataSerial; this.tileFactory.update(); } dispose() { this.tileFactory.removeFromNode(this.node); this.tileFactory = null; this.#tiles.clear(); this.#dataSerial = 0; this.#updateDataSerial = -1; } } //# sourceMappingURL=Map2DTileRenderer.js.map