Android build v1.1.15
This commit is contained in:
+85
-3
@@ -13,6 +13,10 @@ import { Capacitor } from '@capacitor/core'
|
||||
|
||||
export type InputDevice = 'pc' | 'controller'
|
||||
export type ControllerIconStyle = 'xbox' | 'playstation' | 'nintendo'
|
||||
export type MovementVector = {
|
||||
x: number
|
||||
y: number
|
||||
}
|
||||
|
||||
export const INPUT_ACTIONS = [
|
||||
'navigateUp',
|
||||
@@ -131,7 +135,7 @@ const PREFERENCES_STORAGE_KEY = 'ashen-halls-input-preferences-v1'
|
||||
const GAME_ACTION_EVENT = 'ashen-halls-game-action'
|
||||
const NATIVE_CONTROLLER_EVENT = 'ashen-halls-native-controller'
|
||||
const FOCUSABLE_SELECTOR = 'button:not(:disabled):not([data-controller-nav="skip"]), input:not(:disabled):not([data-controller-nav="skip"]), select:not(:disabled):not([data-controller-nav="skip"]), textarea:not(:disabled):not([data-controller-nav="skip"]), [tabindex]:not([tabindex="-1"]):not([data-controller-nav="skip"])'
|
||||
const MAIN_CONTENT_SELECTOR = '.auth-shell, .menu-screen, .content-screen, .dungeon-run-screen, .dual-bottom-display'
|
||||
const MAIN_CONTENT_SELECTOR = '.auth-shell, .menu-screen, .content-screen, .dungeon-run-screen, .dual-bottom-display, .iwt2-bottom-display'
|
||||
const HEADER_CONTENT_SELECTOR = '.app-header'
|
||||
const GAMEPAD_COMBAT_POLL_MS = 1000 / 60
|
||||
const GAMEPAD_MENU_POLL_MS = 1000 / 30
|
||||
@@ -235,13 +239,13 @@ function loadPreferences() {
|
||||
combatTouchLocked?: boolean
|
||||
}
|
||||
return {
|
||||
controllerIconStyle: saved.controllerIconStyle ?? 'xbox',
|
||||
controllerIconStyle: saved.controllerIconStyle ?? 'playstation',
|
||||
directPartyTargeting: saved.directPartyTargeting ?? false,
|
||||
combatTouchLocked: saved.combatTouchLocked ?? Capacitor.isNativePlatform(),
|
||||
}
|
||||
} catch {
|
||||
return {
|
||||
controllerIconStyle: 'xbox' as ControllerIconStyle,
|
||||
controllerIconStyle: 'playstation' as ControllerIconStyle,
|
||||
directPartyTargeting: false,
|
||||
combatTouchLocked: Capacitor.isNativePlatform(),
|
||||
}
|
||||
@@ -530,6 +534,10 @@ function isCombatActive() {
|
||||
return Boolean(document.querySelector('[data-combat-active="true"]'))
|
||||
}
|
||||
|
||||
function hasContinuousMovementActive() {
|
||||
return Boolean(document.querySelector('[data-continuous-movement-active="true"]'))
|
||||
}
|
||||
|
||||
function firstConnectedGamepad() {
|
||||
return Array.from(navigator.getGamepads?.() ?? []).find(Boolean) ?? null
|
||||
}
|
||||
@@ -686,6 +694,12 @@ export function InputProvider({ children }: { children: ReactNode }) {
|
||||
document.querySelector('[data-combat-active="true"]'),
|
||||
)
|
||||
const uiOverlay = hasUiOverlay()
|
||||
if (
|
||||
combatActive
|
||||
&& !uiOverlay
|
||||
&& hasContinuousMovementActive()
|
||||
&& (token.startsWith('Axis0') || token.startsWith('Axis1'))
|
||||
) return
|
||||
const uiPriority = [
|
||||
'navigateUp',
|
||||
'navigateDown',
|
||||
@@ -1046,6 +1060,74 @@ export function InputProvider({ children }: { children: ReactNode }) {
|
||||
)
|
||||
}
|
||||
|
||||
function normalizedVector(x: number, y: number): MovementVector {
|
||||
const magnitude = Math.hypot(x, y)
|
||||
if (magnitude <= 1) return { x, y }
|
||||
return { x: x / magnitude, y: y / magnitude }
|
||||
}
|
||||
|
||||
function applyStickDeadzone(x: number, y: number, deadzone = 0.18): MovementVector {
|
||||
const magnitude = Math.hypot(x, y)
|
||||
if (magnitude < deadzone) return { x: 0, y: 0 }
|
||||
const scaled = Math.min(1, (magnitude - deadzone) / (1 - deadzone))
|
||||
return {
|
||||
x: (x / magnitude) * scaled,
|
||||
y: (y / magnitude) * scaled,
|
||||
}
|
||||
}
|
||||
|
||||
export function useMovementVectorRef(enabled = true) {
|
||||
const movementRef = useRef<MovementVector>({ x: 0, y: 0 })
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled) {
|
||||
movementRef.current = { x: 0, y: 0 }
|
||||
return undefined
|
||||
}
|
||||
|
||||
let frame = 0
|
||||
const pressedKeys = new Set<string>()
|
||||
const keyboardVector = () => normalizedVector(
|
||||
(pressedKeys.has('KeyD') ? 1 : 0) - (pressedKeys.has('KeyA') ? 1 : 0),
|
||||
(pressedKeys.has('KeyS') ? 1 : 0) - (pressedKeys.has('KeyW') ? 1 : 0),
|
||||
)
|
||||
const updateMovement = () => {
|
||||
const gamepad = firstConnectedGamepad()
|
||||
const stick = gamepad ? applyStickDeadzone(gamepad.axes[0] ?? 0, gamepad.axes[1] ?? 0) : { x: 0, y: 0 }
|
||||
const keyboard = keyboardVector()
|
||||
movementRef.current = Math.hypot(stick.x, stick.y) > 0
|
||||
? stick
|
||||
: keyboard
|
||||
frame = window.requestAnimationFrame(updateMovement)
|
||||
}
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
if (isTextInput(document.activeElement)) return
|
||||
if (!['KeyW', 'KeyA', 'KeyS', 'KeyD'].includes(event.code)) return
|
||||
pressedKeys.add(event.code)
|
||||
document.documentElement.dataset.inputDevice = 'pc'
|
||||
event.preventDefault()
|
||||
}
|
||||
const onKeyUp = (event: KeyboardEvent) => {
|
||||
if (!['KeyW', 'KeyA', 'KeyS', 'KeyD'].includes(event.code)) return
|
||||
pressedKeys.delete(event.code)
|
||||
event.preventDefault()
|
||||
}
|
||||
|
||||
window.addEventListener('keydown', onKeyDown)
|
||||
window.addEventListener('keyup', onKeyUp)
|
||||
frame = window.requestAnimationFrame(updateMovement)
|
||||
|
||||
return () => {
|
||||
window.cancelAnimationFrame(frame)
|
||||
window.removeEventListener('keydown', onKeyDown)
|
||||
window.removeEventListener('keyup', onKeyUp)
|
||||
movementRef.current = { x: 0, y: 0 }
|
||||
}
|
||||
}, [enabled])
|
||||
|
||||
return movementRef
|
||||
}
|
||||
|
||||
export function useInput() {
|
||||
const context = useContext(InputContext)
|
||||
if (!context) throw new Error('useInput must be used inside InputProvider')
|
||||
|
||||
Reference in New Issue
Block a user