@air-jam/cli 0.0.0-stage → 1.0.0-beta.1
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 +273 -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/auth-profile.d.ts +5 -0
- package/dist/auth-profile.js +7 -0
- package/dist/auth-profile.js.map +1 -0
- package/dist/auth-sessions.d.ts +5 -0
- package/dist/auth-sessions.js +7 -0
- package/dist/auth-sessions.js.map +1 -0
- package/dist/chunk-7D4AYBAV.js +64 -0
- package/dist/chunk-7D4AYBAV.js.map +1 -0
- package/dist/chunk-J5MAU4QZ.js +102 -0
- package/dist/chunk-J5MAU4QZ.js.map +1 -0
- package/dist/chunk-LH7UO57C.js +23 -0
- package/dist/chunk-LH7UO57C.js.map +1 -0
- package/dist/chunk-N7AI7HZK.js +529 -0
- package/dist/chunk-N7AI7HZK.js.map +1 -0
- package/dist/chunk-PKTLVQXZ.js +82 -0
- package/dist/chunk-PKTLVQXZ.js.map +1 -0
- package/dist/chunk-SQE4NPZP.js +31 -0
- package/dist/chunk-SQE4NPZP.js.map +1 -0
- package/dist/chunk-U6V3HEBB.js +939 -0
- package/dist/chunk-U6V3HEBB.js.map +1 -0
- package/dist/chunk-VEHNFPEO.js +224 -0
- package/dist/chunk-VEHNFPEO.js.map +1 -0
- package/dist/development.d.ts +102 -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/game-analytics.d.ts +5 -0
- package/dist/game-analytics.js +7 -0
- package/dist/game-analytics.js.map +1 -0
- package/dist/game-app-id.d.ts +5 -0
- package/dist/game-app-id.js +7 -0
- package/dist/game-app-id.js.map +1 -0
- package/dist/game-media.d.ts +5 -0
- package/dist/game-media.js +7 -0
- package/dist/game-media.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2468 -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 +82 -3
- package/runtime/local-network.d.mts +6 -0
- package/runtime/local-network.mjs +9 -0
- package/runtime/public-assets.mjs +62 -0
- package/runtime/source-fence.mjs +182 -0
- package/runtime/vite-config.d.mts +43 -0
- package/runtime/vite-config.mjs +134 -0
- package/runtime/vite-dev.mjs +18 -0
- package/runtime/vite-https.d.mts +11 -0
- package/runtime/vite-https.mjs +40 -0
- package/template-assets/bootstrap/.claude/launch.json +12 -0
- package/template-assets/bootstrap/AGENTS.md +290 -0
- package/template-assets/bootstrap/CLAUDE.md +55 -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 +100 -0
- package/template-assets/bootstrap/skills/canvas-2d-gameplay/SKILL.md +47 -0
- package/template-assets/bootstrap/skills/controller-ui/SKILL.md +71 -0
- package/template-assets/bootstrap/skills/debug-and-test/SKILL.md +72 -0
- package/template-assets/bootstrap/skills/game-architecture/SKILL.md +102 -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 +58 -0
- package/template-assets/bootstrap/skills/r3f-3d-scene/SKILL.md +61 -0
- package/template-assets/documentation/for-agents/controller-ui/page.mdx +99 -0
- package/template-assets/documentation/for-agents/page.mdx +120 -0
- package/template-assets/documentation/for-agents/project-structure/page.mdx +170 -0
- package/template-assets/documentation/for-agents/state-and-rendering/page.mdx +77 -0
- package/template-assets/documentation/for-agents/supported-agents/page.mdx +18 -0
- package/template-assets/documentation/getting-started/debugging/page.mdx +164 -0
- package/template-assets/documentation/getting-started/dev-logs/page.mdx +169 -0
- package/template-assets/documentation/getting-started/game-ideas/page.mdx +114 -0
- package/template-assets/documentation/getting-started/introduction/page.mdx +200 -0
- package/template-assets/documentation/getting-started/migrating-legacy-games/page.mdx +673 -0
- package/template-assets/documentation/getting-started/quick-start/page.mdx +310 -0
- package/template-assets/documentation/how-it-works/architecture/page.mdx +230 -0
- package/template-assets/documentation/how-it-works/host-system/page.mdx +270 -0
- package/template-assets/documentation/manifest.json +500 -0
- package/template-assets/documentation/sdk/assets-and-theme/page.mdx +185 -0
- package/template-assets/documentation/sdk/hooks/page.mdx +435 -0
- package/template-assets/documentation/sdk/input-system/page.mdx +240 -0
- package/template-assets/documentation/sdk/networked-state/page.mdx +295 -0
- package/template-assets/documentation/sdk/settings/page.mdx +155 -0
- package/template-assets/documentation/sdk/ui-components/page.mdx +268 -0
- package/template-assets/managed/.airjam/ai-pack.json +209 -0
- package/template-assets/managed/docs/airjam/agent-gold-path.md +115 -0
- package/template-assets/managed/docs/airjam/agent-mcp.md +179 -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 +104 -0
- package/template-assets/managed/docs/airjam/development-loop.md +51 -0
- package/template-assets/managed/docs/airjam/docs-index.md +64 -0
- package/template-assets/managed/docs/airjam/generated/architecture.md +223 -0
- package/template-assets/managed/docs/airjam/generated/assets-and-theme.md +188 -0
- package/template-assets/managed/docs/airjam/generated/controller-ui.md +102 -0
- package/template-assets/managed/docs/airjam/generated/debugging-and-logs.md +165 -0
- package/template-assets/managed/docs/airjam/generated/for-agents.md +123 -0
- package/template-assets/managed/docs/airjam/generated/game-ideas.md +107 -0
- package/template-assets/managed/docs/airjam/generated/host-system.md +261 -0
- package/template-assets/managed/docs/airjam/generated/input-system.md +241 -0
- package/template-assets/managed/docs/airjam/generated/introduction.md +195 -0
- package/template-assets/managed/docs/airjam/generated/migrating-legacy-games.md +674 -0
- package/template-assets/managed/docs/airjam/generated/networked-state.md +298 -0
- package/template-assets/managed/docs/airjam/generated/project-structure.md +171 -0
- package/template-assets/managed/docs/airjam/generated/quick-start.md +311 -0
- package/template-assets/managed/docs/airjam/generated/sdk-hooks.md +430 -0
- package/template-assets/managed/docs/airjam/generated/settings.md +158 -0
- package/template-assets/managed/docs/airjam/generated/state-and-rendering.md +80 -0
- package/template-assets/managed/docs/airjam/generated/supported-agents.md +21 -0
- package/template-assets/managed/docs/airjam/generated/ui-components.md +265 -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 +212 -0
- package/template-assets/managed/docs/airjam/preview-controllers.md +129 -0
- package/template-assets/managed/docs/airjam/r3f-3d-scene.md +163 -0
- package/template-assets/managed/docs/airjam/state-lanes-cookbook.md +101 -0
- package/template-assets/managed/docs/airjam/visual-system.md +103 -0
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
# SDK Hooks
|
|
2
|
+
|
|
3
|
+
The Air Jam SDK provides React hooks for building multiplayer games. This page documents all available hooks and their usage.
|
|
4
|
+
|
|
5
|
+
## Runtime Boundaries
|
|
6
|
+
|
|
7
|
+
### `createAirJamApp`
|
|
8
|
+
|
|
9
|
+
Declare the game once with `defineAirJamGame` from `@air-jam/sdk/core` in
|
|
10
|
+
`src/airjam.config.ts`. In `app.tsx`, `createAirJamApp(game, options)` binds that
|
|
11
|
+
declaration to the browser environment and returns `Host`, `Controller`,
|
|
12
|
+
`paths` and `session`. Its capacity comes from `game.metadata.maxPlayers`.
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { createAirJamApp, env } from "@air-jam/sdk";
|
|
16
|
+
import { airjam as game } from "./airjam.config";
|
|
17
|
+
|
|
18
|
+
export const airjam = createAirJamApp(game, {
|
|
19
|
+
runtime: env.vite(import.meta.env),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
<Routes>
|
|
23
|
+
<Route
|
|
24
|
+
path="/"
|
|
25
|
+
element={
|
|
26
|
+
<airjam.Host>
|
|
27
|
+
<HostView />
|
|
28
|
+
</airjam.Host>
|
|
29
|
+
}
|
|
30
|
+
/>
|
|
31
|
+
<Route
|
|
32
|
+
path={airjam.paths.controller}
|
|
33
|
+
element={
|
|
34
|
+
<airjam.Controller>
|
|
35
|
+
<ControllerView />
|
|
36
|
+
</airjam.Controller>
|
|
37
|
+
}
|
|
38
|
+
/>
|
|
39
|
+
</Routes>;
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**Environment Variables:**
|
|
43
|
+
|
|
44
|
+
The provider automatically reads from these environment variables if props aren't provided:
|
|
45
|
+
|
|
46
|
+
- `VITE_AIR_JAM_SERVER_URL` / `NEXT_PUBLIC_AIR_JAM_SERVER_URL` - WebSocket server URL
|
|
47
|
+
- `VITE_AIR_JAM_APP_ID` / `NEXT_PUBLIC_AIR_JAM_APP_ID` - Public app ID
|
|
48
|
+
|
|
49
|
+
Session runtimes mount only in the browser: `airjam.Host`, `airjam.Controller`
|
|
50
|
+
and `AirJamHostRuntime` / `AirJamControllerRuntime` from
|
|
51
|
+
`@air-jam/sdk/arcade/runtime` render nothing on the server, so a server-rendered
|
|
52
|
+
page emits nothing inside them until it hydrates.
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Host Hooks
|
|
57
|
+
|
|
58
|
+
### `useAirJamHost`
|
|
59
|
+
|
|
60
|
+
Consumer hook for host runtime state and actions. Mount `airjam.Host` once at the boundary, then call `useAirJamHost()` anywhere below it.
|
|
61
|
+
|
|
62
|
+
Call `useAirJamHost(selector)` when a component only needs a narrow slice of
|
|
63
|
+
host session state and you want to avoid rerendering on unrelated runtime
|
|
64
|
+
fields.
|
|
65
|
+
Selectors receive state fields only; call `useAirJamHost()` when a component
|
|
66
|
+
also needs runtime controls such as `joinUrl` or `sendSignal`.
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
import { useAirJamHost } from "@air-jam/sdk";
|
|
70
|
+
import { airjam } from "../app";
|
|
71
|
+
|
|
72
|
+
const HostShell = () => (
|
|
73
|
+
<airjam.Host
|
|
74
|
+
roomId="GAME"
|
|
75
|
+
onPlayerJoin={(player) => {
|
|
76
|
+
console.log(`${player.label} joined with color ${player.color}`);
|
|
77
|
+
}}
|
|
78
|
+
onPlayerLeave={(controllerId) => {
|
|
79
|
+
console.log(`Player ${controllerId} left`);
|
|
80
|
+
}}
|
|
81
|
+
>
|
|
82
|
+
<HostView />
|
|
83
|
+
</airjam.Host>
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
const HostView = () => {
|
|
87
|
+
const host = useAirJamHost();
|
|
88
|
+
|
|
89
|
+
// Return values
|
|
90
|
+
const {
|
|
91
|
+
roomId, // "ABCD" - The room code
|
|
92
|
+
joinUrl, // Full URL for controllers to join
|
|
93
|
+
joinUrlStatus, // "loading" | "ready" | "unavailable"
|
|
94
|
+
connectionStatus, // "connected" | "connecting" | "reconnecting" | "disconnected" | "idle"
|
|
95
|
+
players, // Array of AirJamPlayer, including phones that are away
|
|
96
|
+
queuePosition, // Place in a busy hosted service's queue, or null
|
|
97
|
+
runtimeState, // "playing" | "paused"
|
|
98
|
+
lastError, // Error message if any
|
|
99
|
+
mode, // "standalone" | "platform"
|
|
100
|
+
|
|
101
|
+
// Functions
|
|
102
|
+
sendSignal, // Send haptics/toasts to controllers
|
|
103
|
+
pauseRuntime, // Set runtimeState to "paused"
|
|
104
|
+
resumeRuntime, // Set runtimeState to "playing"
|
|
105
|
+
setRuntimeState, // Explicitly set "playing" | "paused"
|
|
106
|
+
} = host;
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<div className="relative min-h-screen bg-black text-white">
|
|
110
|
+
<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">
|
|
111
|
+
<span>Room {roomId}</span>
|
|
112
|
+
<button
|
|
113
|
+
onClick={runtimeState === "playing" ? pauseRuntime : resumeRuntime}
|
|
114
|
+
>
|
|
115
|
+
{runtimeState === "playing" ? "Pause" : "Resume"}
|
|
116
|
+
</button>
|
|
117
|
+
</header>
|
|
118
|
+
<GameCanvas />
|
|
119
|
+
</div>
|
|
120
|
+
);
|
|
121
|
+
};
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Each player is a seat the room assigned (`p1`, `p2`, …). A phone that drops
|
|
125
|
+
keeps its seat for two minutes and shows `connected: false` meanwhile;
|
|
126
|
+
reconnecting is automatic.
|
|
127
|
+
|
|
128
|
+
Players' controls reach the game only in its simulation steps, through
|
|
129
|
+
`input(playerId)`; see [Input System](/docs/sdk/input-system).
|
|
130
|
+
|
|
131
|
+
Pause and orientation are the only presentation the host sends phones
|
|
132
|
+
directly. Authoritative multiplayer state lives in the networked stores; a
|
|
133
|
+
short message for the players is a toast signal or a store field.
|
|
134
|
+
|
|
135
|
+
A game whose `inputModalities` include `'joystick'` always gets a landscape controller; the SDK applies it, so do not set orientation; lay it out with `GamepadLayout` from `@air-jam/sdk/ui`: the stick under the left thumb, the actions under the right in a square the same size, and only short status between them.
|
|
136
|
+
|
|
137
|
+
**Sending Haptic Feedback:**
|
|
138
|
+
|
|
139
|
+
```tsx
|
|
140
|
+
// Vibrate a specific player's phone
|
|
141
|
+
host.sendSignal("HAPTIC", { pattern: "heavy" }, playerId);
|
|
142
|
+
|
|
143
|
+
// Available patterns: "light", "medium", "heavy", "success", "failure", "custom"
|
|
144
|
+
host.sendSignal(
|
|
145
|
+
"HAPTIC",
|
|
146
|
+
{
|
|
147
|
+
pattern: "custom",
|
|
148
|
+
sequence: [50, 100, 50], // Vibrate 50ms, pause 100ms, vibrate 50ms
|
|
149
|
+
},
|
|
150
|
+
playerId,
|
|
151
|
+
);
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
**Sending Toast Notifications:**
|
|
155
|
+
|
|
156
|
+
```tsx
|
|
157
|
+
// Show notification on a player's controller
|
|
158
|
+
host.sendSignal(
|
|
159
|
+
"TOAST",
|
|
160
|
+
{
|
|
161
|
+
message: "Achievement unlocked: First blood",
|
|
162
|
+
color: "#22c55e", // Optional accent color
|
|
163
|
+
duration: 2000, // Optional duration in ms
|
|
164
|
+
},
|
|
165
|
+
playerId,
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
// Broadcast to all players (omit targetId)
|
|
169
|
+
host.sendSignal("TOAST", {
|
|
170
|
+
message: "Round start! Get ready to fight.",
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
### `useSendSignal`
|
|
177
|
+
|
|
178
|
+
Lightweight hook for sending signals without triggering re-renders. Use in collision handlers.
|
|
179
|
+
|
|
180
|
+
```tsx
|
|
181
|
+
import { useSendSignal } from "@air-jam/sdk";
|
|
182
|
+
|
|
183
|
+
const Laser = ({ ownerId }: { ownerId: string }) => {
|
|
184
|
+
const sendSignal = useSendSignal();
|
|
185
|
+
|
|
186
|
+
const handleHit = (targetId: string) => {
|
|
187
|
+
// Vibrate the player who got hit
|
|
188
|
+
sendSignal("HAPTIC", { pattern: "heavy" }, targetId);
|
|
189
|
+
|
|
190
|
+
// Light feedback for the shooter
|
|
191
|
+
sendSignal("HAPTIC", { pattern: "light" }, ownerId);
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// Collision detection...
|
|
195
|
+
};
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
## Controller Hooks
|
|
201
|
+
|
|
202
|
+
### `useAirJamController`
|
|
203
|
+
|
|
204
|
+
Consumer hook for controller runtime state and actions. Mount `airjam.Controller` once at the boundary, then call `useAirJamController()` below it.
|
|
205
|
+
|
|
206
|
+
Call `useAirJamController(selector)` when a component only needs a narrow slice
|
|
207
|
+
of controller session state and should not rerender on unrelated runtime
|
|
208
|
+
updates.
|
|
209
|
+
Selectors receive state fields only; call `useAirJamController()` when a
|
|
210
|
+
component also needs controls such as `sendSystemCommand`.
|
|
211
|
+
|
|
212
|
+
A controller page keeps its seat's key for each room it joins, so a reload or a
|
|
213
|
+
dropped network returns the phone to the same seat.
|
|
214
|
+
|
|
215
|
+
```tsx
|
|
216
|
+
import { useAirJamController } from "@air-jam/sdk";
|
|
217
|
+
import { ControlButton, Stick } from "@air-jam/sdk/ui";
|
|
218
|
+
import { airjam } from "../app";
|
|
219
|
+
import { shipInput } from "../game/contracts/input";
|
|
220
|
+
|
|
221
|
+
const ControllerShell = () => (
|
|
222
|
+
<airjam.Controller roomId="ABCD" nickname="Player1">
|
|
223
|
+
<ControllerView />
|
|
224
|
+
</airjam.Controller>
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
const ControllerView = () => {
|
|
228
|
+
const controller = useAirJamController();
|
|
229
|
+
|
|
230
|
+
const {
|
|
231
|
+
roomId, // Room code (from URL or props)
|
|
232
|
+
controllerId, // This phone's seat ("p1", "p2", …)
|
|
233
|
+
connectionStatus, // Connection state
|
|
234
|
+
runtimeState, // "playing" | "paused"
|
|
235
|
+
controllerOrientation, // Declared session orientation; always landscape for joystick games
|
|
236
|
+
|
|
237
|
+
// Functions
|
|
238
|
+
sendSystemCommand, // "pause" | "resume"
|
|
239
|
+
setNickname, // Update nickname
|
|
240
|
+
setAvatarId, // Update avatar selection draft
|
|
241
|
+
updatePlayerProfile, // Patch live label/avatar when connected
|
|
242
|
+
selfPlayer, // This controller's player profile when known
|
|
243
|
+
leave, // Give up the seat
|
|
244
|
+
input, // This player's declared controls, which phone controls fill
|
|
245
|
+
} = controller;
|
|
246
|
+
|
|
247
|
+
if (connectionStatus === "connecting") {
|
|
248
|
+
return <div>Connecting to room {roomId}...</div>;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (connectionStatus === "reconnecting") {
|
|
252
|
+
return <div>Reconnecting…</div>;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return (
|
|
256
|
+
<div className="flex h-dvh flex-col bg-zinc-950 p-4 text-white">
|
|
257
|
+
<div className="mb-2 text-xs uppercase">Room {roomId}</div>
|
|
258
|
+
<div className="mb-2 text-xs uppercase">
|
|
259
|
+
Layout {controllerOrientation}
|
|
260
|
+
</div>
|
|
261
|
+
<Stick control={shipInput.move} className="size-48 rounded-full" />
|
|
262
|
+
<ControlButton control={shipInput.fire}>Fire</ControlButton>
|
|
263
|
+
</div>
|
|
264
|
+
);
|
|
265
|
+
};
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Phone controls send the declared controls themselves; a controller runs no
|
|
269
|
+
send loop.
|
|
270
|
+
|
|
271
|
+
### Game simulation
|
|
272
|
+
|
|
273
|
+
A game's authoritative state that changes every step, such as positions,
|
|
274
|
+
timers and physics, lives in a declared simulation. The SDK steps it on the
|
|
275
|
+
host at a fixed rate with seeded randomness, so the same seed and inputs replay
|
|
276
|
+
the same game, in a browser or headless and faster than real time. It only
|
|
277
|
+
steps while the room is playing: pausing freezes the game and its game time.
|
|
278
|
+
|
|
279
|
+
```ts filename="src/game/engine/simulation.ts"
|
|
280
|
+
import { defineAirJamSimulation } from "@air-jam/sdk/core";
|
|
281
|
+
|
|
282
|
+
export const simulation = defineAirJamSimulation<Sim, GameInput, SimEvent>({
|
|
283
|
+
stepMs: 1000 / 60,
|
|
284
|
+
create: ({ random }) => createSim(random),
|
|
285
|
+
step: (sim, { input, players, random, emit }) => {
|
|
286
|
+
advance(sim, players, input);
|
|
287
|
+
},
|
|
288
|
+
// What agents and tests see of the game, as plain JSON.
|
|
289
|
+
observe: (sim) => ({ fighters: sim.fighters }),
|
|
290
|
+
});
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
Declare it with `defineAirJamGame({ simulation })`. When its code is heavy, such
|
|
294
|
+
as a physics engine, declare a loader instead,
|
|
295
|
+
`simulation: () => import("./game/engine/simulation").then((m) => m.simulation)`,
|
|
296
|
+
so only the host downloads it and controllers stay small. The host draws and
|
|
297
|
+
plays feedback from it, never advances it:
|
|
298
|
+
|
|
299
|
+
- `useSimulationFrame(({ sim, alpha }) => draw(sim, alpha))` runs after every
|
|
300
|
+
display frame; `alpha` interpolates between steps.
|
|
301
|
+
- `useSimulationEvents((event) => playSound(event))` receives what `step`
|
|
302
|
+
emitted, such as a hit.
|
|
303
|
+
- `useSimulationSelector((sim) => value, isEqual?)` reads a value for UI, such
|
|
304
|
+
as a health bar or an entity list, re-rendering only when it changes.
|
|
305
|
+
- `useSimulation()` returns the clock itself.
|
|
306
|
+
- `const captureFrame = useHostAgentCapture((target, options) => drawAgentCanvas(canvas, target, options))`
|
|
307
|
+
optionally gives agents a sharper host render surface. Supporting browsers
|
|
308
|
+
already capture the visible host stage, including HTML lobbies and nested canvases;
|
|
309
|
+
this callback takes priority when it returns true. Returning false (for example,
|
|
310
|
+
before a game canvas mounts) uses the native stage instead.
|
|
311
|
+
Call `captureFrame()` immediately after
|
|
312
|
+
drawing the game and its HUD. The SDK owns the persistent capture canvas and
|
|
313
|
+
records native video at 30 fps; still observations use the same drawing callback.
|
|
314
|
+
Still observations and frame-based test clips encode JPEGs asynchronously. The
|
|
315
|
+
clip sampler skips ticks while encoding is pending rather than queueing frames.
|
|
316
|
+
If you call `captureAgentCanvas()` directly, await its image result.
|
|
317
|
+
|
|
318
|
+
Step code reads stores with `gameStore.getState()` and dispatches with
|
|
319
|
+
`gameStore.actions`; it never reads `Date.now()` or `Math.random()`.
|
|
320
|
+
|
|
321
|
+
**Auto Room Join from URL:**
|
|
322
|
+
|
|
323
|
+
Controllers automatically join rooms from URL query parameters:
|
|
324
|
+
|
|
325
|
+
```
|
|
326
|
+
https://yourgame.com/controller?room=ABCD
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
This is how QR code scanning works—the host generates a URL with the room code embedded.
|
|
330
|
+
|
|
331
|
+
---
|
|
332
|
+
|
|
333
|
+
## Advanced (Non-default)
|
|
334
|
+
|
|
335
|
+
Use these only when integrating platform-specific runtime behavior.
|
|
336
|
+
|
|
337
|
+
### Room session access (escape hatch)
|
|
338
|
+
|
|
339
|
+
`host.room` and `controller.session` are the page's sessions in the room
|
|
340
|
+
(`@air-jam/sdk/room-session`). Networked stores and signals cover what games
|
|
341
|
+
need; reach for the session only for a channel of your own.
|
|
342
|
+
|
|
343
|
+
### Internal context hooks
|
|
344
|
+
|
|
345
|
+
`useAirJamContext`, `useAirJamConfig` and `useAirJamState` are intentionally internal and are not part of the root public SDK surface.
|
|
346
|
+
|
|
347
|
+
---
|
|
348
|
+
|
|
349
|
+
## Diagnostics
|
|
350
|
+
|
|
351
|
+
Air Jam emits structured diagnostics for common misuse paths in development builds.
|
|
352
|
+
|
|
353
|
+
```tsx
|
|
354
|
+
import { onAirJamDiagnostic, setAirJamDiagnosticsEnabled } from "@air-jam/sdk";
|
|
355
|
+
|
|
356
|
+
setAirJamDiagnosticsEnabled(true);
|
|
357
|
+
|
|
358
|
+
const unsubscribe = onAirJamDiagnostic((diagnostic) => {
|
|
359
|
+
console.log(diagnostic.code, diagnostic.message, diagnostic.details);
|
|
360
|
+
});
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### Diagnostics Reference
|
|
364
|
+
|
|
365
|
+
| Code | Meaning | Expected Fix |
|
|
366
|
+
| ------------------------------------------ | --------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
|
|
367
|
+
| `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. |
|
|
368
|
+
| `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. |
|
|
369
|
+
| `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. |
|
|
370
|
+
| `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. |
|
|
371
|
+
| `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. |
|
|
372
|
+
| `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. |
|
|
373
|
+
| `AJ_STORE_ACTION_PAYLOAD_NOT_SERIALIZABLE` | State action payload contained non-serializable values. | Use JSON-safe payload objects only (no functions/symbols/cycles). |
|
|
374
|
+
| `AJ_INPUT_INVALID` | Input from a controller or agent did not match the declared controls. | Send each declared control in its kind's shape; leave out controls not in use. |
|
|
375
|
+
| `AJ_INPUT_NOT_DECLARED` | A phone control rendered where the game declares no controls. | Declare them with `defineAirJamGame({ input })` and render controls inside `<airjam.Controller>`. |
|
|
376
|
+
|
|
377
|
+
---
|
|
378
|
+
|
|
379
|
+
## Types
|
|
380
|
+
|
|
381
|
+
### `AirJamPlayer`
|
|
382
|
+
|
|
383
|
+
```typescript
|
|
384
|
+
interface AirJamPlayer {
|
|
385
|
+
id: string; // The seat the room assigned ("p1", "p2", …)
|
|
386
|
+
label: string; // Display name (e.g., "Player 1")
|
|
387
|
+
color: string; // Assigned color (e.g., "#FF5733")
|
|
388
|
+
avatarId?: string; // Preset avatar id chosen by the player
|
|
389
|
+
kind: "phone" | "preview" | "agent";
|
|
390
|
+
connected: boolean; // false while the phone is away
|
|
391
|
+
}
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
### `ConnectionStatus`
|
|
395
|
+
|
|
396
|
+
```typescript
|
|
397
|
+
type ConnectionStatus =
|
|
398
|
+
| "idle" // Not yet connected
|
|
399
|
+
| "connecting" // Connection in progress
|
|
400
|
+
| "connected" // Successfully connected
|
|
401
|
+
| "disconnected" // Connection lost
|
|
402
|
+
| "reconnecting"; // Attempting to reconnect
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### `RuntimeState`
|
|
406
|
+
|
|
407
|
+
```typescript
|
|
408
|
+
type RuntimeState = "playing" | "paused";
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
### `HapticSignalPayload`
|
|
412
|
+
|
|
413
|
+
```typescript
|
|
414
|
+
interface HapticSignalPayload {
|
|
415
|
+
pattern: "light" | "medium" | "heavy" | "success" | "failure" | "custom";
|
|
416
|
+
sequence?: number | number[]; // For "custom" pattern
|
|
417
|
+
}
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### `ToastSignalPayload`
|
|
421
|
+
|
|
422
|
+
```typescript
|
|
423
|
+
interface ToastSignalPayload {
|
|
424
|
+
message: string;
|
|
425
|
+
color?: string; // Optional accent color (e.g., "#22c55e")
|
|
426
|
+
duration?: number; // Optional duration in ms
|
|
427
|
+
}
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
## useAirJamSettings
|
|
431
|
+
|
|
432
|
+
Host rendering reads declared settings with `useAirJamSettings(gameSettings)`.
|
|
433
|
+
The hook returns typed current values and subscribes to accepted host updates.
|
|
434
|
+
Controllers read display values copied to replicated store state. See
|
|
435
|
+
[Game Settings](/docs/sdk/settings) for the full contract.
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# Input System
|
|
2
|
+
|
|
3
|
+
A game declares the controls each player has. Phones, agents and replays fill
|
|
4
|
+
those controls; the host reads them once per simulation step, with a fixed
|
|
5
|
+
meaning for each kind of control.
|
|
6
|
+
|
|
7
|
+
## Declare Controls
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
// src/game/contracts/input.ts
|
|
11
|
+
import {
|
|
12
|
+
button,
|
|
13
|
+
defineAirJamInput,
|
|
14
|
+
stick,
|
|
15
|
+
type AirJamInputValues,
|
|
16
|
+
} from "@air-jam/sdk/core";
|
|
17
|
+
|
|
18
|
+
export const shipInput = defineAirJamInput({
|
|
19
|
+
move: stick(),
|
|
20
|
+
fire: button("pressed"),
|
|
21
|
+
boost: button("held"),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export type ShipInput = AirJamInputValues<typeof shipInput>;
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Declare them once in `src/airjam.config.ts` with `defineAirJamGame` from `@air-jam/sdk/core`:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
export const airjam = defineAirJamGame({
|
|
31
|
+
// ...
|
|
32
|
+
simulation: shipSimulation,
|
|
33
|
+
input: shipInput,
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Control Kinds
|
|
38
|
+
|
|
39
|
+
| Kind | Declared as | A step reads |
|
|
40
|
+
| ------------------------------ | -------------------------------- | ------------------------------------------------------------------------------------------ |
|
|
41
|
+
| Held button | `button("held")` | `true` while the button is down, and for at least one step after a tap shorter than a step |
|
|
42
|
+
| Pressed button | `button("pressed")` | `true` for exactly one step per press |
|
|
43
|
+
| Axis | `axis()` | a number from -1 to 1 |
|
|
44
|
+
| Stick | `stick()` | `{ x, y }`, each from -1 to 1; `y` is down, like screen space |
|
|
45
|
+
| Stick that keeps its direction | `stick({ keepDirection: true })` | the last direction while the stick is centred, as in snake |
|
|
46
|
+
|
|
47
|
+
Presses travel as a counter, so a tap between two network packets or two
|
|
48
|
+
simulation steps is never lost or doubled. Games never debounce or latch input
|
|
49
|
+
themselves.
|
|
50
|
+
|
|
51
|
+
## Read Controls In The Simulation
|
|
52
|
+
|
|
53
|
+
Input reaches a game only in its simulation steps:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
export const shipSimulation = defineAirJamSimulation<Sim, ShipInput>({
|
|
57
|
+
stepMs: 1000 / 60,
|
|
58
|
+
create: () => createSim(),
|
|
59
|
+
step: (sim, { input, players, dtSeconds }) => {
|
|
60
|
+
for (const player of players) {
|
|
61
|
+
const { move, fire, boost } = input(player.id);
|
|
62
|
+
steer(sim, player.id, move, boost, dtSeconds);
|
|
63
|
+
if (fire) shoot(sim, player.id);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`input(playerId)` always returns every control. A player who has sent nothing
|
|
70
|
+
yet reads as released and centred.
|
|
71
|
+
|
|
72
|
+
## Phone Controls
|
|
73
|
+
|
|
74
|
+
The SDK's phone controls fill the declared controls and send them; a game
|
|
75
|
+
never runs its own send loop.
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
import { AxisButton, ControlButton, Stick } from "@air-jam/sdk/ui";
|
|
79
|
+
|
|
80
|
+
<Stick control={shipInput.move} className="size-48 rounded-full bg-zinc-900" />
|
|
81
|
+
<ControlButton control={shipInput.fire} className="bg-red-500 data-[active=true]:ring-2">
|
|
82
|
+
Fire
|
|
83
|
+
</ControlButton>
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
1. `ControlButton` binds a button. Each touch, or Space and Enter while it has
|
|
87
|
+
focus, is one press.
|
|
88
|
+
2. `AxisButton` holds an axis at a value while held, such as a paddle's up and
|
|
89
|
+
down buttons. The latest held button wins.
|
|
90
|
+
3. `Stick` reads a drag anywhere inside it. `directions="4-way"` snaps it to a
|
|
91
|
+
pad, `deadzone` keeps small offsets centred, and a function child draws the
|
|
92
|
+
whole stick. It stays correct however the controller surface is rotated.
|
|
93
|
+
|
|
94
|
+
Every control sets `data-active` while held, so games style the held state
|
|
95
|
+
with their own classes. Controls release when the phone page loses focus.
|
|
96
|
+
|
|
97
|
+
The controller sends its controls once per display frame when they change, again
|
|
98
|
+
after joining, and on a slow keepalive. Nothing stamps input with a device
|
|
99
|
+
clock.
|
|
100
|
+
|
|
101
|
+
## Keyboards And Gamepads
|
|
102
|
+
|
|
103
|
+
Declare bindings for the controls and every keyboard layout and gamepad plays
|
|
104
|
+
as a local player, with no phone:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import {
|
|
108
|
+
arrows,
|
|
109
|
+
button,
|
|
110
|
+
defineAirJamInput,
|
|
111
|
+
defineAirJamInputBindings,
|
|
112
|
+
stick,
|
|
113
|
+
wasd,
|
|
114
|
+
} from "@air-jam/sdk/core";
|
|
115
|
+
|
|
116
|
+
export const shipInput = defineAirJamInput({
|
|
117
|
+
move: stick(),
|
|
118
|
+
fire: button("pressed"),
|
|
119
|
+
boost: button("held"),
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
export const shipInputBindings = defineAirJamInputBindings(shipInput, {
|
|
123
|
+
keyboard: [
|
|
124
|
+
{ move: wasd(), fire: "Space", boost: "ShiftLeft" },
|
|
125
|
+
{ move: arrows(), fire: "Enter", boost: "ShiftRight" },
|
|
126
|
+
],
|
|
127
|
+
gamepad: { move: "leftStick", fire: "south", boost: "rightTrigger" },
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// airjam.config.ts
|
|
131
|
+
defineAirJamGame({ input: shipInput, inputBindings: shipInputBindings, ... });
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
1. Each keyboard layout is one player; every connected gamepad is one more.
|
|
135
|
+
Keys are `KeyboardEvent.code` values. A button binds one or more keys, an
|
|
136
|
+
axis binds `keyAxis(negative, positive)`, and a stick binds `wasd()`,
|
|
137
|
+
`arrows()` or `keyStick({ up, left, down, right })`. A binding for a
|
|
138
|
+
control that isn't declared, or of the wrong kind, throws when declared.
|
|
139
|
+
2. Gamepad names follow the standard layout: `south`, `east`, `west`,
|
|
140
|
+
`north`, `leftBumper`, `rightBumper`, `leftTrigger`, `rightTrigger`,
|
|
141
|
+
`select`, `start`, stick presses and the d-pad for buttons;
|
|
142
|
+
`leftStickX`/`Y` and `rightStickX`/`Y` for axes; `leftStick`, `rightStick`
|
|
143
|
+
or `dpad` for sticks. Sticks get a small deadzone and stay y down.
|
|
144
|
+
3. A layout's first key, or a gamepad's first button or stick push, opens a
|
|
145
|
+
seat: a preview phone named "Keyboard 1" or "Gamepad 1" that joins like any
|
|
146
|
+
phone. Its own screen answers menus and lobby buttons; the keys or buttons
|
|
147
|
+
drive play. Minimizing that phone keeps it playing; removing it frees the
|
|
148
|
+
seat, and the next key opens a new one.
|
|
149
|
+
4. Keys and gamepads count only while the game page has focus, key repeats
|
|
150
|
+
never press, and losing focus or unplugging a gamepad releases what it
|
|
151
|
+
held. Keys aimed at the page's own buttons and fields stay theirs.
|
|
152
|
+
|
|
153
|
+
Local development's preview phones and Studio provide these seats; a page
|
|
154
|
+
with nobody to provide them leaves keys and gamepads alone. Game code never
|
|
155
|
+
listens to keys or polls gamepads itself.
|
|
156
|
+
|
|
157
|
+
A product framing a game offers its preview workspace's phones as that
|
|
158
|
+
game's seats by passing the game frame to `PreviewControllerWorkspace` as
|
|
159
|
+
`localPlayersGame`; `localPlayers={false}` offers none and closes the seats
|
|
160
|
+
it opened. The Arcade offers them only once a player turns keyboard and
|
|
161
|
+
gamepad players on.
|
|
162
|
+
|
|
163
|
+
A product framing its own phones launches each with
|
|
164
|
+
`createPreviewControllerLaunch` from `@air-jam/sdk/preview`, passing its
|
|
165
|
+
page's origin as `parentOrigin` and, for a seat, its label as `localSeat`. A
|
|
166
|
+
phone takes seat input and close requests only from its direct parent at that
|
|
167
|
+
origin, and answers only there; without `parentOrigin` that is the phone's own
|
|
168
|
+
origin, as the preview workspace launches it.
|
|
169
|
+
|
|
170
|
+
## Agents
|
|
171
|
+
|
|
172
|
+
Agents fill the same controls through leased participant control. They send
|
|
173
|
+
values: a button is `true` or `false`, an axis a number and a stick an
|
|
174
|
+
`{ x, y }`. A button going from `false` to `true` is one press; to press again,
|
|
175
|
+
send `false` and then `true`. The input description agents read comes from the
|
|
176
|
+
declaration.
|
|
177
|
+
|
|
178
|
+
## Input Log And Replays
|
|
179
|
+
|
|
180
|
+
Every host whose game declares a simulation keeps a version 2 input log,
|
|
181
|
+
from construction, in the browser as well as headless. It contains the real
|
|
182
|
+
seed, resolved initial settings, players joining and leaving, room state,
|
|
183
|
+
store actions applied between steps, controls when they change and accepted
|
|
184
|
+
settings updates. Each event carries the step it reached the game before.
|
|
185
|
+
Actions a step causes replay with the simulation and are not logged again.
|
|
186
|
+
`runtime.inputLog()` reads it locally; it throws while the simulation is
|
|
187
|
+
loading or when the game declares none.
|
|
188
|
+
|
|
189
|
+
The recorder retains the start of the match. It freezes before a step would
|
|
190
|
+
exceed 30 minutes of played game time or 16 MiB (the sum of each entry's JSON
|
|
191
|
+
length). A full log ends at the last whole step and excludes pending events.
|
|
192
|
+
`runtime.inputLogFull` reports this without changing the version 2 format.
|
|
193
|
+
Reload the game to start a new recording.
|
|
194
|
+
|
|
195
|
+
Agents use `readHostInputLog(request, observation.identity)` from
|
|
196
|
+
`@air-jam/sdk/runtime-control` through the existing host-agent channel. It
|
|
197
|
+
returns `{ status: "recorded", log, full }`, or `{ status: "not-replayable",
|
|
198
|
+
reason, message }`. Reasons are `no-simulation`, `loading`, `runtime-replaced`
|
|
199
|
+
and `host-unavailable`. A game without a simulation says:
|
|
200
|
+
"This game has no simulation, so its matches cannot be replayed; record a clip instead."
|
|
201
|
+
Reading needs no room grant. The first `input-log-read` request fixes a cut;
|
|
202
|
+
later pages read the same prefix while the match keeps playing.
|
|
203
|
+
|
|
204
|
+
Headless runs keep the log in their evidence. Running
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
airjam session run replay <run-id>
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
plays it on a fresh copy of the game, with no server or page, and reports
|
|
211
|
+
whether the replay ends where the run ended.
|
|
212
|
+
`replayAirJamInputLog(airjam.createHostRuntime, log)` from
|
|
213
|
+
`@air-jam/sdk/core` remains the run-to-completion form.
|
|
214
|
+
|
|
215
|
+
For playback, `await openAirJamReplay(airjam.createHostRuntime, log)` returns
|
|
216
|
+
`{ runtime, roomId, agent, stop, stepMs, ticks, tick, done, advance }`.
|
|
217
|
+
It stays paused between calls. `advance({ steps: 10 })` runs whole steps;
|
|
218
|
+
`advance({ ms: elapsed * speed })` carries fractions between calls, so a
|
|
219
|
+
caller can drive any speed per animation frame. Both return the current tick
|
|
220
|
+
and clamp at `ticks`. Reaching the end applies events after the final step
|
|
221
|
+
once, including on a recording with zero steps. Call `stop()` when finished.
|
|
222
|
+
|
|
223
|
+
There is no backward seek. Open a new replay and advance to the desired tick.
|
|
224
|
+
Each replay needs a fresh load of the same game source, in its own page or
|
|
225
|
+
headless process: game stores are module singletons and cannot share the load
|
|
226
|
+
with the live game or another replay. Exact replay also requires game code to
|
|
227
|
+
draw randomness from the step's seeded `random`.
|
|
228
|
+
|
|
229
|
+
## Common Issues
|
|
230
|
+
|
|
231
|
+
### A Control Does Nothing
|
|
232
|
+
|
|
233
|
+
1. Check the control is declared and passed to `defineAirJamGame({ input })`.
|
|
234
|
+
2. Read it in the simulation step; the host has no other way to read input.
|
|
235
|
+
3. Render phone controls inside `<airjam.Controller>`.
|
|
236
|
+
|
|
237
|
+
### A Tap Fires Twice Or Not At All
|
|
238
|
+
|
|
239
|
+
Use `button("pressed")` for one-shot actions, which then read `true` once per
|
|
240
|
+
press, and `button("held")` for actions that last while held.
|