@air-jam/cli 0.0.0-stage → 0.9.3
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/LICENSE +21 -0
- package/README.md +145 -2
- package/bin/airjam.mjs +66 -0
- package/dist/ai-pack.d.ts +44 -0
- package/dist/ai-pack.js +15 -0
- package/dist/ai-pack.js.map +1 -0
- package/dist/chunk-A4V2N4LX.js +938 -0
- package/dist/chunk-A4V2N4LX.js.map +1 -0
- package/dist/chunk-J5MAU4QZ.js +102 -0
- package/dist/chunk-J5MAU4QZ.js.map +1 -0
- package/dist/chunk-N7AI7HZK.js +529 -0
- package/dist/chunk-N7AI7HZK.js.map +1 -0
- package/dist/development.d.ts +98 -0
- package/dist/development.js +33 -0
- package/dist/development.js.map +1 -0
- package/dist/documentation.d.ts +27 -0
- package/dist/documentation.js +7 -0
- package/dist/documentation.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2184 -0
- package/dist/index.js.map +1 -0
- package/dist/scaffold.d.ts +6 -0
- package/dist/scaffold.js +20 -0
- package/dist/scaffold.js.map +1 -0
- package/package.json +78 -3
- package/runtime/local-network.d.mts +8 -0
- package/runtime/local-network.mjs +9 -0
- package/runtime/vite-config.d.mts +42 -0
- package/runtime/vite-config.mjs +130 -0
- package/runtime/vite-https.d.mts +11 -0
- package/runtime/vite-https.mjs +41 -0
- package/template-assets/bootstrap/.claude/launch.json +12 -0
- package/template-assets/bootstrap/AGENTS.md +248 -0
- package/template-assets/bootstrap/CLAUDE.md +63 -0
- package/template-assets/bootstrap/_gitignore +6 -0
- package/template-assets/bootstrap/skills/airjam-docs-workflow/SKILL.md +49 -0
- package/template-assets/bootstrap/skills/airjam-mcp/SKILL.md +56 -0
- package/template-assets/bootstrap/skills/canvas-2d-gameplay/SKILL.md +47 -0
- package/template-assets/bootstrap/skills/controller-ui/SKILL.md +69 -0
- package/template-assets/bootstrap/skills/debug-and-test/SKILL.md +72 -0
- package/template-assets/bootstrap/skills/game-architecture/SKILL.md +58 -0
- package/template-assets/bootstrap/skills/game-state-and-rendering/SKILL.md +44 -0
- package/template-assets/bootstrap/skills/host-surface/SKILL.md +52 -0
- package/template-assets/bootstrap/skills/index.md +36 -0
- package/template-assets/bootstrap/skills/prefab-authoring/SKILL.md +48 -0
- package/template-assets/bootstrap/skills/r3f-3d-scene/SKILL.md +51 -0
- package/template-assets/documentation/for-agents/controller-ui/page.mdx +60 -0
- package/template-assets/documentation/for-agents/page.mdx +98 -0
- package/template-assets/documentation/for-agents/project-structure/page.mdx +172 -0
- package/template-assets/documentation/for-agents/state-and-rendering/page.mdx +79 -0
- package/template-assets/documentation/getting-started/debugging/page.mdx +165 -0
- package/template-assets/documentation/getting-started/dev-logs/page.mdx +171 -0
- package/template-assets/documentation/getting-started/game-ideas/page.mdx +116 -0
- package/template-assets/documentation/getting-started/introduction/page.mdx +183 -0
- package/template-assets/documentation/getting-started/migrating-legacy-games/page.mdx +427 -0
- package/template-assets/documentation/getting-started/quick-start/page.mdx +333 -0
- package/template-assets/documentation/how-it-works/architecture/page.mdx +189 -0
- package/template-assets/documentation/how-it-works/host-system/page.mdx +267 -0
- package/template-assets/documentation/manifest.json +430 -0
- package/template-assets/documentation/sdk/hooks/page.mdx +500 -0
- package/template-assets/documentation/sdk/input-system/page.mdx +270 -0
- package/template-assets/documentation/sdk/networked-state/page.mdx +213 -0
- package/template-assets/documentation/sdk/ui-components/page.mdx +227 -0
- package/template-assets/managed/.airjam/ai-pack.json +191 -0
- package/template-assets/managed/docs/airjam/agent-gold-path.md +67 -0
- package/template-assets/managed/docs/airjam/agent-mcp.md +135 -0
- package/template-assets/managed/docs/airjam/canvas-2d-gameplay.md +82 -0
- package/template-assets/managed/docs/airjam/composition-shell-contract.md +87 -0
- package/template-assets/managed/docs/airjam/debug-and-testing.md +86 -0
- package/template-assets/managed/docs/airjam/development-loop.md +51 -0
- package/template-assets/managed/docs/airjam/docs-index.md +58 -0
- package/template-assets/managed/docs/airjam/generated/architecture.md +178 -0
- package/template-assets/managed/docs/airjam/generated/controller-ui.md +61 -0
- package/template-assets/managed/docs/airjam/generated/debugging-and-logs.md +164 -0
- package/template-assets/managed/docs/airjam/generated/for-agents.md +99 -0
- package/template-assets/managed/docs/airjam/generated/game-ideas.md +107 -0
- package/template-assets/managed/docs/airjam/generated/host-system.md +251 -0
- package/template-assets/managed/docs/airjam/generated/input-system.md +260 -0
- package/template-assets/managed/docs/airjam/generated/introduction.md +175 -0
- package/template-assets/managed/docs/airjam/generated/migrating-legacy-games.md +426 -0
- package/template-assets/managed/docs/airjam/generated/networked-state.md +214 -0
- package/template-assets/managed/docs/airjam/generated/project-structure.md +171 -0
- package/template-assets/managed/docs/airjam/generated/quick-start.md +334 -0
- package/template-assets/managed/docs/airjam/generated/sdk-hooks.md +490 -0
- package/template-assets/managed/docs/airjam/generated/state-and-rendering.md +80 -0
- package/template-assets/managed/docs/airjam/generated/ui-components.md +222 -0
- package/template-assets/managed/docs/airjam/generated/unified-dev-logs.md +170 -0
- package/template-assets/managed/docs/airjam/iconography.md +55 -0
- package/template-assets/managed/docs/airjam/prefab-authoring.md +112 -0
- package/template-assets/managed/docs/airjam/preview-controllers.md +126 -0
- package/template-assets/managed/docs/airjam/r3f-3d-scene.md +97 -0
- package/template-assets/managed/docs/airjam/state-lanes-cookbook.md +106 -0
- package/template-assets/managed/docs/airjam/visual-system.md +103 -0
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
<!-- Generated from content/docs/sdk/hooks/page.mdx. Do not edit directly. -->
|
|
2
|
+
<!-- Canonical public doc: https://airjam.io/docs/sdk/hooks -->
|
|
3
|
+
|
|
4
|
+
# SDK Hooks
|
|
5
|
+
|
|
6
|
+
The Air Jam SDK provides React hooks for building multiplayer games. This page documents all available hooks and their usage.
|
|
7
|
+
|
|
8
|
+
### `createAirJamApp`
|
|
9
|
+
|
|
10
|
+
Use `createAirJamApp` as the canonical runtime setup. It wires scoped providers, route paths, and typed input schema in one place.
|
|
11
|
+
|
|
12
|
+
```tsx
|
|
13
|
+
import { createAirJamApp, env } from "@air-jam/sdk";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
|
|
16
|
+
const inputSchema = z.object({
|
|
17
|
+
vector: z.object({ x: z.number(), y: z.number() }),
|
|
18
|
+
timestamp: z.number(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const airjam = createAirJamApp({
|
|
22
|
+
runtime: env.vite(import.meta.env),
|
|
23
|
+
controllerPath: "/controller",
|
|
24
|
+
input: { schema: inputSchema },
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
<Routes>
|
|
28
|
+
<Route
|
|
29
|
+
path="/"
|
|
30
|
+
element={
|
|
31
|
+
<airjam.Host>
|
|
32
|
+
<HostView />
|
|
33
|
+
</airjam.Host>
|
|
34
|
+
}
|
|
35
|
+
/>
|
|
36
|
+
<Route
|
|
37
|
+
path={airjam.paths.controller}
|
|
38
|
+
element={
|
|
39
|
+
<airjam.Controller>
|
|
40
|
+
<ControllerView />
|
|
41
|
+
</airjam.Controller>
|
|
42
|
+
}
|
|
43
|
+
/>
|
|
44
|
+
</Routes>;
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**Environment Variables:**
|
|
48
|
+
|
|
49
|
+
The provider automatically reads from these environment variables if props aren't provided:
|
|
50
|
+
|
|
51
|
+
- `VITE_AIR_JAM_SERVER_URL` / `NEXT_PUBLIC_AIR_JAM_SERVER_URL` - WebSocket server URL
|
|
52
|
+
- `VITE_AIR_JAM_APP_ID` / `NEXT_PUBLIC_AIR_JAM_APP_ID` - Public app ID
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
### `useAirJamHost`
|
|
57
|
+
|
|
58
|
+
Consumer hook for host runtime state and actions. Mount `airjam.Host` once at the boundary, then call `useAirJamHost()` anywhere below it.
|
|
59
|
+
|
|
60
|
+
Call `useAirJamHost(selector)` when a component only needs a narrow slice of
|
|
61
|
+
host session state and you want to avoid rerendering on unrelated runtime
|
|
62
|
+
fields.
|
|
63
|
+
Selectors receive state fields only; call `useAirJamHost()` when a component
|
|
64
|
+
also needs runtime controls such as `joinUrl`, `sendSignal`, or `getInput`.
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
import { useAirJamHost } from "@air-jam/sdk";
|
|
68
|
+
import { airjam } from "../airjam.config";
|
|
69
|
+
|
|
70
|
+
const HostShell = () => (
|
|
71
|
+
<airjam.Host
|
|
72
|
+
roomId="GAME"
|
|
73
|
+
onPlayerJoin={(player) => {
|
|
74
|
+
console.log(`${player.label} joined with color ${player.color}`);
|
|
75
|
+
}}
|
|
76
|
+
onPlayerLeave={(controllerId) => {
|
|
77
|
+
console.log(`Player ${controllerId} left`);
|
|
78
|
+
}}
|
|
79
|
+
>
|
|
80
|
+
<HostView />
|
|
81
|
+
</airjam.Host>
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const HostView = () => {
|
|
85
|
+
const host = useAirJamHost();
|
|
86
|
+
|
|
87
|
+
// Return values
|
|
88
|
+
const {
|
|
89
|
+
roomId, // "ABCD" - The room code
|
|
90
|
+
joinUrl, // Full URL for controllers to join
|
|
91
|
+
joinUrlStatus, // "loading" | "ready" | "unavailable"
|
|
92
|
+
connectionStatus, // "connected" | "connecting" | "disconnected" | "idle"
|
|
93
|
+
players, // Array of PlayerProfile
|
|
94
|
+
controllers, // Rich controller-session roster with source + lease state
|
|
95
|
+
runtimeState, // "playing" | "paused"
|
|
96
|
+
lastError, // Error message if any
|
|
97
|
+
mode, // "standalone" | "platform"
|
|
98
|
+
|
|
99
|
+
// Functions
|
|
100
|
+
getInput, // (controllerId: string) => Input | undefined
|
|
101
|
+
sendSignal, // Send haptics/toasts to controllers
|
|
102
|
+
sendState, // Publish lightweight controller presentation state
|
|
103
|
+
pauseRuntime, // Set runtimeState to "paused"
|
|
104
|
+
resumeRuntime, // Set runtimeState to "playing"
|
|
105
|
+
setRuntimeState, // Explicitly set "playing" | "paused"
|
|
106
|
+
reconnect, // Force reconnection
|
|
107
|
+
} = host;
|
|
108
|
+
|
|
109
|
+
return (
|
|
110
|
+
<div className="relative min-h-screen bg-black text-white">
|
|
111
|
+
<header className="absolute top-0 right-0 left-0 z-10 flex items-center justify-between border-b border-white/10 bg-black/70 px-4 py-2 backdrop-blur">
|
|
112
|
+
<span>Room {roomId}</span>
|
|
113
|
+
<button
|
|
114
|
+
onClick={runtimeState === "playing" ? pauseRuntime : resumeRuntime}
|
|
115
|
+
>
|
|
116
|
+
{runtimeState === "playing" ? "Pause" : "Resume"}
|
|
117
|
+
</button>
|
|
118
|
+
</header>
|
|
119
|
+
<GameCanvas />
|
|
120
|
+
</div>
|
|
121
|
+
);
|
|
122
|
+
};
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Use `players` for normal gameplay-facing iteration. Use `controllers` when you
|
|
126
|
+
need controller provenance or local recovery tooling.
|
|
127
|
+
|
|
128
|
+
**Reading Input in Game Loops:**
|
|
129
|
+
|
|
130
|
+
```tsx
|
|
131
|
+
// In a React Three Fiber component
|
|
132
|
+
useFrame(() => {
|
|
133
|
+
host.players.forEach((player) => {
|
|
134
|
+
const input = host.getInput(player.id);
|
|
135
|
+
if (!input) return;
|
|
136
|
+
|
|
137
|
+
// Move player based on joystick
|
|
138
|
+
movePlayer(player.id, input.vector);
|
|
139
|
+
|
|
140
|
+
// Handle button press (tap-safe pulse by default)
|
|
141
|
+
if (input.action) {
|
|
142
|
+
playerShoot(player.id);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
`sendState` is only for lightweight controller presentation metadata such as
|
|
149
|
+
orientation, pause/play status, or short UI messages. It is not the primary
|
|
150
|
+
gameplay sync channel. Authoritative multiplayer state should live in the
|
|
151
|
+
networked stores and flow automatically through those boundaries.
|
|
152
|
+
|
|
153
|
+
**Sending Haptic Feedback:**
|
|
154
|
+
|
|
155
|
+
```tsx
|
|
156
|
+
// Vibrate a specific player's phone
|
|
157
|
+
host.sendSignal("HAPTIC", { pattern: "heavy" }, playerId);
|
|
158
|
+
|
|
159
|
+
// Available patterns: "light", "medium", "heavy", "success", "failure", "custom"
|
|
160
|
+
host.sendSignal(
|
|
161
|
+
"HAPTIC",
|
|
162
|
+
{
|
|
163
|
+
pattern: "custom",
|
|
164
|
+
sequence: [50, 100, 50], // Vibrate 50ms, pause 100ms, vibrate 50ms
|
|
165
|
+
},
|
|
166
|
+
playerId,
|
|
167
|
+
);
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
**Sending Toast Notifications:**
|
|
171
|
+
|
|
172
|
+
```tsx
|
|
173
|
+
// Show notification on a player's controller
|
|
174
|
+
host.sendSignal(
|
|
175
|
+
"TOAST",
|
|
176
|
+
{
|
|
177
|
+
message: "Achievement unlocked: First blood",
|
|
178
|
+
color: "#22c55e", // Optional accent color
|
|
179
|
+
duration: 2000, // Optional duration in ms
|
|
180
|
+
},
|
|
181
|
+
playerId,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
// Broadcast to all players (omit targetId)
|
|
185
|
+
host.sendSignal("TOAST", {
|
|
186
|
+
message: "Round start! Get ready to fight.",
|
|
187
|
+
});
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
### `useGetInput`
|
|
193
|
+
|
|
194
|
+
Lightweight hook for accessing input without triggering re-renders. Use in performance-critical components.
|
|
195
|
+
|
|
196
|
+
```tsx
|
|
197
|
+
import { useGetInput } from "@air-jam/sdk";
|
|
198
|
+
|
|
199
|
+
const Ship = ({ playerId }: { playerId: string }) => {
|
|
200
|
+
const getInput = useGetInput();
|
|
201
|
+
|
|
202
|
+
// This component won't re-render when connection state changes
|
|
203
|
+
useFrame(() => {
|
|
204
|
+
const input = getInput(playerId);
|
|
205
|
+
if (!input) return;
|
|
206
|
+
|
|
207
|
+
// Update ship position
|
|
208
|
+
shipRef.current.position.x += input.vector.x * SPEED;
|
|
209
|
+
shipRef.current.position.y += input.vector.y * SPEED;
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
return <mesh ref={shipRef}>...</mesh>;
|
|
213
|
+
};
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
**When to use `useGetInput` vs `useAirJamHost().getInput`:**
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
### `useSendSignal`
|
|
221
|
+
|
|
222
|
+
Lightweight hook for sending signals without triggering re-renders. Use in collision handlers.
|
|
223
|
+
|
|
224
|
+
```tsx
|
|
225
|
+
import { useSendSignal } from "@air-jam/sdk";
|
|
226
|
+
|
|
227
|
+
const Laser = ({ ownerId }: { ownerId: string }) => {
|
|
228
|
+
const sendSignal = useSendSignal();
|
|
229
|
+
|
|
230
|
+
const handleHit = (targetId: string) => {
|
|
231
|
+
// Vibrate the player who got hit
|
|
232
|
+
sendSignal("HAPTIC", { pattern: "heavy" }, targetId);
|
|
233
|
+
|
|
234
|
+
// Light feedback for the shooter
|
|
235
|
+
sendSignal("HAPTIC", { pattern: "light" }, ownerId);
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
// Collision detection...
|
|
239
|
+
};
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
---
|
|
243
|
+
|
|
244
|
+
### `useAirJamController`
|
|
245
|
+
|
|
246
|
+
Consumer hook for controller runtime state and actions. Mount `airjam.Controller` once at the boundary, then call `useAirJamController()` below it.
|
|
247
|
+
|
|
248
|
+
Call `useAirJamController(selector)` when a component only needs a narrow slice
|
|
249
|
+
of controller session state and should not rerender on unrelated runtime
|
|
250
|
+
updates.
|
|
251
|
+
Selectors receive state fields only; call `useAirJamController()` when a
|
|
252
|
+
component also needs controls such as `sendSystemCommand`.
|
|
253
|
+
|
|
254
|
+
Standalone controller runtimes also keep one stable local device identity and
|
|
255
|
+
automatically attempt same-device resume when reconnecting to the same room.
|
|
256
|
+
|
|
257
|
+
```tsx
|
|
258
|
+
import {
|
|
259
|
+
useAirJamController,
|
|
260
|
+
useControllerTick,
|
|
261
|
+
useInputWriter,
|
|
262
|
+
} from "@air-jam/sdk";
|
|
263
|
+
import { useRef } from "react";
|
|
264
|
+
import { airjam } from "../airjam.config";
|
|
265
|
+
|
|
266
|
+
const ControllerShell = () => (
|
|
267
|
+
<airjam.Controller
|
|
268
|
+
roomId="ABCD"
|
|
269
|
+
nickname="Player1"
|
|
270
|
+
onState={(state) => {
|
|
271
|
+
if (state.message) {
|
|
272
|
+
showNotification(state.message);
|
|
273
|
+
}
|
|
274
|
+
}}
|
|
275
|
+
>
|
|
276
|
+
<ControllerView />
|
|
277
|
+
</airjam.Controller>
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
const ControllerView = () => {
|
|
281
|
+
const controller = useAirJamController();
|
|
282
|
+
const writeInput = useInputWriter();
|
|
283
|
+
const vectorRef = useRef({ x: 0, y: 0 });
|
|
284
|
+
const actionRef = useRef(false);
|
|
285
|
+
|
|
286
|
+
const {
|
|
287
|
+
roomId, // Room code (from URL or props)
|
|
288
|
+
controllerId, // This controller's unique ID
|
|
289
|
+
connectionStatus, // Connection state
|
|
290
|
+
runtimeState, // "playing" | "paused"
|
|
291
|
+
controllerOrientation, // "portrait" | "landscape"
|
|
292
|
+
stateMessage, // Optional message from host
|
|
293
|
+
|
|
294
|
+
// Functions
|
|
295
|
+
sendSystemCommand, // "exit" | "pause" | "resume"
|
|
296
|
+
setNickname, // Update nickname
|
|
297
|
+
setAvatarId, // Update avatar selection draft
|
|
298
|
+
updatePlayerProfile, // Patch live label/avatar when connected
|
|
299
|
+
selfPlayer, // This controller's player profile when known
|
|
300
|
+
reconnect, // Force reconnection
|
|
301
|
+
} = controller;
|
|
302
|
+
|
|
303
|
+
// Canonical cadence for continuous controls:
|
|
304
|
+
// publish at a fixed tick with useControllerTick + useInputWriter.
|
|
305
|
+
useControllerTick(
|
|
306
|
+
() => {
|
|
307
|
+
writeInput({
|
|
308
|
+
vector: vectorRef.current,
|
|
309
|
+
action: actionRef.current,
|
|
310
|
+
timestamp: Date.now(),
|
|
311
|
+
});
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
enabled: connectionStatus === "connected" && runtimeState === "playing",
|
|
315
|
+
intervalMs: 16,
|
|
316
|
+
},
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
if (connectionStatus === "connecting") {
|
|
320
|
+
return <div>Connecting to room {roomId}...</div>;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (connectionStatus === "disconnected") {
|
|
324
|
+
return (
|
|
325
|
+
<div>
|
|
326
|
+
Disconnected. <button onClick={reconnect}>Retry</button>
|
|
327
|
+
</div>
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return (
|
|
332
|
+
<div className="flex h-dvh flex-col bg-zinc-950 p-4 text-white">
|
|
333
|
+
<div className="mb-2 text-xs uppercase">Room {roomId}</div>
|
|
334
|
+
<div className="mb-2 text-xs uppercase">
|
|
335
|
+
Layout {controllerOrientation}
|
|
336
|
+
</div>
|
|
337
|
+
<Joystick
|
|
338
|
+
onMove={(x, y) => {
|
|
339
|
+
vectorRef.current = { x, y };
|
|
340
|
+
}}
|
|
341
|
+
/>
|
|
342
|
+
<FireButton
|
|
343
|
+
onPress={() => (actionRef.current = true)}
|
|
344
|
+
onRelease={() => (actionRef.current = false)}
|
|
345
|
+
/>
|
|
346
|
+
</div>
|
|
347
|
+
);
|
|
348
|
+
};
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
### `useControllerTick`
|
|
352
|
+
|
|
353
|
+
Fixed-cadence helper for controller loops. Use it with `useInputWriter` instead of hand-rolled `setInterval` loops.
|
|
354
|
+
|
|
355
|
+
### `useHostTick`
|
|
356
|
+
|
|
357
|
+
Canonical host loop helper. Pass a named options object with `onTick` plus
|
|
358
|
+
the loop mode/options.
|
|
359
|
+
|
|
360
|
+
- `mode: "raf"` (default) for render-aligned loops.
|
|
361
|
+
- `mode: "interval"` for fixed-cadence polling loops.
|
|
362
|
+
- `mode: "fixed"` for deterministic fixed-step simulation loops.
|
|
363
|
+
|
|
364
|
+
In fixed mode, pass `onFrame` when rendering should happen every animation
|
|
365
|
+
frame while simulation still advances at the fixed cadence. The frame info
|
|
366
|
+
includes `fixedStepAlpha`, which games can use to interpolate between previous
|
|
367
|
+
and current simulation state.
|
|
368
|
+
|
|
369
|
+
```tsx
|
|
370
|
+
useHostTick({
|
|
371
|
+
mode: "fixed",
|
|
372
|
+
intervalMs: 16,
|
|
373
|
+
onTick: ({ deltaSeconds }) => {
|
|
374
|
+
stepSimulation(deltaSeconds);
|
|
375
|
+
},
|
|
376
|
+
onFrame: ({ fixedStepAlpha }) => {
|
|
377
|
+
renderInterpolatedFrame(fixedStepAlpha);
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
**Auto Room Join from URL:**
|
|
383
|
+
|
|
384
|
+
Controllers automatically join rooms from URL query parameters:
|
|
385
|
+
|
|
386
|
+
```
|
|
387
|
+
https://yourgame.com/controller?room=ABCD
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
This is how QR code scanning works—the host generates a URL with the room code embedded.
|
|
391
|
+
|
|
392
|
+
---
|
|
393
|
+
|
|
394
|
+
## Advanced (Non-default)
|
|
395
|
+
|
|
396
|
+
Use these only when integrating platform-specific runtime behavior.
|
|
397
|
+
|
|
398
|
+
### Raw socket access (escape hatch)
|
|
399
|
+
|
|
400
|
+
```tsx
|
|
401
|
+
const host = useAirJamHost();
|
|
402
|
+
host.socket.emit("custom:event", { value: 1 });
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### Internal context hooks
|
|
406
|
+
|
|
407
|
+
`useAirJamContext`, `useAirJamConfig`, `useAirJamState`, and `useAirJamSocket` are intentionally internal and are not part of the root public SDK surface.
|
|
408
|
+
|
|
409
|
+
---
|
|
410
|
+
|
|
411
|
+
## Diagnostics
|
|
412
|
+
|
|
413
|
+
Air Jam emits structured diagnostics for common misuse paths in development builds.
|
|
414
|
+
|
|
415
|
+
```tsx
|
|
416
|
+
import { onAirJamDiagnostic, setAirJamDiagnosticsEnabled } from "@air-jam/sdk";
|
|
417
|
+
|
|
418
|
+
setAirJamDiagnosticsEnabled(true);
|
|
419
|
+
|
|
420
|
+
const unsubscribe = onAirJamDiagnostic((diagnostic) => {
|
|
421
|
+
console.log(diagnostic.code, diagnostic.message, diagnostic.details);
|
|
422
|
+
});
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
### Diagnostics Reference
|
|
426
|
+
|
|
427
|
+
| Code | Meaning | Expected Fix |
|
|
428
|
+
| ------------------------------------------ | --------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
|
|
429
|
+
| `AJ_SCOPE_MISMATCH` | A host/controller hook was used in the wrong scoped provider tree. | Use the correct scoped wrapper (`airjam.Host` / `airjam.Controller`) or the matching session provider. |
|
|
430
|
+
| `AJ_MISSING_SESSION_PROVIDER` | SDK context was read outside Air Jam session providers. | Wrap the route/root with `airjam.Host`, `airjam.Controller`, or the matching low-level session provider. |
|
|
431
|
+
| `AJ_CONFIG_MISSING_RUNTIME_TOPOLOGY` | Runtime config cannot resolve the complete topology contract. | Provide `topology` directly or enable environment resolution for a supported runtime mode. |
|
|
432
|
+
| `AJ_CONFIG_MISSING_APP_ID` | Production runtime started without canonical app ID config. | Set `VITE_AIR_JAM_APP_ID` / `NEXT_PUBLIC_AIR_JAM_APP_ID` or pass `appId` explicitly. |
|
|
433
|
+
| `AJ_STORE_ACTION_SESSION_NOT_READY` | Controller action dispatch attempted before role/room was ready. | Wait until room/session is established before dispatching store actions. |
|
|
434
|
+
| `AJ_STORE_ACTION_SOCKET_DISCONNECTED` | Controller state action dispatch attempted while socket disconnected. | Reconnect before dispatching action. |
|
|
435
|
+
| `AJ_STORE_ACTION_PAYLOAD_INVALID_SHAPE` | State action payload root was not omitted or a plain object. | Dispatch no payload or one plain object payload only. |
|
|
436
|
+
| `AJ_STORE_ACTION_PAYLOAD_NOT_SERIALIZABLE` | State action payload contained non-serializable values. | Use JSON-safe payload objects only (no functions/symbols/cycles). |
|
|
437
|
+
| `AJ_INPUT_WRITER_INVALID_SHAPE` | `useInputWriter` payload was not an object. | Send plain object payloads only. |
|
|
438
|
+
| `AJ_INPUT_WRITER_NOT_SERIALIZABLE` | Input payload contained non-serializable values. | Keep input payload JSON-safe. |
|
|
439
|
+
| `AJ_INPUT_WRITER_SESSION_NOT_READY` | Input writer called before room/controller identity was ready. | Start publish loop after controller connection/session is ready. |
|
|
440
|
+
| `AJ_INPUT_WRITER_SOCKET_DISCONNECTED` | Input writer called while controller socket was disconnected. | Reconnect before sending input. |
|
|
441
|
+
| `AJ_INPUT_WRITER_SCHEMA_INVALID` | Input payload failed runtime schema validation. | Match payload shape to configured input schema. |
|
|
442
|
+
|
|
443
|
+
---
|
|
444
|
+
|
|
445
|
+
### `PlayerProfile`
|
|
446
|
+
|
|
447
|
+
```typescript
|
|
448
|
+
interface PlayerProfile {
|
|
449
|
+
id: string; // Unique controller ID
|
|
450
|
+
label: string; // Display name (e.g., "Player 1")
|
|
451
|
+
color?: string; // Assigned color (e.g., "#FF5733")
|
|
452
|
+
avatarId?: string; // Preset avatar id chosen by the player
|
|
453
|
+
}
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
### `ConnectionStatus`
|
|
457
|
+
|
|
458
|
+
```typescript
|
|
459
|
+
type ConnectionStatus =
|
|
460
|
+
| "idle" // Not yet connected
|
|
461
|
+
| "connecting" // Connection in progress
|
|
462
|
+
| "connected" // Successfully connected
|
|
463
|
+
| "disconnected" // Connection lost
|
|
464
|
+
| "reconnecting"; // Attempting to reconnect
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
### `RuntimeState`
|
|
468
|
+
|
|
469
|
+
```typescript
|
|
470
|
+
type RuntimeState = "playing" | "paused";
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
### `HapticSignalPayload`
|
|
474
|
+
|
|
475
|
+
```typescript
|
|
476
|
+
interface HapticSignalPayload {
|
|
477
|
+
pattern: "light" | "medium" | "heavy" | "success" | "failure" | "custom";
|
|
478
|
+
sequence?: number | number[]; // For "custom" pattern
|
|
479
|
+
}
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
### `ToastSignalPayload`
|
|
483
|
+
|
|
484
|
+
```typescript
|
|
485
|
+
interface ToastSignalPayload {
|
|
486
|
+
message: string;
|
|
487
|
+
color?: string; // Optional accent color (e.g., "#22c55e")
|
|
488
|
+
duration?: number; // Optional duration in ms
|
|
489
|
+
}
|
|
490
|
+
```
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
<!-- Generated from content/docs/for-agents/state-and-rendering/page.mdx. Do not edit directly. -->
|
|
2
|
+
<!-- Canonical public doc: https://airjam.io/docs/for-agents/state-and-rendering -->
|
|
3
|
+
|
|
4
|
+
# State and Rendering
|
|
5
|
+
|
|
6
|
+
Air Jam projects often combine React, Zustand, per-frame gameplay logic, and sometimes R3F or Three.
|
|
7
|
+
|
|
8
|
+
That stack works well only when state ownership is explicit.
|
|
9
|
+
|
|
10
|
+
## Core State Rules
|
|
11
|
+
|
|
12
|
+
1. keep authoritative gameplay state separate from local UI state
|
|
13
|
+
2. avoid one mega store for unrelated concerns
|
|
14
|
+
3. use narrow selectors when consuming Zustand state
|
|
15
|
+
4. keep store writes intentional and coarse
|
|
16
|
+
5. keep per-frame mutable values out of React state when React rendering is not needed
|
|
17
|
+
|
|
18
|
+
These are framework-level rules about ownership and performance.
|
|
19
|
+
They do not require one exact store or engine file layout.
|
|
20
|
+
|
|
21
|
+
## React Rules
|
|
22
|
+
|
|
23
|
+
1. do not run simulation through React rerenders
|
|
24
|
+
2. keep business logic out of presentational components
|
|
25
|
+
3. use refs for hot mutable runtime values
|
|
26
|
+
4. keep component boundaries small and purposeful
|
|
27
|
+
|
|
28
|
+
## Starter Template Reference
|
|
29
|
+
|
|
30
|
+
The starter Pong template demonstrates one good starter implementation of the state/rendering split:
|
|
31
|
+
|
|
32
|
+
1. `src/game/stores/pong-store.ts` owns Air Jam store wiring
|
|
33
|
+
2. `src/game/stores/pong-store-state.ts` owns pure state transitions
|
|
34
|
+
3. `src/game/engine/runtime-state.ts` owns per-frame mutable values
|
|
35
|
+
4. `src/game/engine/simulation.ts` owns update ordering and orchestration
|
|
36
|
+
5. `src/game/ui/` owns reusable game-facing presentation that host and controller surfaces can share
|
|
37
|
+
|
|
38
|
+
That split is a recommended starter pattern, not a mandatory framework contract.
|
|
39
|
+
If the game needs a different module layout, preserve the same ownership boundaries rather than copying the filenames literally.
|
|
40
|
+
|
|
41
|
+
## Starter Testing Pattern
|
|
42
|
+
|
|
43
|
+
The starter template also mirrors those boundaries in tests:
|
|
44
|
+
|
|
45
|
+
1. `tests/game/domain/` for pure gameplay rules
|
|
46
|
+
2. `tests/game/stores/` for pure state transitions
|
|
47
|
+
3. `tests/game/engine/` for focused runtime helpers
|
|
48
|
+
4. `tests/game/adapters/` for transport-facing mapping
|
|
49
|
+
5. `tests/game/ui/` for shared game-facing UI modules that do not require full host/controller shells
|
|
50
|
+
|
|
51
|
+
The default order is:
|
|
52
|
+
|
|
53
|
+
1. test pure logic first
|
|
54
|
+
2. test thin adapter boundaries second
|
|
55
|
+
3. add heavier runtime or browser-facing coverage only where the integration boundary actually matters
|
|
56
|
+
|
|
57
|
+
## R3F and Three Rules
|
|
58
|
+
|
|
59
|
+
1. avoid rerendering scene components on every frame
|
|
60
|
+
2. mutate refs in frame loops when visual updates do not require React state
|
|
61
|
+
3. keep rendering integration separate from pure game rules
|
|
62
|
+
4. use instancing when repeated objects justify it
|
|
63
|
+
|
|
64
|
+
## Air Jam Lane Rules
|
|
65
|
+
|
|
66
|
+
Keep the framework lanes separate:
|
|
67
|
+
|
|
68
|
+
1. input lane for high-frequency player control input
|
|
69
|
+
2. replicated state lane for replayable shared truth
|
|
70
|
+
3. signal lane for coarse UX and system events
|
|
71
|
+
|
|
72
|
+
Do not use replicated store actions as a substitute for per-frame input handling.
|
|
73
|
+
|
|
74
|
+
## Common Pitfalls
|
|
75
|
+
|
|
76
|
+
1. putting gameplay logic directly in render components
|
|
77
|
+
2. using React state as the simulation engine
|
|
78
|
+
3. selecting whole stores across many components
|
|
79
|
+
4. mixing debug state into hot runtime paths
|
|
80
|
+
5. solving every state problem with more component state
|