diff --git a/prismarine-viewer/examples/playground.js b/prismarine-viewer/examples/playground.js index 39f05d119..61f6d30bd 100644 --- a/prismarine-viewer/examples/playground.js +++ b/prismarine-viewer/examples/playground.js @@ -101,8 +101,8 @@ async function main () { }) // await schem.paste(world, new Vec3(0, 60, 0)) - - const worldView = new WorldView(world, viewDistance, center) + + const worldView = new WorldDataEmitter(world, viewDistance, center) // Create three.js context, add to page const renderer = new THREE.WebGLRenderer() diff --git a/prismarine-viewer/viewer/lib/worldDataEmitter.ts b/prismarine-viewer/viewer/lib/worldDataEmitter.ts index cdc9aacec..fa4307d3f 100644 --- a/prismarine-viewer/viewer/lib/worldDataEmitter.ts +++ b/prismarine-viewer/viewer/lib/worldDataEmitter.ts @@ -1,9 +1,10 @@ import { chunkPos } from './simpleUtils' -import { spiral, ViewRect } from 'flying-squid/src/utils' +import { generateSpiralMatrix, ViewRect } from 'flying-squid/src/utils' import { Vec3 } from 'vec3' import { EventEmitter } from 'events' export type ChunkPosKey = string +type ChunkPos = { x: number, z: number } /** * Usually connects to mineflayer bot and emits world data (chunks, entities) @@ -76,10 +77,9 @@ export class WorldDataEmitter extends EventEmitter { async init (pos: Vec3) { const [botX, botZ] = chunkPos(pos) - const positions = spiral(this.viewDistance).map(([x, z]) => new Vec3((botX + x) * 16, 0, (botZ + z) * 16)) + const positions = generateSpiralMatrix(this.viewDistance).map(([x, z]) => new Vec3((botX + x) * 16, 0, (botZ + z) * 16)) this.lastPos.update(pos) - console.log(positions) await this._loadChunks(positions) } @@ -90,12 +90,12 @@ export class WorldDataEmitter extends EventEmitter { } } - async loadChunk (pos: Vec3) { + async loadChunk (pos: ChunkPos) { const [botX, botZ] = chunkPos(this.lastPos) const dx = Math.abs(botX - Math.floor(pos.x / 16)) const dz = Math.abs(botZ - Math.floor(pos.z / 16)) if (dx < this.viewDistance && dz < this.viewDistance) { - const column = await this.world.getColumnAt(pos) + const column = await this.world.getColumnAt(pos['y'] ? pos as Vec3 : new Vec3(pos.x, 0, pos.z)) if (column) { // todo optimize toJson data, make it clear why it is used const chunk = column.toJson() @@ -107,7 +107,7 @@ export class WorldDataEmitter extends EventEmitter { } } - unloadChunk (pos: Vec3) { + unloadChunk (pos: ChunkPos) { this.emitter.emit('unloadChunk', { x: pos.x, z: pos.z }) delete this.loadedChunks[`${pos.x},${pos.z}`] } @@ -127,11 +127,12 @@ export class WorldDataEmitter extends EventEmitter { chunksToUnload.push(p) } } + // todo @sa2urami console.log('unloading', chunksToUnload.length, 'total now', Object.keys(this.loadedChunks).length) for (const p of chunksToUnload) { this.unloadChunk(p) } - const positions = spiral(this.viewDistance).map(([x, z]) => { + const positions = generateSpiralMatrix(this.viewDistance).map(([x, z]) => { const pos = new Vec3((botX + x) * 16, 0, (botZ + z) * 16) if (!this.loadedChunks[`${pos.x},${pos.z}`]) return pos }).filter(Boolean)