From 9322e09a836a0d896ccaa0e5aa8ebd48e31f34f2 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Sat, 27 Apr 2024 22:55:25 +0300 Subject: [PATCH] fix: "modern" controls now correctly works with flying fix: right stick button on gamepad now toggles sneaking --- src/controls.ts | 13 +++++++++++-- src/react/HotbarRenderApp.tsx | 4 +++- src/react/TouchAreasControls.tsx | 29 ++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/controls.ts b/src/controls.ts index 734a2a6d6..264050829 100644 --- a/src/controls.ts +++ b/src/controls.ts @@ -32,7 +32,8 @@ export const contro = new ControMax({ jump: ['Space', 'A'], inventory: ['KeyE', 'X'], drop: ['KeyQ', 'B'], - sneak: ['ShiftLeft', 'Right Stick'], + sneak: ['ShiftLeft'], + toggleSneakOrDown: [null, 'Right Stick'], sprint: ['ControlLeft', 'Left Stick'], nextHotbarSlot: [null, 'Left Bumper'], prevHotbarSlot: [null, 'Right Bumper'], @@ -152,7 +153,7 @@ const uiCommand = (command: Command) => { } } -export const setSneaking = (state: boolean) => { +const setSneaking = (state: boolean) => { gameAdditionalState.isSneaking = state bot.setControlState('sneak', state) } @@ -178,6 +179,14 @@ const onTriggerOrReleased = (command: Command, pressed: boolean) => { if (pressed) { setSprinting(pressed) } + break + case 'general.toggleSneakOrDown': + if (gameAdditionalState.isFlying) { + setSneaking(pressed) + } else if (pressed) { + setSneaking(!gameAdditionalState.isSneaking) + } + break case 'general.attackDestroy': document.dispatchEvent(new MouseEvent(pressed ? 'mousedown' : 'mouseup', { button: 0 })) diff --git a/src/react/HotbarRenderApp.tsx b/src/react/HotbarRenderApp.tsx index e747c4e38..5b32e3ccf 100644 --- a/src/react/HotbarRenderApp.tsx +++ b/src/react/HotbarRenderApp.tsx @@ -108,13 +108,14 @@ export default () => { } setSize() watchUnloadForCleanup(subscribe(currentScaling, setSize)) + inv.canvas.style.pointerEvents = 'auto' container.current.appendChild(inv.canvas) const upHotbarItems = () => { if (!viewer.world.downloadedTextureImage && !viewer.world.customTexturesDataUrl) return upInventoryItems(true, inv) } - canvasManager.canvas.onpointerdown = (e) => { + canvasManager.canvas.onclick = (e) => { if (!isGameActive(true)) return const pos = inv.canvasManager.getMousePos(inv.canvas, e) if (canvasManager.canvas.width - pos.x < 35 * inv.canvasManager.scale) { @@ -209,6 +210,7 @@ export default () => { display: 'flex', justifyContent: 'center', zIndex: hasModals ? 1 : 8, + pointerEvents: 'none', bottom: 'env(safe-area-inset-bottom)' }} /> diff --git a/src/react/TouchAreasControls.tsx b/src/react/TouchAreasControls.tsx index e2a3fd873..f56db0471 100644 --- a/src/react/TouchAreasControls.tsx +++ b/src/react/TouchAreasControls.tsx @@ -1,6 +1,6 @@ -import { CSSProperties, PointerEvent, PointerEventHandler, useEffect, useRef, useState } from 'react' +import { CSSProperties, PointerEvent, useEffect, useRef } from 'react' import { proxy, ref, useSnapshot } from 'valtio' -import { contro, setSneaking } from '../controls' +import { contro } from '../controls' import worldInteractions from '../worldInteractions' import PixelartIcon from './PixelartIcon' import Button from './Button' @@ -56,6 +56,7 @@ export default ({ touchActive, setupActive, buttonsPositions, closeButtonsSetup const joystickInner = useRef(null) const { pointer } = useSnapshot(joystickPointer) + // const { isFlying, isSneaking } = useSnapshot(gameAdditionalState) const newButtonPositions = { ...buttonsPositions } const buttonProps = (name: ButtonName) => { @@ -72,7 +73,10 @@ export default ({ touchActive, setupActive, buttonsPositions, closeButtonsSetup document.dispatchEvent(new MouseEvent('mouseup', { button: 2 })) }, sneak () { - setSneaking(!bot.getControlState('sneak')) + void contro.emit('trigger', { + command: 'general.toggleSneakOrDown', + schema: null as any, + }) active = bot.getControlState('sneak') }, break () { @@ -81,14 +85,22 @@ export default ({ touchActive, setupActive, buttonsPositions, closeButtonsSetup active = true }, jump () { - bot.setControlState('jump', true) - active = true + void contro.emit('trigger', { + command: 'general.jump', + schema: null as any, + }) + active = bot.controlState.jump } } const holdUp = { action () { }, sneak () { + void contro.emit('release', { + command: 'general.toggleSneakOrDown', + schema: null as any, + }) + active = bot.getControlState('sneak') }, break () { document.dispatchEvent(new MouseEvent('mouseup', { button: 0 })) @@ -96,8 +108,11 @@ export default ({ touchActive, setupActive, buttonsPositions, closeButtonsSetup active = false }, jump () { - bot.setControlState('jump', false) - active = false + void contro.emit('release', { + command: 'general.jump', + schema: null as any, + }) + active = bot.controlState.jump } }