Upload src/hooks/usePlayerControls.ts with huggingface_hub
Browse files- src/hooks/usePlayerControls.ts +73 -49
src/hooks/usePlayerControls.ts
CHANGED
|
@@ -28,7 +28,6 @@ export function usePlayerControls() {
|
|
| 28 |
jump: false, sneak: false, sprint: false,
|
| 29 |
})
|
| 30 |
const isPointerLocked = useRef(false)
|
| 31 |
-
const breakInterval = useRef<NodeJS.Timeout | null>(null)
|
| 32 |
|
| 33 |
// Block interaction via raycasting (DDA algorithm)
|
| 34 |
const raycastBlock = useCallback((maxDist: number = REACH_DISTANCE) => {
|
|
@@ -38,12 +37,10 @@ export function usePlayerControls() {
|
|
| 38 |
const [px, py, pz] = player.position
|
| 39 |
const [yaw, pitch] = player.rotation
|
| 40 |
|
| 41 |
-
// Origin = eye position
|
| 42 |
const ox = px
|
| 43 |
const oy = py + PLAYER_EYE_HEIGHT
|
| 44 |
const oz = pz
|
| 45 |
|
| 46 |
-
// Direction from yaw/pitch
|
| 47 |
const dirX = -Math.sin(yaw) * Math.cos(pitch)
|
| 48 |
const dirY = -Math.sin(pitch)
|
| 49 |
const dirZ = -Math.cos(yaw) * Math.cos(pitch)
|
|
@@ -56,13 +53,14 @@ export function usePlayerControls() {
|
|
| 56 |
const stepY = dirY >= 0 ? 1 : -1
|
| 57 |
const stepZ = dirZ >= 0 ? 1 : -1
|
| 58 |
|
| 59 |
-
const
|
| 60 |
-
const
|
| 61 |
-
const
|
|
|
|
| 62 |
|
| 63 |
-
let tMaxX = dirX >= 0 ? (x + 1 - ox) / dirX : (ox - x) / -dirX
|
| 64 |
-
let tMaxY = dirY >= 0 ? (y + 1 - oy) / dirY : (oy - y) / -dirY
|
| 65 |
-
let tMaxZ = dirZ >= 0 ? (z + 1 - oz) / dirZ : (oz - z) / -dirZ
|
| 66 |
|
| 67 |
let prevX = x, prevY = y, prevZ = z
|
| 68 |
const { getBlock } = useWorldStore.getState()
|
|
@@ -71,7 +69,7 @@ export function usePlayerControls() {
|
|
| 71 |
const block = getBlock(x, y, z)
|
| 72 |
if (block !== BLOCK_AIR) {
|
| 73 |
const def = BLOCK_DEFS[block]
|
| 74 |
-
if (def && def.solid) {
|
| 75 |
return {
|
| 76 |
blockPos: [x, y, z] as [number, number, number],
|
| 77 |
placePos: [prevX, prevY, prevZ] as [number, number, number],
|
|
@@ -115,7 +113,7 @@ export function usePlayerControls() {
|
|
| 115 |
case 'KeyD': keys.current.right = true; break
|
| 116 |
case 'Space': keys.current.jump = true; e.preventDefault(); break
|
| 117 |
case 'ShiftLeft': case 'ShiftRight': keys.current.sneak = true; break
|
| 118 |
-
case 'ControlLeft': keys.current.sprint = true; break
|
| 119 |
case 'Escape':
|
| 120 |
if (isPointerLocked.current) {
|
| 121 |
document.exitPointerLock()
|
|
@@ -133,6 +131,14 @@ export function usePlayerControls() {
|
|
| 133 |
e.preventDefault()
|
| 134 |
useGameStore.getState().setDebug(!useGameStore.getState().debug)
|
| 135 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
case 'Digit1': case 'Digit2': case 'Digit3': case 'Digit4': case 'Digit5':
|
| 137 |
case 'Digit6': case 'Digit7': case 'Digit8': case 'Digit9':
|
| 138 |
useUIStore.getState().setHotbarSlot(parseInt(e.code.slice(5)) - 1)
|
|
@@ -156,7 +162,7 @@ export function usePlayerControls() {
|
|
| 156 |
if (!isPointerLocked.current) return
|
| 157 |
const { player, updateRotation } = usePlayerStore.getState()
|
| 158 |
if (!player) return
|
| 159 |
-
const sensitivity =
|
| 160 |
const yaw = player.rotation[0] - e.movementX * sensitivity
|
| 161 |
const pitch = Math.max(-Math.PI / 2 + 0.01,
|
| 162 |
Math.min(Math.PI / 2 - 0.01, player.rotation[1] - e.movementY * sensitivity))
|
|
@@ -167,7 +173,6 @@ export function usePlayerControls() {
|
|
| 167 |
isPointerLocked.current = document.pointerLockElement !== null
|
| 168 |
}, [])
|
| 169 |
|
| 170 |
-
// Mouse click = break/place blocks
|
| 171 |
const handleMouseDown = useCallback((e: MouseEvent) => {
|
| 172 |
if (!isPointerLocked.current) return
|
| 173 |
e.preventDefault()
|
|
@@ -185,7 +190,7 @@ export function usePlayerControls() {
|
|
| 185 |
// Right click = place block
|
| 186 |
const slot = useUIStore.getState().hotbarSlot
|
| 187 |
const item = player.inventory.hotbar[slot]
|
| 188 |
-
if (item && BLOCK_DEFS[item.id]
|
| 189 |
setBlock(hit.placePos[0], hit.placePos[1], hit.placePos[2], item.id)
|
| 190 |
}
|
| 191 |
}
|
|
@@ -217,11 +222,6 @@ export function usePlayerControls() {
|
|
| 217 |
document.addEventListener('contextmenu', handleContextMenu)
|
| 218 |
document.addEventListener('wheel', handleWheel)
|
| 219 |
|
| 220 |
-
const handleClick = () => {
|
| 221 |
-
if (!isPointerLocked.current) document.body.requestPointerLock()
|
| 222 |
-
}
|
| 223 |
-
document.addEventListener('click', handleClick)
|
| 224 |
-
|
| 225 |
return () => {
|
| 226 |
document.removeEventListener('keydown', handleKeyDown)
|
| 227 |
document.removeEventListener('keyup', handleKeyUp)
|
|
@@ -230,7 +230,6 @@ export function usePlayerControls() {
|
|
| 230 |
document.removeEventListener('mousedown', handleMouseDown)
|
| 231 |
document.removeEventListener('contextmenu', handleContextMenu)
|
| 232 |
document.removeEventListener('wheel', handleWheel)
|
| 233 |
-
document.removeEventListener('click', handleClick)
|
| 234 |
}
|
| 235 |
}, [])
|
| 236 |
|
|
@@ -265,9 +264,9 @@ export function updatePlayerMovement(keys: KeyState, delta: number) {
|
|
| 265 |
if (keys.right) { moveX += right[0]; moveZ += right[1] }
|
| 266 |
|
| 267 |
// Add mobile movement
|
| 268 |
-
if (window.__MC_MOBILE_MOVE) {
|
| 269 |
-
moveX += window.__MC_MOBILE_MOVE.x
|
| 270 |
-
moveZ += window.__MC_MOBILE_MOVE.z
|
| 271 |
}
|
| 272 |
|
| 273 |
const len = Math.sqrt(moveX * moveX + moveZ * moveZ)
|
|
@@ -279,8 +278,8 @@ export function updatePlayerMovement(keys: KeyState, delta: number) {
|
|
| 279 |
let [vx, vy, vz] = player.velocity
|
| 280 |
let [px, py, pz] = player.position
|
| 281 |
|
| 282 |
-
const jumping = keys.jump || window.__MC_MOBILE_JUMP
|
| 283 |
-
const sneaking = keys.sneak || window.__MC_MOBILE_SNEAK
|
| 284 |
|
| 285 |
if (player.flying) {
|
| 286 |
vx = moveX; vz = moveZ; vy = 0
|
|
@@ -291,44 +290,69 @@ export function updatePlayerMovement(keys: KeyState, delta: number) {
|
|
| 291 |
vy -= GRAVITY * delta
|
| 292 |
if (jumping && player.onGround) vy = JUMP_VELOCITY
|
| 293 |
vy = Math.max(vy, -78.4)
|
| 294 |
-
|
| 295 |
-
// Simple collision: check block below
|
| 296 |
-
const { getBlock } = useWorldStore.getState()
|
| 297 |
-
const blockBelow = getBlock(
|
| 298 |
-
Math.floor(px), Math.floor(py - 0.1), Math.floor(pz)
|
| 299 |
-
)
|
| 300 |
-
const defBelow = BLOCK_DEFS[blockBelow]
|
| 301 |
-
if (defBelow && defBelow.solid && vy < 0 && py % 1 < 0.1) {
|
| 302 |
-
vy = 0
|
| 303 |
-
py = Math.ceil(py)
|
| 304 |
-
}
|
| 305 |
}
|
| 306 |
|
|
|
|
| 307 |
px += vx * delta
|
| 308 |
py += vy * delta
|
| 309 |
pz += vz * delta
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
}
|
| 314 |
|
| 315 |
-
//
|
| 316 |
-
const { getBlock } = useWorldStore.getState()
|
| 317 |
const playerWidth = 0.3
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
| 323 |
if (BLOCK_DEFS[block]?.solid) {
|
| 324 |
-
|
| 325 |
-
if (
|
| 326 |
-
|
| 327 |
}
|
| 328 |
}
|
| 329 |
}
|
| 330 |
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
store.updatePosition([px, py, pz])
|
| 334 |
store.updateVelocity([vx, vy, vz])
|
|
|
|
| 28 |
jump: false, sneak: false, sprint: false,
|
| 29 |
})
|
| 30 |
const isPointerLocked = useRef(false)
|
|
|
|
| 31 |
|
| 32 |
// Block interaction via raycasting (DDA algorithm)
|
| 33 |
const raycastBlock = useCallback((maxDist: number = REACH_DISTANCE) => {
|
|
|
|
| 37 |
const [px, py, pz] = player.position
|
| 38 |
const [yaw, pitch] = player.rotation
|
| 39 |
|
|
|
|
| 40 |
const ox = px
|
| 41 |
const oy = py + PLAYER_EYE_HEIGHT
|
| 42 |
const oz = pz
|
| 43 |
|
|
|
|
| 44 |
const dirX = -Math.sin(yaw) * Math.cos(pitch)
|
| 45 |
const dirY = -Math.sin(pitch)
|
| 46 |
const dirZ = -Math.cos(yaw) * Math.cos(pitch)
|
|
|
|
| 53 |
const stepY = dirY >= 0 ? 1 : -1
|
| 54 |
const stepZ = dirZ >= 0 ? 1 : -1
|
| 55 |
|
| 56 |
+
const eps = 1e-10
|
| 57 |
+
const tDeltaX = Math.abs(1 / (dirX || eps))
|
| 58 |
+
const tDeltaY = Math.abs(1 / (dirY || eps))
|
| 59 |
+
const tDeltaZ = Math.abs(1 / (dirZ || eps))
|
| 60 |
|
| 61 |
+
let tMaxX = dirX >= 0 ? (x + 1 - ox) / (dirX || eps) : (ox - x) / (-dirX || eps)
|
| 62 |
+
let tMaxY = dirY >= 0 ? (y + 1 - oy) / (dirY || eps) : (oy - y) / (-dirY || eps)
|
| 63 |
+
let tMaxZ = dirZ >= 0 ? (z + 1 - oz) / (dirZ || eps) : (oz - z) / (-dirZ || eps)
|
| 64 |
|
| 65 |
let prevX = x, prevY = y, prevZ = z
|
| 66 |
const { getBlock } = useWorldStore.getState()
|
|
|
|
| 69 |
const block = getBlock(x, y, z)
|
| 70 |
if (block !== BLOCK_AIR) {
|
| 71 |
const def = BLOCK_DEFS[block]
|
| 72 |
+
if (def && (def.solid || def.transparent)) {
|
| 73 |
return {
|
| 74 |
blockPos: [x, y, z] as [number, number, number],
|
| 75 |
placePos: [prevX, prevY, prevZ] as [number, number, number],
|
|
|
|
| 113 |
case 'KeyD': keys.current.right = true; break
|
| 114 |
case 'Space': keys.current.jump = true; e.preventDefault(); break
|
| 115 |
case 'ShiftLeft': case 'ShiftRight': keys.current.sneak = true; break
|
| 116 |
+
case 'ControlLeft': keys.current.sprint = true; e.preventDefault(); break
|
| 117 |
case 'Escape':
|
| 118 |
if (isPointerLocked.current) {
|
| 119 |
document.exitPointerLock()
|
|
|
|
| 131 |
e.preventDefault()
|
| 132 |
useGameStore.getState().setDebug(!useGameStore.getState().debug)
|
| 133 |
break
|
| 134 |
+
case 'KeyG':
|
| 135 |
+
const current = usePlayerStore.getState().player?.gamemode || 'creative'
|
| 136 |
+
usePlayerStore.getState().setGamemode(current === 'creative' ? 'survival' : 'creative')
|
| 137 |
+
break
|
| 138 |
+
case 'KeyT':
|
| 139 |
+
const w = useWorldStore.getState().world
|
| 140 |
+
if (w) useWorldStore.getState().initWorld(w.seed) // Reset time by re-init
|
| 141 |
+
break
|
| 142 |
case 'Digit1': case 'Digit2': case 'Digit3': case 'Digit4': case 'Digit5':
|
| 143 |
case 'Digit6': case 'Digit7': case 'Digit8': case 'Digit9':
|
| 144 |
useUIStore.getState().setHotbarSlot(parseInt(e.code.slice(5)) - 1)
|
|
|
|
| 162 |
if (!isPointerLocked.current) return
|
| 163 |
const { player, updateRotation } = usePlayerStore.getState()
|
| 164 |
if (!player) return
|
| 165 |
+
const sensitivity = 0.002
|
| 166 |
const yaw = player.rotation[0] - e.movementX * sensitivity
|
| 167 |
const pitch = Math.max(-Math.PI / 2 + 0.01,
|
| 168 |
Math.min(Math.PI / 2 - 0.01, player.rotation[1] - e.movementY * sensitivity))
|
|
|
|
| 173 |
isPointerLocked.current = document.pointerLockElement !== null
|
| 174 |
}, [])
|
| 175 |
|
|
|
|
| 176 |
const handleMouseDown = useCallback((e: MouseEvent) => {
|
| 177 |
if (!isPointerLocked.current) return
|
| 178 |
e.preventDefault()
|
|
|
|
| 190 |
// Right click = place block
|
| 191 |
const slot = useUIStore.getState().hotbarSlot
|
| 192 |
const item = player.inventory.hotbar[slot]
|
| 193 |
+
if (item && BLOCK_DEFS[item.id]) {
|
| 194 |
setBlock(hit.placePos[0], hit.placePos[1], hit.placePos[2], item.id)
|
| 195 |
}
|
| 196 |
}
|
|
|
|
| 222 |
document.addEventListener('contextmenu', handleContextMenu)
|
| 223 |
document.addEventListener('wheel', handleWheel)
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
return () => {
|
| 226 |
document.removeEventListener('keydown', handleKeyDown)
|
| 227 |
document.removeEventListener('keyup', handleKeyUp)
|
|
|
|
| 230 |
document.removeEventListener('mousedown', handleMouseDown)
|
| 231 |
document.removeEventListener('contextmenu', handleContextMenu)
|
| 232 |
document.removeEventListener('wheel', handleWheel)
|
|
|
|
| 233 |
}
|
| 234 |
}, [])
|
| 235 |
|
|
|
|
| 264 |
if (keys.right) { moveX += right[0]; moveZ += right[1] }
|
| 265 |
|
| 266 |
// Add mobile movement
|
| 267 |
+
if ((window as any).__MC_MOBILE_MOVE) {
|
| 268 |
+
moveX += (window as any).__MC_MOBILE_MOVE.x
|
| 269 |
+
moveZ += (window as any).__MC_MOBILE_MOVE.z
|
| 270 |
}
|
| 271 |
|
| 272 |
const len = Math.sqrt(moveX * moveX + moveZ * moveZ)
|
|
|
|
| 278 |
let [vx, vy, vz] = player.velocity
|
| 279 |
let [px, py, pz] = player.position
|
| 280 |
|
| 281 |
+
const jumping = keys.jump || (window as any).__MC_MOBILE_JUMP
|
| 282 |
+
const sneaking = keys.sneak || (window as any).__MC_MOBILE_SNEAK
|
| 283 |
|
| 284 |
if (player.flying) {
|
| 285 |
vx = moveX; vz = moveZ; vy = 0
|
|
|
|
| 290 |
vy -= GRAVITY * delta
|
| 291 |
if (jumping && player.onGround) vy = JUMP_VELOCITY
|
| 292 |
vy = Math.max(vy, -78.4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
}
|
| 294 |
|
| 295 |
+
// Apply velocity
|
| 296 |
px += vx * delta
|
| 297 |
py += vy * delta
|
| 298 |
pz += vz * delta
|
| 299 |
|
| 300 |
+
// Collision detection
|
| 301 |
+
const { getBlock } = useWorldStore.getState()
|
| 302 |
+
|
| 303 |
+
if (!player.flying) {
|
| 304 |
+
// Ground collision
|
| 305 |
+
const blockBelow = getBlock(Math.floor(px), Math.floor(py - 0.05), Math.floor(pz))
|
| 306 |
+
const defBelow = BLOCK_DEFS[blockBelow]
|
| 307 |
+
if (defBelow && defBelow.solid && vy < 0) {
|
| 308 |
+
// Snap to top of block
|
| 309 |
+
const groundY = Math.floor(py - 0.05) + 1
|
| 310 |
+
if (py - groundY < 0.5) {
|
| 311 |
+
py = groundY
|
| 312 |
+
vy = 0
|
| 313 |
+
}
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
// Head collision
|
| 317 |
+
const blockAbove = getBlock(Math.floor(px), Math.floor(py + PLAYER_HEIGHT + 0.1), Math.floor(pz))
|
| 318 |
+
const defAbove = BLOCK_DEFS[blockAbove]
|
| 319 |
+
if (defAbove && defAbove.solid && vy > 0) {
|
| 320 |
+
vy = 0
|
| 321 |
+
}
|
| 322 |
}
|
| 323 |
|
| 324 |
+
// X/Z collision
|
|
|
|
| 325 |
const playerWidth = 0.3
|
| 326 |
+
const feetY = Math.floor(py)
|
| 327 |
+
const headY = Math.floor(py + PLAYER_HEIGHT)
|
| 328 |
+
|
| 329 |
+
for (const [dx, dz] of [[-1, 0], [1, 0], [0, -1], [0, 1]] as [number, number][]) {
|
| 330 |
+
for (let checkY = feetY; checkY <= headY; checkY++) {
|
| 331 |
+
const checkX = Math.floor(px + dx * playerWidth)
|
| 332 |
+
const checkZ = Math.floor(pz + dz * playerWidth)
|
| 333 |
+
const block = getBlock(checkX, checkY, checkZ)
|
| 334 |
if (BLOCK_DEFS[block]?.solid) {
|
| 335 |
+
if (dx !== 0) px -= vx * delta
|
| 336 |
+
if (dz !== 0) pz -= vz * delta
|
| 337 |
+
break
|
| 338 |
}
|
| 339 |
}
|
| 340 |
}
|
| 341 |
|
| 342 |
+
// World bounds
|
| 343 |
+
if (py < MIN_BUILD_HEIGHT + PLAYER_HEIGHT) {
|
| 344 |
+
py = MIN_BUILD_HEIGHT + PLAYER_HEIGHT; vy = 0
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
// Fall damage (survival only)
|
| 348 |
+
const onGround = !!(BLOCK_DEFS[getBlock(Math.floor(px), Math.floor(py - 0.05), Math.floor(pz))]?.solid)
|
| 349 |
+
|
| 350 |
+
if (player.gamemode === 'survival' && !player.flying && player.onGround === false && onGround && vy < -15) {
|
| 351 |
+
const fallDistance = (vy * delta) * -1
|
| 352 |
+
if (fallDistance > 3) {
|
| 353 |
+
store.damage(Math.floor(fallDistance - 3))
|
| 354 |
+
}
|
| 355 |
+
}
|
| 356 |
|
| 357 |
store.updatePosition([px, py, pz])
|
| 358 |
store.updateVelocity([vx, vy, vz])
|