fix: "modern" controls now correctly works with flying

fix: right stick button on gamepad now toggles sneaking
This commit is contained in:
Vitaly Turovsky 2024-04-27 22:55:25 +03:00
commit 9322e09a83
3 changed files with 36 additions and 10 deletions

View file

@ -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 }))

View file

@ -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)'
}} />
</Portal>

View file

@ -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<HTMLDivElement>(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
}
}