-
Notifications
You must be signed in to change notification settings - Fork 122
feat(examples): add flappy-bird hook demo to React templates #2325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /** | ||
| * Framework-agnostic flappy-bird physics engine. | ||
| * | ||
| * Manages gravity, jump impulse, and a 60fps game loop. | ||
| * Wire it up to any UI framework by calling `jump()` on tap | ||
| * and reading `getY()` in the loop callback. | ||
| */ | ||
|
|
||
| export interface FlappyOptions { | ||
| /** Downward acceleration per frame (default 0.6) */ | ||
| gravity?: number; | ||
| /** Upward impulse per tap — negative value (default -12) */ | ||
| jumpForce?: number; | ||
| /** Impulse stacking factor for rapid taps (default 0.6) */ | ||
| stackFactor?: number; | ||
| /** Frame interval in ms (default 16 ≈ 60fps) */ | ||
| frameMs?: number; | ||
| } | ||
|
|
||
| export type OnUpdate = (y: number) => void; | ||
|
|
||
| export interface FlappyEngine { | ||
| /** Call on each tap to apply upward impulse. */ | ||
| jump(): void; | ||
| /** Current Y offset (0 = ground, negative = airborne). */ | ||
| getY(): number; | ||
| /** Stop the game loop and clean up. */ | ||
| destroy(): void; | ||
| } | ||
|
|
||
| export function createFlappy( | ||
| onUpdate: OnUpdate, | ||
| options: FlappyOptions = {}, | ||
| ): FlappyEngine { | ||
| const { | ||
| gravity = 0.6, | ||
| jumpForce = -12, | ||
| stackFactor = 0.6, | ||
| frameMs = 16, | ||
| } = options; | ||
|
|
||
| let y = 0; | ||
| let velocity = 0; | ||
| let timer: ReturnType<typeof setTimeout> | null = null; | ||
|
|
||
| function loop() { | ||
| velocity += gravity; | ||
| y += velocity; | ||
| if (y >= 0) { | ||
| y = 0; | ||
| velocity = 0; | ||
| timer = null; | ||
| onUpdate(y); | ||
| return; | ||
| } | ||
| onUpdate(y); | ||
| timer = setTimeout(loop, frameMs); | ||
| } | ||
|
|
||
| function jump() { | ||
| // Stack impulse on rapid taps, clamped to one full jumpForce | ||
| velocity = Math.max(velocity + jumpForce * stackFactor, jumpForce); | ||
| if (!timer) { | ||
| loop(); | ||
| } | ||
| } | ||
Huxpro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| function destroy() { | ||
| if (timer) { | ||
| clearTimeout(timer); | ||
| timer = null; | ||
| } | ||
| } | ||
Huxpro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return { jump, getY: () => y, destroy }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { useCallback, useEffect, useRef, useState } from '@lynx-js/react'; | ||
|
|
||
| import { createFlappy } from './lib/flappy.js'; | ||
| import type { FlappyEngine, FlappyOptions } from './lib/flappy.js'; | ||
|
|
||
| /** | ||
| * React hook for flappy-bird physics. | ||
| * | ||
| * Returns `[y, jump]` — a state value and a stable callback. | ||
| * The game loop runs automatically; cleanup happens on unmount. | ||
| * Options are read once on mount and not reactive to later changes. | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * function Bird() { | ||
| * const [y, jump] = useFlappy(); | ||
| * return ( | ||
| * <view bindtap={jump} style={{ transform: `translateY(${y}px)` }}> | ||
| * <text>Tap me!</text> | ||
| * </view> | ||
| * ); | ||
| * } | ||
| * ``` | ||
| */ | ||
| export function useFlappy( | ||
| options?: FlappyOptions, | ||
| ): [number, () => void] { | ||
| const [y, setY] = useState(0); | ||
| const engineRef = useRef<FlappyEngine | null>(null); | ||
|
|
||
| engineRef.current ??= createFlappy((newY) => { | ||
| setY(newY); | ||
| }, options); | ||
|
|
||
Huxpro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| useEffect(() => { | ||
| return () => { | ||
| engineRef.current?.destroy(); | ||
| }; | ||
| }, []); | ||
|
|
||
| const jump = useCallback(() => { | ||
| 'background-only'; | ||
| engineRef.current?.jump(); | ||
| }, []); | ||
|
|
||
| return [y, jump]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/rspeedy/create-rspeedy/template-react-js/src/lib/flappy.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * Framework-agnostic flappy-bird physics engine. | ||
| * | ||
| * Manages gravity, jump impulse, and a 60fps game loop. | ||
| * Wire it up to any UI framework by calling `jump()` on tap | ||
| * and reading `getY()` in the loop callback. | ||
| * | ||
| * @param {(y: number) => void} onUpdate | ||
| * @param {object} [options] | ||
| * @param {number} [options.gravity=0.6] | ||
| * @param {number} [options.jumpForce=-12] | ||
| * @param {number} [options.stackFactor=0.6] | ||
| * @param {number} [options.frameMs=16] | ||
| * @returns {{ jump: () => void, getY: () => number, destroy: () => void }} | ||
| */ | ||
| export function createFlappy(onUpdate, options = {}) { | ||
| const { | ||
| gravity = 0.6, | ||
| jumpForce = -12, | ||
| stackFactor = 0.6, | ||
| frameMs = 16, | ||
| } = options | ||
|
|
||
| let y = 0 | ||
| let velocity = 0 | ||
| let timer = null | ||
|
|
||
| function loop() { | ||
| velocity += gravity | ||
| y += velocity | ||
| if (y >= 0) { | ||
| y = 0 | ||
| velocity = 0 | ||
| timer = null | ||
| onUpdate(y) | ||
| return | ||
| } | ||
| onUpdate(y) | ||
| timer = setTimeout(loop, frameMs) | ||
| } | ||
|
|
||
| function jump() { | ||
| // Stack impulse on rapid taps, clamped to one full jumpForce | ||
| velocity = Math.max(velocity + jumpForce * stackFactor, jumpForce) | ||
| if (!timer) { | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| loop() | ||
| } | ||
Huxpro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| function destroy() { | ||
| if (timer) { | ||
| clearTimeout(timer) | ||
| timer = null | ||
| } | ||
| } | ||
|
|
||
| return { jump, getY: () => y, destroy } | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
packages/rspeedy/create-rspeedy/template-react-js/src/useFlappy.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { useCallback, useEffect, useRef, useState } from '@lynx-js/react' | ||
|
|
||
| import { createFlappy } from './lib/flappy.js' | ||
|
|
||
| /** | ||
| * React hook for flappy-bird physics. | ||
| * | ||
| * Returns `[y, jump]` — a state value and a stable callback. | ||
| * The game loop runs automatically; cleanup happens on unmount. | ||
| * Options are read once on mount and not reactive to later changes. | ||
| * | ||
| * @param {object} [options] | ||
| * @returns {[number, () => void]} | ||
| */ | ||
| export function useFlappy(options) { | ||
| const [y, setY] = useState(0) | ||
| const engineRef = useRef(null) | ||
|
|
||
| if (!engineRef.current) { | ||
| engineRef.current = createFlappy((newY) => { | ||
| setY(newY) | ||
| }, options) | ||
| } | ||
Huxpro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| engineRef.current?.destroy() | ||
| } | ||
| }, []) | ||
|
|
||
| const jump = useCallback(() => { | ||
| 'background only' | ||
| engineRef.current?.jump() | ||
| }, []) | ||
|
|
||
| return [y, jump] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
packages/rspeedy/create-rspeedy/template-react-ts/src/lib/flappy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /** | ||
| * Framework-agnostic flappy-bird physics engine. | ||
| * | ||
| * Manages gravity, jump impulse, and a 60fps game loop. | ||
| * Wire it up to any UI framework by calling `jump()` on tap | ||
| * and reading `getY()` in the loop callback. | ||
| */ | ||
|
|
||
| export interface FlappyOptions { | ||
| /** Downward acceleration per frame (default 0.6) */ | ||
| gravity?: number | ||
| /** Upward impulse per tap — negative value (default -12) */ | ||
| jumpForce?: number | ||
| /** Impulse stacking factor for rapid taps (default 0.6) */ | ||
| stackFactor?: number | ||
| /** Frame interval in ms (default 16 ≈ 60fps) */ | ||
| frameMs?: number | ||
| } | ||
|
|
||
| export type OnUpdate = (y: number) => void | ||
|
|
||
| export interface FlappyEngine { | ||
| /** Call on each tap to apply upward impulse. */ | ||
| jump(): void | ||
| /** Current Y offset (0 = ground, negative = airborne). */ | ||
| getY(): number | ||
| /** Stop the game loop and clean up. */ | ||
| destroy(): void | ||
| } | ||
|
|
||
| export function createFlappy( | ||
| onUpdate: OnUpdate, | ||
| options: FlappyOptions = {}, | ||
| ): FlappyEngine { | ||
| const { | ||
| gravity = 0.6, | ||
| jumpForce = -12, | ||
| stackFactor = 0.6, | ||
| frameMs = 16, | ||
| } = options | ||
|
|
||
| let y = 0 | ||
| let velocity = 0 | ||
| let timer: ReturnType<typeof setTimeout> | null = null | ||
|
|
||
| function loop() { | ||
| velocity += gravity | ||
| y += velocity | ||
| if (y >= 0) { | ||
| y = 0 | ||
| velocity = 0 | ||
| timer = null | ||
| onUpdate(y) | ||
| return | ||
| } | ||
| onUpdate(y) | ||
| timer = setTimeout(loop, frameMs) | ||
| } | ||
|
|
||
| function jump() { | ||
| // Stack impulse on rapid taps, clamped to one full jumpForce | ||
| velocity = Math.max(velocity + jumpForce * stackFactor, jumpForce) | ||
| if (!timer) { | ||
Huxpro marked this conversation as resolved.
Show resolved
Hide resolved
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| loop() | ||
| } | ||
| } | ||
|
|
||
| function destroy() { | ||
| if (timer) { | ||
| clearTimeout(timer) | ||
| timer = null | ||
| } | ||
| } | ||
|
|
||
| return { jump, getY: () => y, destroy } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.