Skip to content

Commit

Permalink
fix: "modern" controls now correctly works with flying
Browse files Browse the repository at this point in the history
fix: right stick button on gamepad now toggles sneaking
  • Loading branch information
zardoy committed Apr 27, 2024
1 parent a504d3f commit 9322e09
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
13 changes: 11 additions & 2 deletions src/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -152,7 +153,7 @@ const uiCommand = (command: Command) => {
}
}

export const setSneaking = (state: boolean) => {
const setSneaking = (state: boolean) => {
gameAdditionalState.isSneaking = state
bot.setControlState('sneak', state)
}
Expand All @@ -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 }))
Expand Down
4 changes: 3 additions & 1 deletion src/react/HotbarRenderApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -209,6 +210,7 @@ export default () => {
display: 'flex',
justifyContent: 'center',
zIndex: hasModals ? 1 : 8,
pointerEvents: 'none',
bottom: 'env(safe-area-inset-bottom)'
}} />
</Portal>
Expand Down
29 changes: 22 additions & 7 deletions src/react/TouchAreasControls.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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) => {
Expand All @@ -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 () {
Expand All @@ -81,23 +85,34 @@ 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 }))
worldInteractions.update()
active = false
},
jump () {
bot.setControlState('jump', false)
active = false
void contro.emit('release', {
command: 'general.jump',
schema: null as any,
})
active = bot.controlState.jump
}
}

Expand Down

0 comments on commit 9322e09

Please sign in to comment.