diff --git a/index.d.ts b/index.d.ts index 8e525015..6b4b32e2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -50,6 +50,7 @@ declare module 'mineflayer' { drawBoxGrid: (id: string, start: Vec3, end: Vec3, color?: string) => void; drawLine: (id: string, points: Vec3[], color?: number) => void; drawPoints: (id: string, points: Vec3[], color?: number, size?: number) => void; + focusPoint: (position: Vec3) => void; close: () => void; on(event: 'blockClicked', listener: (block: any, face: any, button: any) => void): this; emit(event: 'blockClicked', block: any, face: any, button: any): boolean; diff --git a/lib/index.js b/lib/index.js index 706772dd..106ab342 100644 --- a/lib/index.js +++ b/lib/index.js @@ -43,7 +43,7 @@ socket.on('version', (version) => { viewer.listen(socket) let botMesh - socket.on('position', ({ pos, addMesh, yaw, pitch }) => { + socket.on('position', ({ pos, addMesh, yaw, pitch, focus }) => { if (yaw !== undefined && pitch !== undefined) { if (controls) { controls.dispose() @@ -70,4 +70,8 @@ socket.on('version', (version) => { new TWEEN.Tween(botMesh.rotation).to({ y: botMesh.rotation.y + dy }, 50).start() } }) + + socket.on('focusPoint', (pos) => { + viewer.focusOnPosition(pos, controls) + }) }) diff --git a/lib/mineflayer.js b/lib/mineflayer.js index 19145a26..d7ad7c31 100644 --- a/lib/mineflayer.js +++ b/lib/mineflayer.js @@ -45,6 +45,12 @@ module.exports = (bot, { viewDistance = 6, firstPerson = false, port = 3000, pre } } + bot.viewer.focusPoint = (position) => { + for (const socket of sockets) { + socket.emit('focusPoint', position) + } + } + io.on('connection', (socket) => { socket.emit('version', bot.version) sockets.push(socket) diff --git a/viewer/lib/viewer.js b/viewer/lib/viewer.js index db02d904..4a6403ae 100644 --- a/viewer/lib/viewer.js +++ b/viewer/lib/viewer.js @@ -82,6 +82,31 @@ class Viewer { this.camera.rotation.set(pitch, yaw, 0, 'ZYX') } + focusOnPosition(pos, controls) { + if(controls) { + // Calculate the initial offset between the camera (controls.object) and its current target + const initialOffset = new THREE.Vector3().subVectors(controls.object.position, controls.target); + + // Set the end position for the target + const newTarget = new THREE.Vector3(pos.x, pos.y, pos.z); + + // Start a tween for the target + new TWEEN.Tween(controls.target) + .to({x: newTarget.x, y: newTarget.y, z: newTarget.z}, 800) + .easing(TWEEN.Easing.Quadratic.InOut) + .onUpdate(() => { + // As the target moves, calculate the new position for the camera using the updated target and the initial offset + controls.object.position.x = controls.target.x + initialOffset.x; + controls.object.position.y = controls.target.y + initialOffset.y; + controls.object.position.z = controls.target.z + initialOffset.z; + + // Optional: Update the controls in each frame if needed + controls.update(); + }) + .start(); + } + } + listen (emitter) { emitter.on('entity', (e) => { this.updateEntity(e)