create-rspeedy 0.13.5 → 0.13.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/package.json +4 -4
- package/template-react-js/src/App.jsx +8 -2
- package/template-react-js/src/lib/flappy.js +58 -0
- package/template-react-js/src/useFlappy.js +37 -0
- package/template-react-ts/src/App.tsx +8 -2
- package/template-react-ts/src/lib/flappy.ts +76 -0
- package/template-react-ts/src/useFlappy.ts +49 -0
- package/template-react-vitest-rltl-js/src/App.jsx +8 -2
- package/template-react-vitest-rltl-js/src/__tests__/index.test.jsx +1 -0
- package/template-react-vitest-rltl-js/src/lib/flappy.js +58 -0
- package/template-react-vitest-rltl-js/src/useFlappy.js +37 -0
- package/template-react-vitest-rltl-ts/src/App.tsx +8 -2
- package/template-react-vitest-rltl-ts/src/__tests__/index.test.tsx +1 -0
- package/template-react-vitest-rltl-ts/src/lib/flappy.ts +76 -0
- package/template-react-vitest-rltl-ts/src/useFlappy.ts +49 -0
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-rspeedy",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.6",
|
|
4
4
|
"description": "Create Rspeedy-powered ReactLynx apps with one command",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"webpack",
|
|
@@ -36,9 +36,9 @@
|
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@rsbuild/plugin-type-check": "1.3.4",
|
|
38
38
|
"@lynx-js/qrcode-rsbuild-plugin": "^0.4.6",
|
|
39
|
-
"@lynx-js/react": "^0.
|
|
40
|
-
"@lynx-js/react-rsbuild-plugin": "^0.
|
|
41
|
-
"@lynx-js/rspeedy": "^0.13.
|
|
39
|
+
"@lynx-js/react": "^0.117.0",
|
|
40
|
+
"@lynx-js/react-rsbuild-plugin": "^0.13.0",
|
|
41
|
+
"@lynx-js/rspeedy": "^0.13.6"
|
|
42
42
|
},
|
|
43
43
|
"engines": {
|
|
44
44
|
"node": ">=18"
|
|
@@ -4,9 +4,11 @@ import './App.css'
|
|
|
4
4
|
import arrow from './assets/arrow.png'
|
|
5
5
|
import lynxLogo from './assets/lynx-logo.png'
|
|
6
6
|
import reactLynxLogo from './assets/react-logo.png'
|
|
7
|
+
import { useFlappy } from './useFlappy.js'
|
|
7
8
|
|
|
8
9
|
export function App() {
|
|
9
10
|
const [alterLogo, setAlterLogo] = useState(false)
|
|
11
|
+
const [logoY, jump] = useFlappy()
|
|
10
12
|
|
|
11
13
|
useEffect(() => {
|
|
12
14
|
console.info('Hello, ReactLynx')
|
|
@@ -18,11 +20,15 @@ export function App() {
|
|
|
18
20
|
}, [])
|
|
19
21
|
|
|
20
22
|
return (
|
|
21
|
-
<view>
|
|
23
|
+
<view bindtap={jump}>
|
|
22
24
|
<view className='Background' />
|
|
23
25
|
<view className='App'>
|
|
24
26
|
<view className='Banner'>
|
|
25
|
-
<view
|
|
27
|
+
<view
|
|
28
|
+
className='Logo'
|
|
29
|
+
style={{ transform: `translateY(${logoY}px)` }}
|
|
30
|
+
bindtap={onTap}
|
|
31
|
+
>
|
|
26
32
|
{alterLogo
|
|
27
33
|
? <image src={reactLynxLogo} className='Logo--react' />
|
|
28
34
|
: <image src={lynxLogo} className='Logo--lynx' />}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic flappy-bird physics engine.
|
|
3
|
+
*
|
|
4
|
+
* Manages gravity, jump impulse, and a 60fps game loop.
|
|
5
|
+
* Wire it up to any UI framework by calling `jump()` on tap
|
|
6
|
+
* and reading `getY()` in the loop callback.
|
|
7
|
+
*
|
|
8
|
+
* @param {(y: number) => void} onUpdate
|
|
9
|
+
* @param {object} [options]
|
|
10
|
+
* @param {number} [options.gravity=0.6]
|
|
11
|
+
* @param {number} [options.jumpForce=-12]
|
|
12
|
+
* @param {number} [options.stackFactor=0.6]
|
|
13
|
+
* @param {number} [options.frameMs=16]
|
|
14
|
+
* @returns {{ jump: () => void, getY: () => number, destroy: () => void }}
|
|
15
|
+
*/
|
|
16
|
+
export function createFlappy(onUpdate, options = {}) {
|
|
17
|
+
const {
|
|
18
|
+
gravity = 0.6,
|
|
19
|
+
jumpForce = -12,
|
|
20
|
+
stackFactor = 0.6,
|
|
21
|
+
frameMs = 16,
|
|
22
|
+
} = options
|
|
23
|
+
|
|
24
|
+
let y = 0
|
|
25
|
+
let velocity = 0
|
|
26
|
+
let timer = null
|
|
27
|
+
|
|
28
|
+
function loop() {
|
|
29
|
+
velocity += gravity
|
|
30
|
+
y += velocity
|
|
31
|
+
if (y >= 0) {
|
|
32
|
+
y = 0
|
|
33
|
+
velocity = 0
|
|
34
|
+
timer = null
|
|
35
|
+
onUpdate(y)
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
onUpdate(y)
|
|
39
|
+
timer = setTimeout(loop, frameMs)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function jump() {
|
|
43
|
+
// Stack impulse on rapid taps, clamped to one full jumpForce
|
|
44
|
+
velocity = Math.max(velocity + jumpForce * stackFactor, jumpForce)
|
|
45
|
+
if (!timer) {
|
|
46
|
+
loop()
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function destroy() {
|
|
51
|
+
if (timer) {
|
|
52
|
+
clearTimeout(timer)
|
|
53
|
+
timer = null
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return { jump, getY: () => y, destroy }
|
|
58
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from '@lynx-js/react'
|
|
2
|
+
|
|
3
|
+
import { createFlappy } from './lib/flappy.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* React hook for flappy-bird physics.
|
|
7
|
+
*
|
|
8
|
+
* Returns `[y, jump]` — a state value and a stable callback.
|
|
9
|
+
* The game loop runs automatically; cleanup happens on unmount.
|
|
10
|
+
* Options are read once on mount and not reactive to later changes.
|
|
11
|
+
*
|
|
12
|
+
* @param {object} [options]
|
|
13
|
+
* @returns {[number, () => void]}
|
|
14
|
+
*/
|
|
15
|
+
export function useFlappy(options) {
|
|
16
|
+
const [y, setY] = useState(0)
|
|
17
|
+
const engineRef = useRef(null)
|
|
18
|
+
|
|
19
|
+
if (engineRef.current == null) {
|
|
20
|
+
engineRef.current = createFlappy((newY) => {
|
|
21
|
+
setY(newY)
|
|
22
|
+
}, options)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
return () => {
|
|
27
|
+
engineRef.current?.destroy()
|
|
28
|
+
}
|
|
29
|
+
}, [])
|
|
30
|
+
|
|
31
|
+
const jump = useCallback(() => {
|
|
32
|
+
'background only'
|
|
33
|
+
engineRef.current?.jump()
|
|
34
|
+
}, [])
|
|
35
|
+
|
|
36
|
+
return [y, jump]
|
|
37
|
+
}
|
|
@@ -4,9 +4,11 @@ import './App.css'
|
|
|
4
4
|
import arrow from './assets/arrow.png'
|
|
5
5
|
import lynxLogo from './assets/lynx-logo.png'
|
|
6
6
|
import reactLynxLogo from './assets/react-logo.png'
|
|
7
|
+
import { useFlappy } from './useFlappy.js'
|
|
7
8
|
|
|
8
9
|
export function App() {
|
|
9
10
|
const [alterLogo, setAlterLogo] = useState(false)
|
|
11
|
+
const [logoY, jump] = useFlappy()
|
|
10
12
|
|
|
11
13
|
useEffect(() => {
|
|
12
14
|
console.info('Hello, ReactLynx')
|
|
@@ -18,11 +20,15 @@ export function App() {
|
|
|
18
20
|
}, [])
|
|
19
21
|
|
|
20
22
|
return (
|
|
21
|
-
<view>
|
|
23
|
+
<view bindtap={jump}>
|
|
22
24
|
<view className='Background' />
|
|
23
25
|
<view className='App'>
|
|
24
26
|
<view className='Banner'>
|
|
25
|
-
<view
|
|
27
|
+
<view
|
|
28
|
+
className='Logo'
|
|
29
|
+
style={{ transform: `translateY(${logoY}px)` }}
|
|
30
|
+
bindtap={onTap}
|
|
31
|
+
>
|
|
26
32
|
{alterLogo
|
|
27
33
|
? <image src={reactLynxLogo} className='Logo--react' />
|
|
28
34
|
: <image src={lynxLogo} className='Logo--lynx' />}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic flappy-bird physics engine.
|
|
3
|
+
*
|
|
4
|
+
* Manages gravity, jump impulse, and a 60fps game loop.
|
|
5
|
+
* Wire it up to any UI framework by calling `jump()` on tap
|
|
6
|
+
* and reading `getY()` in the loop callback.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface FlappyOptions {
|
|
10
|
+
/** Downward acceleration per frame (default 0.6) */
|
|
11
|
+
gravity?: number
|
|
12
|
+
/** Upward impulse per tap — negative value (default -12) */
|
|
13
|
+
jumpForce?: number
|
|
14
|
+
/** Impulse stacking factor for rapid taps (default 0.6) */
|
|
15
|
+
stackFactor?: number
|
|
16
|
+
/** Frame interval in ms (default 16 ≈ 60fps) */
|
|
17
|
+
frameMs?: number
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type OnUpdate = (y: number) => void
|
|
21
|
+
|
|
22
|
+
export interface FlappyEngine {
|
|
23
|
+
/** Call on each tap to apply upward impulse. */
|
|
24
|
+
jump(): void
|
|
25
|
+
/** Current Y offset (0 = ground, negative = airborne). */
|
|
26
|
+
getY(): number
|
|
27
|
+
/** Stop the game loop and clean up. */
|
|
28
|
+
destroy(): void
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function createFlappy(
|
|
32
|
+
onUpdate: OnUpdate,
|
|
33
|
+
options: FlappyOptions = {},
|
|
34
|
+
): FlappyEngine {
|
|
35
|
+
const {
|
|
36
|
+
gravity = 0.6,
|
|
37
|
+
jumpForce = -12,
|
|
38
|
+
stackFactor = 0.6,
|
|
39
|
+
frameMs = 16,
|
|
40
|
+
} = options
|
|
41
|
+
|
|
42
|
+
let y = 0
|
|
43
|
+
let velocity = 0
|
|
44
|
+
let timer: ReturnType<typeof setTimeout> | null = null
|
|
45
|
+
|
|
46
|
+
function loop() {
|
|
47
|
+
velocity += gravity
|
|
48
|
+
y += velocity
|
|
49
|
+
if (y >= 0) {
|
|
50
|
+
y = 0
|
|
51
|
+
velocity = 0
|
|
52
|
+
timer = null
|
|
53
|
+
onUpdate(y)
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
onUpdate(y)
|
|
57
|
+
timer = setTimeout(loop, frameMs)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function jump() {
|
|
61
|
+
// Stack impulse on rapid taps, clamped to one full jumpForce
|
|
62
|
+
velocity = Math.max(velocity + jumpForce * stackFactor, jumpForce)
|
|
63
|
+
if (!timer) {
|
|
64
|
+
loop()
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function destroy() {
|
|
69
|
+
if (timer) {
|
|
70
|
+
clearTimeout(timer)
|
|
71
|
+
timer = null
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return { jump, getY: () => y, destroy }
|
|
76
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from '@lynx-js/react'
|
|
2
|
+
|
|
3
|
+
import { createFlappy } from './lib/flappy.js'
|
|
4
|
+
import type { FlappyEngine, FlappyOptions } from './lib/flappy.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* React hook for flappy-bird physics.
|
|
8
|
+
*
|
|
9
|
+
* Returns `[y, jump]` — a state value and a stable callback.
|
|
10
|
+
* The game loop runs automatically; cleanup happens on unmount.
|
|
11
|
+
* Options are read once on mount and not reactive to later changes.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* function Bird() {
|
|
16
|
+
* const [y, jump] = useFlappy()
|
|
17
|
+
* return (
|
|
18
|
+
* <view bindtap={jump} style={{ transform: `translateY(${y}px)` }}>
|
|
19
|
+
* <text>Tap me!</text>
|
|
20
|
+
* </view>
|
|
21
|
+
* )
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function useFlappy(
|
|
26
|
+
options?: FlappyOptions,
|
|
27
|
+
): [number, () => void] {
|
|
28
|
+
const [y, setY] = useState(0)
|
|
29
|
+
const engineRef = useRef<FlappyEngine | null>(null)
|
|
30
|
+
|
|
31
|
+
if (engineRef.current == null) {
|
|
32
|
+
engineRef.current = createFlappy((newY) => {
|
|
33
|
+
setY(newY)
|
|
34
|
+
}, options)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
return () => {
|
|
39
|
+
engineRef.current?.destroy()
|
|
40
|
+
}
|
|
41
|
+
}, [])
|
|
42
|
+
|
|
43
|
+
const jump = useCallback(() => {
|
|
44
|
+
'background only'
|
|
45
|
+
engineRef.current?.jump()
|
|
46
|
+
}, [])
|
|
47
|
+
|
|
48
|
+
return [y, jump]
|
|
49
|
+
}
|
|
@@ -4,9 +4,11 @@ import './App.css'
|
|
|
4
4
|
import arrow from './assets/arrow.png'
|
|
5
5
|
import lynxLogo from './assets/lynx-logo.png'
|
|
6
6
|
import reactLynxLogo from './assets/react-logo.png'
|
|
7
|
+
import { useFlappy } from './useFlappy.js'
|
|
7
8
|
|
|
8
9
|
export function App(props) {
|
|
9
10
|
const [alterLogo, setAlterLogo] = useState(false)
|
|
11
|
+
const [logoY, jump] = useFlappy()
|
|
10
12
|
|
|
11
13
|
useEffect(() => {
|
|
12
14
|
console.info('Hello, ReactLynx')
|
|
@@ -19,11 +21,15 @@ export function App(props) {
|
|
|
19
21
|
}, [])
|
|
20
22
|
|
|
21
23
|
return (
|
|
22
|
-
<view>
|
|
24
|
+
<view bindtap={jump}>
|
|
23
25
|
<view className='Background' />
|
|
24
26
|
<view className='App'>
|
|
25
27
|
<view className='Banner'>
|
|
26
|
-
<view
|
|
28
|
+
<view
|
|
29
|
+
className='Logo'
|
|
30
|
+
style={{ transform: `translateY(${logoY}px)` }}
|
|
31
|
+
bindtap={onTap}
|
|
32
|
+
>
|
|
27
33
|
{alterLogo
|
|
28
34
|
? <image src={reactLynxLogo} className='Logo--react' />
|
|
29
35
|
: <image src={lynxLogo} className='Logo--lynx' />}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic flappy-bird physics engine.
|
|
3
|
+
*
|
|
4
|
+
* Manages gravity, jump impulse, and a 60fps game loop.
|
|
5
|
+
* Wire it up to any UI framework by calling `jump()` on tap
|
|
6
|
+
* and reading `getY()` in the loop callback.
|
|
7
|
+
*
|
|
8
|
+
* @param {(y: number) => void} onUpdate
|
|
9
|
+
* @param {object} [options]
|
|
10
|
+
* @param {number} [options.gravity=0.6]
|
|
11
|
+
* @param {number} [options.jumpForce=-12]
|
|
12
|
+
* @param {number} [options.stackFactor=0.6]
|
|
13
|
+
* @param {number} [options.frameMs=16]
|
|
14
|
+
* @returns {{ jump: () => void, getY: () => number, destroy: () => void }}
|
|
15
|
+
*/
|
|
16
|
+
export function createFlappy(onUpdate, options = {}) {
|
|
17
|
+
const {
|
|
18
|
+
gravity = 0.6,
|
|
19
|
+
jumpForce = -12,
|
|
20
|
+
stackFactor = 0.6,
|
|
21
|
+
frameMs = 16,
|
|
22
|
+
} = options
|
|
23
|
+
|
|
24
|
+
let y = 0
|
|
25
|
+
let velocity = 0
|
|
26
|
+
let timer = null
|
|
27
|
+
|
|
28
|
+
function loop() {
|
|
29
|
+
velocity += gravity
|
|
30
|
+
y += velocity
|
|
31
|
+
if (y >= 0) {
|
|
32
|
+
y = 0
|
|
33
|
+
velocity = 0
|
|
34
|
+
timer = null
|
|
35
|
+
onUpdate(y)
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
onUpdate(y)
|
|
39
|
+
timer = setTimeout(loop, frameMs)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function jump() {
|
|
43
|
+
// Stack impulse on rapid taps, clamped to one full jumpForce
|
|
44
|
+
velocity = Math.max(velocity + jumpForce * stackFactor, jumpForce)
|
|
45
|
+
if (!timer) {
|
|
46
|
+
loop()
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function destroy() {
|
|
51
|
+
if (timer) {
|
|
52
|
+
clearTimeout(timer)
|
|
53
|
+
timer = null
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return { jump, getY: () => y, destroy }
|
|
58
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from '@lynx-js/react'
|
|
2
|
+
|
|
3
|
+
import { createFlappy } from './lib/flappy.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* React hook for flappy-bird physics.
|
|
7
|
+
*
|
|
8
|
+
* Returns `[y, jump]` — a state value and a stable callback.
|
|
9
|
+
* The game loop runs automatically; cleanup happens on unmount.
|
|
10
|
+
* Options are read once on mount and not reactive to later changes.
|
|
11
|
+
*
|
|
12
|
+
* @param {object} [options]
|
|
13
|
+
* @returns {[number, () => void]}
|
|
14
|
+
*/
|
|
15
|
+
export function useFlappy(options) {
|
|
16
|
+
const [y, setY] = useState(0)
|
|
17
|
+
const engineRef = useRef(null)
|
|
18
|
+
|
|
19
|
+
if (engineRef.current == null) {
|
|
20
|
+
engineRef.current = createFlappy((newY) => {
|
|
21
|
+
setY(newY)
|
|
22
|
+
}, options)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
return () => {
|
|
27
|
+
engineRef.current?.destroy()
|
|
28
|
+
}
|
|
29
|
+
}, [])
|
|
30
|
+
|
|
31
|
+
const jump = useCallback(() => {
|
|
32
|
+
'background only'
|
|
33
|
+
engineRef.current?.jump()
|
|
34
|
+
}, [])
|
|
35
|
+
|
|
36
|
+
return [y, jump]
|
|
37
|
+
}
|
|
@@ -4,11 +4,13 @@ import './App.css'
|
|
|
4
4
|
import arrow from './assets/arrow.png'
|
|
5
5
|
import lynxLogo from './assets/lynx-logo.png'
|
|
6
6
|
import reactLynxLogo from './assets/react-logo.png'
|
|
7
|
+
import { useFlappy } from './useFlappy.js'
|
|
7
8
|
|
|
8
9
|
export function App(props: {
|
|
9
10
|
onRender?: () => void
|
|
10
11
|
}) {
|
|
11
12
|
const [alterLogo, setAlterLogo] = useState(false)
|
|
13
|
+
const [logoY, jump] = useFlappy()
|
|
12
14
|
|
|
13
15
|
useEffect(() => {
|
|
14
16
|
console.info('Hello, ReactLynx')
|
|
@@ -21,11 +23,15 @@ export function App(props: {
|
|
|
21
23
|
}, [])
|
|
22
24
|
|
|
23
25
|
return (
|
|
24
|
-
<view>
|
|
26
|
+
<view bindtap={jump}>
|
|
25
27
|
<view className='Background' />
|
|
26
28
|
<view className='App'>
|
|
27
29
|
<view className='Banner'>
|
|
28
|
-
<view
|
|
30
|
+
<view
|
|
31
|
+
className='Logo'
|
|
32
|
+
style={{ transform: `translateY(${logoY}px)` }}
|
|
33
|
+
bindtap={onTap}
|
|
34
|
+
>
|
|
29
35
|
{alterLogo
|
|
30
36
|
? <image src={reactLynxLogo} className='Logo--react' />
|
|
31
37
|
: <image src={lynxLogo} className='Logo--lynx' />}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic flappy-bird physics engine.
|
|
3
|
+
*
|
|
4
|
+
* Manages gravity, jump impulse, and a 60fps game loop.
|
|
5
|
+
* Wire it up to any UI framework by calling `jump()` on tap
|
|
6
|
+
* and reading `getY()` in the loop callback.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface FlappyOptions {
|
|
10
|
+
/** Downward acceleration per frame (default 0.6) */
|
|
11
|
+
gravity?: number
|
|
12
|
+
/** Upward impulse per tap — negative value (default -12) */
|
|
13
|
+
jumpForce?: number
|
|
14
|
+
/** Impulse stacking factor for rapid taps (default 0.6) */
|
|
15
|
+
stackFactor?: number
|
|
16
|
+
/** Frame interval in ms (default 16 ≈ 60fps) */
|
|
17
|
+
frameMs?: number
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type OnUpdate = (y: number) => void
|
|
21
|
+
|
|
22
|
+
export interface FlappyEngine {
|
|
23
|
+
/** Call on each tap to apply upward impulse. */
|
|
24
|
+
jump(): void
|
|
25
|
+
/** Current Y offset (0 = ground, negative = airborne). */
|
|
26
|
+
getY(): number
|
|
27
|
+
/** Stop the game loop and clean up. */
|
|
28
|
+
destroy(): void
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function createFlappy(
|
|
32
|
+
onUpdate: OnUpdate,
|
|
33
|
+
options: FlappyOptions = {},
|
|
34
|
+
): FlappyEngine {
|
|
35
|
+
const {
|
|
36
|
+
gravity = 0.6,
|
|
37
|
+
jumpForce = -12,
|
|
38
|
+
stackFactor = 0.6,
|
|
39
|
+
frameMs = 16,
|
|
40
|
+
} = options
|
|
41
|
+
|
|
42
|
+
let y = 0
|
|
43
|
+
let velocity = 0
|
|
44
|
+
let timer: ReturnType<typeof setTimeout> | null = null
|
|
45
|
+
|
|
46
|
+
function loop() {
|
|
47
|
+
velocity += gravity
|
|
48
|
+
y += velocity
|
|
49
|
+
if (y >= 0) {
|
|
50
|
+
y = 0
|
|
51
|
+
velocity = 0
|
|
52
|
+
timer = null
|
|
53
|
+
onUpdate(y)
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
onUpdate(y)
|
|
57
|
+
timer = setTimeout(loop, frameMs)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function jump() {
|
|
61
|
+
// Stack impulse on rapid taps, clamped to one full jumpForce
|
|
62
|
+
velocity = Math.max(velocity + jumpForce * stackFactor, jumpForce)
|
|
63
|
+
if (!timer) {
|
|
64
|
+
loop()
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function destroy() {
|
|
69
|
+
if (timer) {
|
|
70
|
+
clearTimeout(timer)
|
|
71
|
+
timer = null
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return { jump, getY: () => y, destroy }
|
|
76
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from '@lynx-js/react'
|
|
2
|
+
|
|
3
|
+
import { createFlappy } from './lib/flappy.js'
|
|
4
|
+
import type { FlappyEngine, FlappyOptions } from './lib/flappy.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* React hook for flappy-bird physics.
|
|
8
|
+
*
|
|
9
|
+
* Returns `[y, jump]` — a state value and a stable callback.
|
|
10
|
+
* The game loop runs automatically; cleanup happens on unmount.
|
|
11
|
+
* Options are read once on mount and not reactive to later changes.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* function Bird() {
|
|
16
|
+
* const [y, jump] = useFlappy()
|
|
17
|
+
* return (
|
|
18
|
+
* <view bindtap={jump} style={{ transform: `translateY(${y}px)` }}>
|
|
19
|
+
* <text>Tap me!</text>
|
|
20
|
+
* </view>
|
|
21
|
+
* )
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function useFlappy(
|
|
26
|
+
options?: FlappyOptions,
|
|
27
|
+
): [number, () => void] {
|
|
28
|
+
const [y, setY] = useState(0)
|
|
29
|
+
const engineRef = useRef<FlappyEngine | null>(null)
|
|
30
|
+
|
|
31
|
+
if (engineRef.current == null) {
|
|
32
|
+
engineRef.current = createFlappy((newY) => {
|
|
33
|
+
setY(newY)
|
|
34
|
+
}, options)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
return () => {
|
|
39
|
+
engineRef.current?.destroy()
|
|
40
|
+
}
|
|
41
|
+
}, [])
|
|
42
|
+
|
|
43
|
+
const jump = useCallback(() => {
|
|
44
|
+
'background only'
|
|
45
|
+
engineRef.current?.jump()
|
|
46
|
+
}, [])
|
|
47
|
+
|
|
48
|
+
return [y, jump]
|
|
49
|
+
}
|