Skip to content

Commit

Permalink
should fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Oct 6, 2023
1 parent deb3255 commit 7ad7ef2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions prismarine-viewer/examples/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 8 additions & 7 deletions prismarine-viewer/viewer/lib/worldDataEmitter.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
}

Expand All @@ -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()
Expand All @@ -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}`]
}
Expand All @@ -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)
Expand Down

0 comments on commit 7ad7ef2

Please sign in to comment.