TomatitoToho commited on
Commit
78691c3
·
verified ·
1 Parent(s): 6f8bf09

Upload src/components/ui/MobileControls.tsx with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/components/ui/MobileControls.tsx +112 -139
src/components/ui/MobileControls.tsx CHANGED
@@ -1,168 +1,147 @@
1
  'use client'
2
 
3
- import { useEffect, useRef, useState, useCallback } from 'react'
4
-
5
- interface TouchState {
6
- moveX: number // -1 to 1
7
- moveZ: number // -1 to 1
8
- lookX: number // delta
9
- lookY: number // delta
10
- jumping: boolean
11
- sneaking: boolean
12
- breaking: boolean
13
- placing: boolean
14
- }
15
-
16
- export function useMobileControls() {
17
- const touchState = useRef<TouchState>({
18
- moveX: 0, moveZ: 0, lookX: 0, lookY: 0,
19
- jumping: false, sneaking: false, breaking: false, placing: false,
20
- })
21
-
22
- const isMobile = useRef(false)
23
-
24
- useEffect(() => {
25
- isMobile.current = 'ontouchstart' in window || navigator.maxTouchPoints > 0
26
- }, [])
27
-
28
- return { touchState, isMobile }
29
- }
30
 
31
  export function MobileControls() {
32
- const [isMobile, setIsMobile] = useState(false)
33
- const moveTouch = useRef<number | null>(null)
34
- const lookTouch = useRef<number | null>(null)
35
- const moveStart = useRef<{ x: number; y: number }>({ x: 0, y: 0 })
36
- const lookStart = useRef<{ x: number; y: number }>({ x: 0, y: 0 })
37
- const moveBase = useRef<{ x: number; y: number }>({ x: 0, y: 0 })
38
-
39
- useEffect(() => {
40
- setIsMobile('ontouchstart' in window || navigator.maxTouchPoints > 0)
 
 
 
 
 
 
41
  }, [])
42
 
43
- const handleTouchStart = useCallback((e: React.TouchEvent) => {
44
  e.preventDefault()
45
  for (let i = 0; i < e.changedTouches.length; i++) {
46
  const touch = e.changedTouches[i]
47
- const x = touch.clientX
48
- const y = touch.clientY
49
-
50
- // Left half = movement joystick
51
- if (x < window.innerWidth / 2 && moveTouch.current === null) {
52
- moveTouch.current = touch.identifier
53
- moveStart.current = { x, y }
54
- moveBase.current = { x, y }
55
- }
56
- // Right half = look
57
- else if (x >= window.innerWidth / 2 && lookTouch.current === null) {
58
- lookTouch.current = touch.identifier
59
- lookStart.current = { x, y }
60
  }
61
  }
62
  }, [])
63
 
64
- const handleTouchMove = useCallback((e: React.TouchEvent) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  e.preventDefault()
66
  for (let i = 0; i < e.changedTouches.length; i++) {
67
  const touch = e.changedTouches[i]
 
 
 
 
68
 
69
- if (touch.identifier === moveTouch.current) {
70
- const dx = (touch.clientX - moveBase.current.x) / 50
71
- const dy = (touch.clientY - moveBase.current.y) / 50
72
- // Update move state via store
73
- const { player, updateVelocity } = usePlayerStore?.getState?.() || {}
74
  if (player) {
75
- const speed = player.flying ? 10.89 : player.sprinting ? 5.612 : 4.317
76
- const len = Math.sqrt(dx * dx + dy * dy)
77
- if (len > 0) {
78
- const [yaw] = player.rotation
79
- const forward = [-Math.sin(yaw), -Math.cos(yaw)]
80
- const right = [Math.cos(yaw), -Math.sin(yaw)]
81
- const norm = Math.min(len, 1)
82
- const mx = (forward[0] * dy + right[0] * dx) / len * speed * norm
83
- const mz = (forward[1] * dy + right[1] * dx) / len * speed * norm
84
- // Store movement for game loop
85
- window.__MC_MOBILE_MOVE = { x: mx, z: mz }
86
- }
87
- }
88
- }
89
-
90
- if (touch.identifier === lookTouch.current) {
91
- const dx = touch.clientX - lookStart.current.x
92
- const dy = touch.clientY - lookStart.current.y
93
- lookStart.current = { x: touch.clientX, y: touch.clientY }
94
-
95
- // Update rotation
96
- const { player, updateRotation } = usePlayerStore?.getState?.() || {}
97
- if (player) {
98
- const sensitivity = 0.003
99
  const yaw = player.rotation[0] - dx * sensitivity
100
  const pitch = Math.max(-Math.PI / 2 + 0.01,
101
  Math.min(Math.PI / 2 - 0.01, player.rotation[1] - dy * sensitivity))
102
- updateRotation([yaw, pitch])
103
  }
104
  }
105
  }
106
  }, [])
107
 
108
- const handleTouchEnd = useCallback((e: React.TouchEvent) => {
109
- for (let i = 0; i < e.changedTouches.length; i++) {
110
- const touch = e.changedTouches[i]
111
- if (touch.identifier === moveTouch.current) {
112
- moveTouch.current = null
113
- window.__MC_MOBILE_MOVE = null
114
- }
115
- if (touch.identifier === lookTouch.current) {
116
- lookTouch.current = null
117
- }
118
- }
 
119
  }, [])
120
 
121
- if (!isMobile) return null
 
 
122
 
123
  return (
124
- <div
125
- className="absolute inset-0 z-40 touch-none"
126
- onTouchStart={handleTouchStart}
127
- onTouchMove={handleTouchMove}
128
- onTouchEnd={handleTouchEnd}
129
- >
130
- {/* Left joystick area indicator */}
131
- <div className="absolute bottom-20 left-8 w-28 h-28 rounded-full border-2 border-white/20 flex items-center justify-center">
132
- <div className="w-12 h-12 rounded-full bg-white/15" />
 
 
 
133
  </div>
134
 
135
- {/* Right side buttons */}
136
- <div className="absolute bottom-20 right-8 flex flex-col gap-3">
137
- <button
138
- className="w-14 h-14 rounded-full bg-white/15 border border-white/20 flex items-center justify-center text-white text-lg active:bg-white/30"
139
- onTouchStart={(e) => { e.stopPropagation(); window.__MC_MOBILE_JUMP = true }}
140
- onTouchEnd={() => { window.__MC_MOBILE_JUMP = false }}
141
- >
142
- ▲
143
- </button>
 
 
 
 
 
 
 
144
  <button
145
- className="w-14 h-14 rounded-full bg-white/15 border border-white/20 flex items-center justify-center text-white text-lg active:bg-white/30"
146
- onTouchStart={(e) => { e.stopPropagation(); window.__MC_MOBILE_SNEAK = true }}
147
- onTouchEnd={() => { window.__MC_MOBILE_SNEAK = false }}
148
  >
149
- ▼
150
  </button>
151
  </div>
152
 
153
  {/* Break/Place buttons */}
154
- <div className="absolute bottom-20 right-32 flex gap-2">
155
  <button
156
- className="w-12 h-12 rounded-lg bg-red-500/30 border border-red-400/30 flex items-center justify-center text-white text-xs active:bg-red-500/50"
157
- onTouchStart={(e) => { e.stopPropagation(); window.__MC_MOBILE_BREAK = true }}
158
- onTouchEnd={() => { window.__MC_MOBILE_BREAK = false }}
159
  >
160
  ⛏
161
  </button>
162
  <button
163
- className="w-12 h-12 rounded-lg bg-green-500/30 border border-green-400/30 flex items-center justify-center text-white text-xs active:bg-green-500/50"
164
- onTouchStart={(e) => { e.stopPropagation(); window.__MC_MOBILE_PLACE = true }}
165
- onTouchEnd={() => { window.__MC_MOBILE_PLACE = false }}
166
  >
167
  🧱
168
  </button>
@@ -170,28 +149,22 @@ export function MobileControls() {
170
 
171
  {/* Fly toggle */}
172
  <button
173
- className="absolute top-16 right-4 px-3 py-2 rounded bg-white/10 border border-white/20 text-white text-xs active:bg-white/20"
174
  onClick={() => {
175
- const { player, setFlying } = usePlayerStore?.getState?.() || {}
176
- if (player) setFlying(!player.flying)
177
  }}
178
  >
179
  ✈ Fly
180
  </button>
 
 
 
 
 
 
 
 
181
  </div>
182
  )
183
  }
184
-
185
- // Import at bottom to avoid circular deps
186
- import { usePlayerStore } from '@/stores/playerStore'
187
-
188
- // Global mobile state
189
- declare global {
190
- interface Window {
191
- __MC_MOBILE_MOVE: { x: number; z: number } | null
192
- __MC_MOBILE_JUMP: boolean
193
- __MC_MOBILE_SNEAK: boolean
194
- __MC_MOBILE_BREAK: boolean
195
- __MC_MOBILE_PLACE: boolean
196
- }
197
- }
 
1
  'use client'
2
 
3
+ import { useCallback, useRef, useEffect, useState } from 'react'
4
+ import { usePlayerStore } from '@/stores/playerStore'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  export function MobileControls() {
7
+ const [moveActive, setMoveActive] = useState(false)
8
+ const [lookActive, setLookActive] = useState(false)
9
+ const moveRef = useRef<{ x: number; z: number }>({ x: 0, z: 0 })
10
+ const lookRef = useRef<{ lastX: number; lastY: number }>({ lastX: 0, lastY: 0 })
11
+ const moveTouchId = useRef<number | null>(null)
12
+ const lookTouchId = useRef<number | null>(null)
13
+
14
+ // Move joystick
15
+ const handleMoveStart = useCallback((e: React.TouchEvent) => {
16
+ e.preventDefault()
17
+ const touch = e.changedTouches[0]
18
+ moveTouchId.current = touch.identifier
19
+ lookRef.current.lastX = touch.clientX
20
+ lookRef.current.lastY = touch.clientY
21
+ setMoveActive(true)
22
  }, [])
23
 
24
+ const handleMoveMove = useCallback((e: React.TouchEvent) => {
25
  e.preventDefault()
26
  for (let i = 0; i < e.changedTouches.length; i++) {
27
  const touch = e.changedTouches[i]
28
+ if (touch.identifier === moveTouchId.current) {
29
+ const dx = (touch.clientX - lookRef.current.lastX) / 40
30
+ const dz = (touch.clientY - lookRef.current.lastY) / 40
31
+ moveRef.current = {
32
+ x: Math.max(-1, Math.min(1, dx)),
33
+ z: Math.max(-1, Math.min(1, dz)),
34
+ }
35
+ ;(window as any).__MC_MOBILE_MOVE = moveRef.current
 
 
 
 
 
36
  }
37
  }
38
  }, [])
39
 
40
+ const handleMoveEnd = useCallback(() => {
41
+ moveTouchId.current = null
42
+ moveRef.current = { x: 0, z: 0 }
43
+ ;(window as any).__MC_MOBILE_MOVE = null
44
+ setMoveActive(false)
45
+ }, [])
46
+
47
+ // Look joystick
48
+ const handleLookStart = useCallback((e: React.TouchEvent) => {
49
+ e.preventDefault()
50
+ const touch = e.changedTouches[0]
51
+ lookTouchId.current = touch.identifier
52
+ lookRef.current = { lastX: touch.clientX, lastY: touch.clientY }
53
+ setLookActive(true)
54
+ }, [])
55
+
56
+ const handleLookMove = useCallback((e: React.TouchEvent) => {
57
  e.preventDefault()
58
  for (let i = 0; i < e.changedTouches.length; i++) {
59
  const touch = e.changedTouches[i]
60
+ if (touch.identifier === lookTouchId.current) {
61
+ const dx = touch.clientX - lookRef.current.lastX
62
+ const dy = touch.clientY - lookRef.current.lastY
63
+ lookRef.current = { lastX: touch.clientX, lastY: touch.clientY }
64
 
65
+ const player = usePlayerStore.getState().player
 
 
 
 
66
  if (player) {
67
+ const sensitivity = 0.004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  const yaw = player.rotation[0] - dx * sensitivity
69
  const pitch = Math.max(-Math.PI / 2 + 0.01,
70
  Math.min(Math.PI / 2 - 0.01, player.rotation[1] - dy * sensitivity))
71
+ usePlayerStore.getState().updateRotation([yaw, pitch])
72
  }
73
  }
74
  }
75
  }, [])
76
 
77
+ const handleLookEnd = useCallback(() => {
78
+ lookTouchId.current = null
79
+ setLookActive(false)
80
+ }, [])
81
+
82
+ const handleJump = useCallback(() => {
83
+ ;(window as any).__MC_MOBILE_JUMP = true
84
+ setTimeout(() => { (window as any).__MC_MOBILE_JUMP = false }, 100)
85
+ }, [])
86
+
87
+ const handleBreak = useCallback(() => {
88
+ ;(window as any).__MC_MOBILE_BREAK = true
89
  }, [])
90
 
91
+ const handlePlace = useCallback(() => {
92
+ ;(window as any).__MC_MOBILE_PLACE = true
93
+ }, [])
94
 
95
  return (
96
+ <div className="absolute inset-0 z-20 pointer-events-none">
97
+ {/* Move joystick - left side */}
98
+ <div
99
+ className="absolute bottom-16 left-4 w-28 h-28 rounded-full border-2 border-white/30 bg-white/10 pointer-events-auto touch-none"
100
+ onTouchStart={handleMoveStart}
101
+ onTouchMove={handleMoveMove}
102
+ onTouchEnd={handleMoveEnd}
103
+ >
104
+ <div className={`absolute inset-0 flex items-center justify-center text-white/50 text-xs
105
+ ${moveActive ? 'text-green-400/80' : ''}`}>
106
+ {moveActive ? '●' : 'Move'}
107
+ </div>
108
  </div>
109
 
110
+ {/* Look area - right side */}
111
+ <div
112
+ className="absolute bottom-16 right-4 w-28 h-28 rounded-full border-2 border-white/30 bg-white/10 pointer-events-auto touch-none"
113
+ onTouchStart={handleLookStart}
114
+ onTouchMove={handleLookMove}
115
+ onTouchEnd={handleLookEnd}
116
+ >
117
+ <div className={`absolute inset-0 flex items-center justify-center text-white/50 text-xs
118
+ ${lookActive ? 'text-blue-400/80' : ''}`}>
119
+ {lookActive ? '●' : 'Look'}
120
+ </div>
121
+ </div>
122
+
123
+ {/* Action buttons */}
124
+ <div className="absolute bottom-16 left-1/2 -translate-x-1/2 flex gap-3 pointer-events-auto">
125
+ {/* Jump */}
126
  <button
127
+ className="w-12 h-12 rounded-full bg-white/15 border border-white/30 flex items-center justify-center text-white/70 active:bg-white/30"
128
+ onTouchStart={handleJump}
 
129
  >
130
+ ⬆
131
  </button>
132
  </div>
133
 
134
  {/* Break/Place buttons */}
135
+ <div className="absolute bottom-32 right-6 flex flex-col gap-2 pointer-events-auto">
136
  <button
137
+ className="w-14 h-14 rounded-xl bg-red-500/30 border border-red-400/50 flex items-center justify-center text-white text-xs active:bg-red-500/50"
138
+ onTouchStart={handleBreak}
 
139
  >
140
  ⛏
141
  </button>
142
  <button
143
+ className="w-14 h-14 rounded-xl bg-blue-500/30 border border-blue-400/50 flex items-center justify-center text-white text-xs active:bg-blue-500/50"
144
+ onTouchStart={handlePlace}
 
145
  >
146
  🧱
147
  </button>
 
149
 
150
  {/* Fly toggle */}
151
  <button
152
+ className="absolute top-4 right-4 px-3 py-1.5 bg-white/15 border border-white/30 rounded text-white/70 text-xs pointer-events-auto active:bg-white/30"
153
  onClick={() => {
154
+ const { player } = usePlayerStore.getState()
155
+ if (player) usePlayerStore.getState().setFlying(!player.flying)
156
  }}
157
  >
158
  ✈ Fly
159
  </button>
160
+
161
+ {/* Inventory */}
162
+ <button
163
+ className="absolute top-4 right-20 px-3 py-1.5 bg-white/15 border border-white/30 rounded text-white/70 text-xs pointer-events-auto active:bg-white/30"
164
+ onClick={() => usePlayerStore.getState().setGamemode('creative')}
165
+ >
166
+ 📦 Inv
167
+ </button>
168
  </div>
169
  )
170
  }