viewer: working first person
This commit is contained in:
parent
2aade94033
commit
41b9f7b383
7 changed files with 34 additions and 26 deletions
|
|
@ -95,7 +95,7 @@ class BotViewer {
|
|||
|
||||
onPointerLockChange = () => {
|
||||
const e = this.renderer.domElement
|
||||
if (document.pointerLockElement === e) {
|
||||
if (document.pointerLockElement === e) {
|
||||
e.parentElement.addEventListener('mousemove', this.onMouseMove, { passive: true })
|
||||
} else {
|
||||
e.parentElement.removeEventListener('mousemove', this.onMouseMove, false)
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ class ClientProvider extends BotProvider {
|
|||
downKeys = new Set()
|
||||
|
||||
connect () {
|
||||
const client = new Client({ hostname: '127.0.0.1', version: '1.16.210', port: 19132, connectTimeout: 100000 })
|
||||
const client = new Client({ hostname: '127.0.0.1', version: '1.16.210', username: 'notch', offline: true, port: 19132, connectTimeout: 100000 })
|
||||
|
||||
client.once('resource_packs_info', (packet) => {
|
||||
client.write('resource_pack_client_response', {
|
||||
|
|
@ -50,7 +50,7 @@ class ClientProvider extends BotProvider {
|
|||
})
|
||||
this.client.on('start_game', packet => {
|
||||
this.updatePosition(packet.player_position)
|
||||
this.movements.init('', packet.player_position, null, packet.rotation.z, packet.rotation.x, 0)
|
||||
this.movements.init('server', packet.player_position, /* vel */ null, packet.rotation.z || 0, packet.rotation.x || 0, 0)
|
||||
})
|
||||
|
||||
this.client.on('spawn', () => {
|
||||
|
|
@ -68,7 +68,9 @@ class ClientProvider extends BotProvider {
|
|||
})
|
||||
|
||||
this.client.on('move_player', packet => {
|
||||
if (packet.runtime_id === this.client.entityId) { this.movements.updatePosition(packet.position, packet.yaw, packet.pitch, packet.head_yaw, packet.tick) }
|
||||
if (packet.runtime_id === this.client.entityId) {
|
||||
this.movements.updatePosition(packet.position, packet.yaw, packet.pitch, packet.head_yaw, packet.tick)
|
||||
}
|
||||
})
|
||||
|
||||
this.client.on('set_entity_motion', packet => {
|
||||
|
|
@ -96,7 +98,7 @@ class ClientProvider extends BotProvider {
|
|||
|
||||
onKeyUp = (evt) => {
|
||||
const code = evt.code
|
||||
if (code == 'ControlLeft' && this.downKeys.has('ControlLeft')) {
|
||||
if (code === 'ControlLeft' && this.downKeys.has('ControlLeft')) {
|
||||
this.movements.setControlState('sprint', false)
|
||||
}
|
||||
for (const control in controlMap) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
const { Physics, PlayerState } = require('prismarine-physics')
|
||||
const { performance } = require('perf_hooks')
|
||||
const { d2r } = require('./util')
|
||||
const { d2r, r2d } = require('./util')
|
||||
const vec3 = require('vec3')
|
||||
|
||||
const PHYSICS_INTERVAL_MS = 50
|
||||
|
|
@ -21,9 +21,9 @@ class MovementManager {
|
|||
set lastPos (newPos) { this.player.entity.position.set(newPos.x, newPos.y, newPos.z) }
|
||||
get lastRot () { return vec3(this.player.entity.yaw, this.player.entity.pitch, this.player.entity.headYaw) }
|
||||
set lastRot (rot) {
|
||||
this.player.entity.yaw = rot.x
|
||||
this.player.entity.pitch = rot.y
|
||||
if (rot.z) this.player.entity.headYaw = rot.z
|
||||
if (!isNaN(rot.x)) this.player.entity.yaw = rot.x
|
||||
if (!isNaN(rot.y)) this.player.entity.pitch = rot.y
|
||||
if (!isNaN(rot.z)) this.player.entity.headYaw = rot.z
|
||||
}
|
||||
|
||||
// Ask the server to be in a new position
|
||||
|
|
@ -36,28 +36,29 @@ class MovementManager {
|
|||
// console.log('We computed', this.lastPos)
|
||||
this.bot.updatePlayerCamera(2, this.lastSentPos, this.playerState.yaw, this.playerState.pitch || this.player.entity.pitch)
|
||||
if (this.serverMovements) {
|
||||
this.client.queue('player_auth_input', {
|
||||
pitch: this.player.pitch,
|
||||
yaw: this.player.yaw,
|
||||
globalThis.movePayload = {
|
||||
pitch: r2d(this.player.entity.pitch),
|
||||
yaw: r2d(this.player.entity.yaw), // r2d(this.player.entity.yaw),
|
||||
position: {
|
||||
x: this.lastPos.x,
|
||||
y: this.lastPos.y,
|
||||
y: this.lastPos.y + 1.62,
|
||||
z: this.lastPos.z
|
||||
},
|
||||
move_vector: { // Minecraft coords, N: Z+1, S: Z-1, W: X+1, E: X-1
|
||||
x: inputState.left ? 1 : (inputState.right ? -1 : 0),
|
||||
z: inputState.up ? 1 : (inputState.down ? -1 : 0)
|
||||
},
|
||||
head_yaw: this.player.headYaw,
|
||||
head_yaw: r2d(this.player.entity.yaw), // r2d(this.player.entity.headYaw),
|
||||
input_data: inputState,
|
||||
input_mode: 'mouse',
|
||||
play_mode: 'screen',
|
||||
tick: this.tick,
|
||||
delta: this.lastSentPos?.minus(this.lastPos) ?? { x: 0, y: 0, z: 0 }
|
||||
})
|
||||
this.positionUpdated = false
|
||||
}
|
||||
this.bot.client.queue('player_auth_input', globalThis.movePayload)
|
||||
}
|
||||
|
||||
this.positionUpdated = false
|
||||
this.lastSentPos = this.lastPos
|
||||
this.lastSentRot = this.lastRot
|
||||
}
|
||||
|
|
@ -126,8 +127,8 @@ class MovementManager {
|
|||
const q = this.inputQueue.shift()
|
||||
if (q) {
|
||||
Object.assign(this.playerState.control, q)
|
||||
if (q.yaw) this.player.entity.yaw = q.yaw
|
||||
if (q.pitch) this.player.entity.pitch = q.pitch
|
||||
if (!isNaN(q.yaw)) this.player.entity.yaw = q.yaw
|
||||
if (!isNaN(q.pitch)) this.player.entity.pitch = q.pitch
|
||||
}
|
||||
this.playerState = new PlayerState(this.player, this.controls)
|
||||
this.physics.simulatePlayer(this.playerState, this.world.sync).apply(this.player)
|
||||
|
|
@ -147,8 +148,8 @@ class MovementManager {
|
|||
sneak_down: false,
|
||||
up: this.controls.forward,
|
||||
down: this.controls.back,
|
||||
left: this.controls.left,
|
||||
right: this.controls.right,
|
||||
left: this.controls.right,
|
||||
right: this.controls.left,
|
||||
up_left: false,
|
||||
up_right: false,
|
||||
want_up: this.controls.jump, // Jump
|
||||
|
|
@ -247,9 +248,7 @@ class MovementManager {
|
|||
}
|
||||
|
||||
onViewerCameraMove (newYaw, newPitch, newHeadYaw) {
|
||||
this.player.yaw = newYaw
|
||||
this.player.pitch = newPitch
|
||||
this.player.headYaw = newHeadYaw
|
||||
this.lastRot = { x: newYaw, y: newPitch, z: newHeadYaw }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,8 +9,14 @@ const difference = (o1, o2) => Object.keys(o2).reduce((diff, key) => {
|
|||
const diff = (o1, o2) => { const dif = difference(o1, o2); return Object.keys(dif).length ? dif : null }
|
||||
|
||||
const d2r = deg => (180 - (deg < 0 ? (360 + deg) : deg)) * (Math.PI / 180)
|
||||
const r2d = rad => {
|
||||
let deg = rad * (180 / Math.PI)
|
||||
deg = deg % 360
|
||||
return 180 - deg
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
diff,
|
||||
d2r
|
||||
d2r,
|
||||
r2d
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue