TomatitoToho commited on
Commit
72553b6
·
verified ·
1 Parent(s): 8a2b8bc

Upload src/hooks/usePlayerControls.ts with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/hooks/usePlayerControls.ts +349 -0
src/hooks/usePlayerControls.ts ADDED
@@ -0,0 +1,349 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 'use client'
2
+
3
+ import { useEffect, useRef, useCallback } from 'react'
4
+ import { usePlayerStore } from '@/stores/playerStore'
5
+ import { useGameStore } from '@/stores/gameStore'
6
+ import { useUIStore } from '@/stores/uiStore'
7
+ import { useWorldStore } from '@/stores/worldStore'
8
+ import {
9
+ WALK_SPEED, SPRINT_SPEED, FLY_SPEED, SNEAK_SPEED,
10
+ GRAVITY, JUMP_VELOCITY, PLAYER_HEIGHT, PLAYER_EYE_HEIGHT,
11
+ SEA_LEVEL, MIN_BUILD_HEIGHT, REACH_DISTANCE,
12
+ BLOCK_AIR, BLOCK_DEFS, CREATIVE_BLOCKS,
13
+ } from '@/engine/constants'
14
+
15
+ interface KeyState {
16
+ forward: boolean
17
+ backward: boolean
18
+ left: boolean
19
+ right: boolean
20
+ jump: boolean
21
+ sneak: boolean
22
+ sprint: boolean
23
+ }
24
+
25
+ export function usePlayerControls() {
26
+ const keys = useRef<KeyState>({
27
+ forward: false, backward: false, left: false, right: false,
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) => {
35
+ const { player } = usePlayerStore.getState()
36
+ if (!player) return null
37
+
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)
50
+
51
+ let x = Math.floor(ox)
52
+ let y = Math.floor(oy)
53
+ let z = Math.floor(oz)
54
+
55
+ const stepX = dirX >= 0 ? 1 : -1
56
+ const stepY = dirY >= 0 ? 1 : -1
57
+ const stepZ = dirZ >= 0 ? 1 : -1
58
+
59
+ const tDeltaX = Math.abs(1 / (dirX || 0.0001))
60
+ const tDeltaY = Math.abs(1 / (dirY || 0.0001))
61
+ const tDeltaZ = Math.abs(1 / (dirZ || 0.0001))
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()
69
+
70
+ for (let i = 0; i < maxDist * 3; i++) {
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],
78
+ blockId: block,
79
+ }
80
+ }
81
+ }
82
+
83
+ prevX = x; prevY = y; prevZ = z
84
+
85
+ if (tMaxX < tMaxY) {
86
+ if (tMaxX < tMaxZ) {
87
+ if (tMaxX > maxDist) return null
88
+ x += stepX; tMaxX += tDeltaX
89
+ } else {
90
+ if (tMaxZ > maxDist) return null
91
+ z += stepZ; tMaxZ += tDeltaZ
92
+ }
93
+ } else {
94
+ if (tMaxY < tMaxZ) {
95
+ if (tMaxY > maxDist) return null
96
+ y += stepY; tMaxY += tDeltaY
97
+ } else {
98
+ if (tMaxZ > maxDist) return null
99
+ z += stepZ; tMaxZ += tDeltaZ
100
+ }
101
+ }
102
+ }
103
+
104
+ return null
105
+ }, [])
106
+
107
+ const handleKeyDown = useCallback((e: KeyboardEvent) => {
108
+ const { setShowPause } = useUIStore.getState()
109
+ const { setPaused } = useGameStore.getState()
110
+
111
+ switch (e.code) {
112
+ case 'KeyW': keys.current.forward = true; break
113
+ case 'KeyS': keys.current.backward = true; break
114
+ case 'KeyA': keys.current.left = true; break
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()
122
+ } else {
123
+ setShowPause(true); setPaused(true)
124
+ }
125
+ break
126
+ case 'KeyF':
127
+ usePlayerStore.getState().setFlying(!usePlayerStore.getState().player?.flying)
128
+ break
129
+ case 'KeyE':
130
+ useUIStore.getState().toggleInventory()
131
+ break
132
+ case 'F3':
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)
139
+ break
140
+ }
141
+ }, [])
142
+
143
+ const handleKeyUp = useCallback((e: KeyboardEvent) => {
144
+ switch (e.code) {
145
+ case 'KeyW': keys.current.forward = false; break
146
+ case 'KeyS': keys.current.backward = false; break
147
+ case 'KeyA': keys.current.left = false; break
148
+ case 'KeyD': keys.current.right = false; break
149
+ case 'Space': keys.current.jump = false; break
150
+ case 'ShiftLeft': case 'ShiftRight': keys.current.sneak = false; break
151
+ case 'ControlLeft': keys.current.sprint = false; break
152
+ }
153
+ }, [])
154
+
155
+ const handleMouseMove = useCallback((e: MouseEvent) => {
156
+ if (!isPointerLocked.current) return
157
+ const { player, updateRotation } = usePlayerStore.getState()
158
+ if (!player) return
159
+ const sensitivity = useGameStore.getState().debug ? 0.003 : 0.002
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))
163
+ updateRotation([yaw, pitch])
164
+ }, [])
165
+
166
+ const handlePointerLockChange = useCallback(() => {
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()
174
+
175
+ const hit = raycastBlock()
176
+ if (!hit) return
177
+
178
+ const { setBlock } = useWorldStore.getState()
179
+ const { player } = usePlayerStore.getState()
180
+
181
+ if (e.button === 0) {
182
+ // Left click = break block
183
+ setBlock(hit.blockPos[0], hit.blockPos[1], hit.blockPos[2], BLOCK_AIR)
184
+ } else if (e.button === 2 && player) {
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]?.solid) {
189
+ setBlock(hit.placePos[0], hit.placePos[1], hit.placePos[2], item.id)
190
+ }
191
+ }
192
+ }, [raycastBlock])
193
+
194
+ const handleContextMenu = useCallback((e: Event) => e.preventDefault(), [])
195
+
196
+ const handleWheel = useCallback((e: WheelEvent) => {
197
+ const { hotbarSlot, setHotbarSlot } = useUIStore.getState()
198
+ if (e.deltaY > 0) {
199
+ setHotbarSlot((hotbarSlot + 1) % 9)
200
+ } else {
201
+ setHotbarSlot((hotbarSlot + 8) % 9)
202
+ }
203
+ }, [])
204
+
205
+ const requestPointerLock = useCallback(() => {
206
+ document.body.requestPointerLock()
207
+ useUIStore.getState().setShowPause(false)
208
+ useGameStore.getState().setPaused(false)
209
+ }, [])
210
+
211
+ useEffect(() => {
212
+ document.addEventListener('keydown', handleKeyDown)
213
+ document.addEventListener('keyup', handleKeyUp)
214
+ document.addEventListener('mousemove', handleMouseMove)
215
+ document.addEventListener('pointerlockchange', handlePointerLockChange)
216
+ document.addEventListener('mousedown', handleMouseDown)
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)
228
+ document.removeEventListener('mousemove', handleMouseMove)
229
+ document.removeEventListener('pointerlockchange', handlePointerLockChange)
230
+ document.removeEventListener('mousedown', handleMouseDown)
231
+ document.removeEventListener('contextmenu', handleContextMenu)
232
+ document.removeEventListener('wheel', handleWheel)
233
+ document.removeEventListener('click', handleClick)
234
+ }
235
+ }, [])
236
+
237
+ return { keys, isPointerLocked, requestPointerLock, raycastBlock }
238
+ }
239
+
240
+ /**
241
+ * Update player position every frame
242
+ */
243
+ export function updatePlayerMovement(keys: KeyState, delta: number) {
244
+ const store = usePlayerStore.getState()
245
+ const player = store.player
246
+ if (!player) return
247
+
248
+ const { paused } = useGameStore.getState()
249
+ if (paused) return
250
+
251
+ const [yaw, pitch] = player.rotation
252
+ const speed = player.flying ? FLY_SPEED
253
+ : player.sprinting ? SPRINT_SPEED
254
+ : player.sneaking ? SNEAK_SPEED
255
+ : WALK_SPEED
256
+
257
+ // Keyboard movement
258
+ const forward = [-Math.sin(yaw), -Math.cos(yaw)]
259
+ const right = [Math.cos(yaw), -Math.sin(yaw)]
260
+
261
+ let moveX = 0, moveZ = 0
262
+ if (keys.forward) { moveX += forward[0]; moveZ += forward[1] }
263
+ if (keys.backward) { moveX -= forward[0]; moveZ -= forward[1] }
264
+ if (keys.left) { moveX -= right[0]; moveZ -= right[1] }
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)
274
+ if (len > 0) {
275
+ moveX = (moveX / len) * speed
276
+ moveZ = (moveZ / len) * speed
277
+ }
278
+
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
287
+ if (jumping) vy = speed
288
+ if (sneaking) vy = -speed
289
+ } else {
290
+ vx = moveX; vz = moveZ
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
+ if (py < MIN_BUILD_HEIGHT + PLAYER_HEIGHT) {
312
+ py = MIN_BUILD_HEIGHT + PLAYER_HEIGHT; vy = 0
313
+ }
314
+
315
+ // Simple X/Z collision
316
+ const { getBlock } = useWorldStore.getState()
317
+ const playerWidth = 0.3
318
+ for (const [dx, dz] of [[-1,0],[1,0],[0,-1],[0,1]]) {
319
+ const checkX = px + dx * playerWidth
320
+ const checkZ = pz + dz * playerWidth
321
+ for (let checkY = Math.floor(py); checkY < Math.floor(py + PLAYER_HEIGHT); checkY++) {
322
+ const block = getBlock(Math.floor(checkX), checkY, Math.floor(checkZ))
323
+ if (BLOCK_DEFS[block]?.solid) {
324
+ // Push back
325
+ if (dx !== 0) px -= vx * delta * 0.5
326
+ if (dz !== 0) pz -= vz * delta * 0.5
327
+ }
328
+ }
329
+ }
330
+
331
+ const onGround = BLOCK_DEFS[getBlock(Math.floor(px), Math.floor(py - 0.1), Math.floor(pz))]?.solid ?? false
332
+
333
+ store.updatePosition([px, py, pz])
334
+ store.updateVelocity([vx, vy, vz])
335
+ if (player.onGround !== onGround) {
336
+ const p = usePlayerStore.getState().player
337
+ if (p) usePlayerStore.setState({ player: { ...p, onGround } })
338
+ }
339
+ }
340
+
341
+ declare global {
342
+ interface Window {
343
+ __MC_MOBILE_MOVE: { x: number; z: number } | null
344
+ __MC_MOBILE_JUMP: boolean
345
+ __MC_MOBILE_SNEAK: boolean
346
+ __MC_MOBILE_BREAK: boolean
347
+ __MC_MOBILE_PLACE: boolean
348
+ }
349
+ }