Skip to content

Commit

Permalink
Implemented focus to point
Browse files Browse the repository at this point in the history
  • Loading branch information
SinanAkkoyun committed Sep 4, 2024
1 parent f33da9c commit 0da58c6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
})
})
6 changes: 6 additions & 0 deletions lib/mineflayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions viewer/lib/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0da58c6

Please sign in to comment.