Spaces:
Sleeping
Sleeping
File size: 9,243 Bytes
1829efb d55e335 1829efb d55e335 1829efb d55e335 1829efb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | import { useRef, useEffect, useState, forwardRef, useImperativeHandle, useCallback } from 'react'
import { useFrame, useThree } from '@react-three/fiber'
import { RigidBody, CapsuleCollider, useRapier } from '@react-three/rapier'
import * as THREE from 'three'
import { useCameraGestures } from '../camera/useCameraGestures'
import { useDebugStore } from '../../store/debug'
import { isEditableTarget } from '../../utils/dom'
import {
CAMERA_EYE_OFFSET,
CHARACTER_BODY_SPAWN,
CHARACTER_BODY_SPAWN_POSITION,
CHARACTER_HEIGHT,
} from './spawn'
export interface CharacterControllerHandle {
reset: () => void
}
const SPEED = 4
const JUMP_VELOCITY = 3
const MOUSE_SMOOTHING = 0.12
const DOLLY_UNITS_PER_PIXEL = 0.01
const CHARACTER_RADIUS = 0.25
const CHARACTER_HALF_SEGMENT = CHARACTER_HEIGHT / 2 - CHARACTER_RADIUS
const GROUND_CHECK_DISTANCE = CAMERA_EYE_OFFSET + 0.08
const DEFAULT_YAW = 0
const _forward = new THREE.Vector3()
const _right = new THREE.Vector3()
const _dollyForward = new THREE.Vector3()
const _move = new THREE.Vector3()
const _euler = new THREE.Euler(0, 0, 0, 'YXZ')
export const CharacterController = forwardRef<CharacterControllerHandle>(
function CharacterController(_, ref) {
const bodyRef = useRef<React.ComponentRef<typeof RigidBody>>(null)
const { camera, gl } = useThree()
const mouseSensitivity = useDebugStore((s) => s.flyMouseSensitivity)
const { rapier, world } = useRapier()
const keys = useRef(new Set<string>())
const jumpQueued = useRef(false)
const rawYaw = useRef(DEFAULT_YAW)
const rawPitch = useRef(0)
const smoothYaw = useRef(DEFAULT_YAW)
const smoothPitch = useRef(0)
const touchLook = useRef<{ id: number; x: number; y: number } | null>(null)
const touchMove = useRef<{ id: number; x: number; y: number } | null>(null)
const [joystickPos, setJoystickPos] = useState({ x: 0, y: 0 })
useImperativeHandle(ref, () => ({
reset: () => {
if (!bodyRef.current) return
let spawnX = CHARACTER_BODY_SPAWN.x
let spawnY = CHARACTER_BODY_SPAWN.y
let spawnZ = CHARACTER_BODY_SPAWN.z
let spawnYaw = DEFAULT_YAW
let spawnPitch = 0
if (typeof window !== 'undefined') {
const params = new URLSearchParams(window.location.search)
const spawnParam = params.get('spawn')
if (spawnParam) {
const parts = spawnParam.split(',').map(parseFloat)
if (parts.length >= 3 && !parts.slice(0, 3).some(isNaN)) {
spawnX = parts[0]
spawnY = parts[1] - CAMERA_EYE_OFFSET
spawnZ = parts[2]
}
if (parts.length >= 5 && !parts.slice(3, 5).some(isNaN)) {
spawnYaw = parts[3]
spawnPitch = parts[4]
}
}
}
bodyRef.current.setTranslation({ x: spawnX, y: spawnY, z: spawnZ }, true)
bodyRef.current.setLinvel({ x: 0, y: 0, z: 0 }, true)
bodyRef.current.setAngvel({ x: 0, y: 0, z: 0 }, true)
rawYaw.current = spawnYaw
rawPitch.current = spawnPitch
smoothYaw.current = spawnYaw
smoothPitch.current = spawnPitch
keys.current.clear()
camera.quaternion.setFromEuler(_euler.set(spawnPitch, spawnYaw, 0))
},
}))
const applyDolly = useCallback((deltaY: number) => {
const body = bodyRef.current
if (!body) return
_dollyForward.set(0, 0, -1).applyQuaternion(camera.quaternion).setY(0)
if (_dollyForward.lengthSq() < 0.0001) return
_dollyForward.normalize()
const amount = -deltaY * DOLLY_UNITS_PER_PIXEL
const position = body.translation()
body.setTranslation(
{
x: position.x + _dollyForward.x * amount,
y: position.y,
z: position.z + _dollyForward.z * amount,
},
true,
)
body.wakeUp()
}, [camera])
const applyLook = useCallback((dx: number, dy: number) => {
rawYaw.current -= dx * mouseSensitivity
rawPitch.current -= dy * mouseSensitivity
rawPitch.current = Math.max(-Math.PI / 2.2, Math.min(Math.PI / 2.2, rawPitch.current))
}, [mouseSensitivity])
useCameraGestures({ domElement: gl.domElement, onDollyPixels: applyDolly, onTumblePixels: applyLook })
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (isEditableTarget(e.target)) {
keys.current.delete(e.code)
return
}
if (e.type === 'keydown') {
keys.current.add(e.code)
if (e.code === 'Space' && !e.repeat) {
jumpQueued.current = true
e.preventDefault()
}
} else {
keys.current.delete(e.code)
}
}
window.addEventListener('keydown', onKey)
window.addEventListener('keyup', onKey)
// Touch handlers
const onTouchStart = (e: TouchEvent) => {
if (e.touches.length >= 2) return
for (const touch of Array.from(e.changedTouches)) {
const isLeft = touch.clientX < window.innerWidth / 2
if (isLeft && !touchMove.current) {
touchMove.current = { id: touch.identifier, x: touch.clientX, y: touch.clientY }
setJoystickPos({ x: touch.clientX, y: touch.clientY })
} else if (!isLeft && !touchLook.current) {
touchLook.current = { id: touch.identifier, x: touch.clientX, y: touch.clientY }
}
}
}
const onTouchMove = (e: TouchEvent) => {
if (e.touches.length >= 2) return
for (const touch of Array.from(e.changedTouches)) {
if (touchLook.current?.id === touch.identifier) {
const dx = touch.clientX - touchLook.current.x
const dy = touch.clientY - touchLook.current.y
rawYaw.current -= dx * 0.004
rawPitch.current -= dy * 0.004
rawPitch.current = Math.max(-Math.PI / 2.2, Math.min(Math.PI / 2.2, rawPitch.current))
touchLook.current = { id: touch.identifier, x: touch.clientX, y: touch.clientY }
}
if (touchMove.current?.id === touch.identifier) {
touchMove.current = { ...touchMove.current, x: touch.clientX, y: touch.clientY }
}
}
}
const onTouchEnd = (e: TouchEvent) => {
for (const touch of Array.from(e.changedTouches)) {
if (touchLook.current?.id === touch.identifier) touchLook.current = null
if (touchMove.current?.id === touch.identifier) {
touchMove.current = null
setJoystickPos({ x: 0, y: 0 })
}
}
}
gl.domElement.addEventListener('touchstart', onTouchStart, { passive: true })
gl.domElement.addEventListener('touchmove', onTouchMove, { passive: true })
gl.domElement.addEventListener('touchend', onTouchEnd, { passive: true })
return () => {
window.removeEventListener('keydown', onKey)
window.removeEventListener('keyup', onKey)
gl.domElement.removeEventListener('touchstart', onTouchStart)
gl.domElement.removeEventListener('touchmove', onTouchMove)
gl.domElement.removeEventListener('touchend', onTouchEnd)
}
}, [gl.domElement])
useFrame((_state, delta) => {
const body = bodyRef.current
if (!body) return
const smoothing = 1 - Math.pow(1 - MOUSE_SMOOTHING, delta * 60)
smoothYaw.current += (rawYaw.current - smoothYaw.current) * smoothing
smoothPitch.current += (rawPitch.current - smoothPitch.current) * smoothing
_euler.set(smoothPitch.current, smoothYaw.current, 0)
camera.quaternion.setFromEuler(_euler)
// Compute move direction from keys + touch joystick
let fwd = 0, strafe = 0
if (keys.current.has('KeyW') || keys.current.has('ArrowUp')) fwd += 1
if (keys.current.has('KeyS') || keys.current.has('ArrowDown')) fwd -= 1
if (keys.current.has('KeyA') || keys.current.has('ArrowLeft')) strafe -= 1
if (keys.current.has('KeyD') || keys.current.has('ArrowRight')) strafe += 1
if (touchMove.current) {
const origin = joystickPos
const dx = (touchMove.current.x - origin.x) / 50
const dy = (touchMove.current.y - origin.y) / 50
strafe += Math.max(-1, Math.min(1, dx))
fwd -= Math.max(-1, Math.min(1, dy))
}
_forward.set(0, 0, -1).applyQuaternion(camera.quaternion).setY(0).normalize()
_right.set(1, 0, 0).applyQuaternion(camera.quaternion).setY(0).normalize()
_move.set(0, 0, 0).addScaledVector(_forward, fwd).addScaledVector(_right, strafe)
if (_move.lengthSq() > 1) _move.normalize()
const vel = body.linvel()
let nextY = vel.y
if (jumpQueued.current) {
const pos = body.translation()
const ray = new rapier.Ray(
{ x: pos.x, y: pos.y, z: pos.z },
{ x: 0, y: -1, z: 0 },
)
const grounded = Boolean(world.castRay(ray, GROUND_CHECK_DISTANCE, true, undefined, undefined, undefined, body))
if (grounded) nextY = JUMP_VELOCITY
jumpQueued.current = false
}
body.setLinvel({ x: _move.x * SPEED, y: nextY, z: _move.z * SPEED }, true)
// Sync camera position to body
const pos = body.translation()
camera.position.set(pos.x, pos.y + CAMERA_EYE_OFFSET, pos.z)
})
return (
<RigidBody
ref={bodyRef}
position={CHARACTER_BODY_SPAWN_POSITION}
enabledRotations={[false, false, false]}
linearDamping={0}
>
<CapsuleCollider args={[CHARACTER_HALF_SEGMENT, CHARACTER_RADIUS]} />
</RigidBody>
)
})
|