From 7841ba9d921c5f773c41588b0af1fd80d15a7212 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Fri, 13 Dec 2024 20:39:41 +0300 Subject: [PATCH] implement offhand --- .../viewer/lib/worldDataEmitter.ts | 41 +++++++++++-------- .../viewer/lib/worldrendererCommon.ts | 4 +- .../viewer/lib/worldrendererThree.ts | 30 ++++++++++---- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/prismarine-viewer/viewer/lib/worldDataEmitter.ts b/prismarine-viewer/viewer/lib/worldDataEmitter.ts index 20543538a..371ef8f58 100644 --- a/prismarine-viewer/viewer/lib/worldDataEmitter.ts +++ b/prismarine-viewer/viewer/lib/worldDataEmitter.ts @@ -107,24 +107,33 @@ export class WorldDataEmitter extends EventEmitter { time: () => { this.emitter.emit('time', bot.time.timeOfDay) }, - heldItemChanged: () => { - if (!this.handDisplay) { - viewer.world.onHandItemSwitch(undefined) - return - } - const newItem = bot.heldItem - if (!newItem) { - viewer.world.onHandItemSwitch(undefined) - return - } - const block = loadedData.blocksByName[newItem.name] - // todo clean types - const blockProperties = block ? new window.PrismarineBlock(block.id, 'void', newItem.metadata).getProperties() : {} - // todo item props - viewer.world.onHandItemSwitch({ name: newItem.name, properties: blockProperties, id: newItem.type, type: block ? 'block' : 'item', }) + heldItemChanged () { + handChanged(false) }, } satisfies Partial - this.eventListeners.heldItemChanged() + const handChanged = (isLeftHand: boolean) => { + if (!this.handDisplay) { + viewer.world.onHandItemSwitch(undefined, isLeftHand) + return + } + const newItem = isLeftHand ? bot.inventory.slots[45] : bot.heldItem + if (!newItem) { + viewer.world.onHandItemSwitch(undefined, isLeftHand) + return + } + const block = loadedData.blocksByName[newItem.name] + // todo clean types + const blockProperties = block ? new window.PrismarineBlock(block.id, 'void', newItem.metadata).getProperties() : {} + // todo item props + viewer.world.onHandItemSwitch({ name: newItem.name, properties: blockProperties, id: newItem.type, type: block ? 'block' : 'item', }, isLeftHand) + } + bot.inventory.on('updateSlot', (index) => { + if (index === 45) { + handChanged(true) + } + }) + handChanged(false) + handChanged(true) bot._client.on('update_light', ({ chunkX, chunkZ }) => { diff --git a/prismarine-viewer/viewer/lib/worldrendererCommon.ts b/prismarine-viewer/viewer/lib/worldrendererCommon.ts index 71c094b4f..a7c3972c1 100644 --- a/prismarine-viewer/viewer/lib/worldrendererCommon.ts +++ b/prismarine-viewer/viewer/lib/worldrendererCommon.ts @@ -217,8 +217,8 @@ export abstract class WorldRendererCommon } } - onHandItemSwitch (item: HandItemBlock | undefined): void { } - changeHandSwingingState (isAnimationPlaying: boolean): void { } + onHandItemSwitch (item: HandItemBlock | undefined, isLeftHand: boolean): void { } + changeHandSwingingState (isAnimationPlaying: boolean, isLeftHand: boolean): void { } abstract handleWorkerMessage (data: WorkerReceive): void diff --git a/prismarine-viewer/viewer/lib/worldrendererThree.ts b/prismarine-viewer/viewer/lib/worldrendererThree.ts index 0f3190d34..de1116908 100644 --- a/prismarine-viewer/viewer/lib/worldrendererThree.ts +++ b/prismarine-viewer/viewer/lib/worldrendererThree.ts @@ -23,6 +23,7 @@ export class WorldRendererThree extends WorldRendererCommon { starField: StarField cameraSectionPos: Vec3 = new Vec3(0, 0, 0) holdingBlock: HoldingBlock + holdingBlockLeft: HoldingBlock rendererDevice = '...' get tilesRendered () { @@ -38,33 +39,43 @@ export class WorldRendererThree extends WorldRendererCommon { this.rendererDevice = String(WorldRendererThree.getRendererInfo(this.renderer)) this.starField = new StarField(scene) this.holdingBlock = new HoldingBlock() + this.holdingBlockLeft = new HoldingBlock() + this.holdingBlockLeft.rightSide = false this.renderUpdateEmitter.on('itemsTextureDownloaded', () => { - if (this.holdingBlock.toBeRenderedItem || true) { + if (this.holdingBlock.toBeRenderedItem) { this.onHandItemSwitch(this.holdingBlock.toBeRenderedItem) this.holdingBlock.toBeRenderedItem = undefined } + if (this.holdingBlockLeft.toBeRenderedItem) { + this.onHandItemSwitch(this.holdingBlock.toBeRenderedItem, true) + this.holdingBlockLeft.toBeRenderedItem = undefined + } }) this.addDebugOverlay() } - onHandItemSwitch (item: HandItemBlock | undefined) { - item ??= { - type: 'hand', + onHandItemSwitch (item: HandItemBlock | undefined, isLeft = false) { + if (!isLeft) { + item ??= { + type: 'hand', + } } + const holdingBlock = isLeft ? this.holdingBlockLeft : this.holdingBlock if (!this.currentTextureImage) { - this.holdingBlock.toBeRenderedItem = item + holdingBlock.toBeRenderedItem = item return } - void this.holdingBlock.initHandObject(this.material, this.blockstatesModels, this.blocksAtlases, item) + void holdingBlock.initHandObject(this.material, this.blockstatesModels, this.blocksAtlases, item) } - changeHandSwingingState (isAnimationPlaying: boolean) { + changeHandSwingingState (isAnimationPlaying: boolean, isLeft = false) { + const holdingBlock = isLeft ? this.holdingBlockLeft : this.holdingBlock if (isAnimationPlaying) { - this.holdingBlock.startSwing() + holdingBlock.startSwing() } else { - void this.holdingBlock.stopSwing() + void holdingBlock.stopSwing() } } @@ -230,6 +241,7 @@ export class WorldRendererThree extends WorldRendererCommon { const cam = this.camera instanceof THREE.Group ? this.camera.children.find(child => child instanceof THREE.PerspectiveCamera) as THREE.PerspectiveCamera : this.camera this.renderer.render(this.scene, cam) this.holdingBlock.render(this.camera, this.renderer, viewer.ambientLight, viewer.directionalLight) + this.holdingBlockLeft.render(this.camera, this.renderer, viewer.ambientLight, viewer.directionalLight) } renderSign (position: Vec3, rotation: number, isWall: boolean, isHanging: boolean, blockEntity) {