@air-jam/cli 0.9.3 → 1.0.0-beta.2
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/README.md +128 -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-LH7UO57C.js +23 -0
- package/dist/chunk-LH7UO57C.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-A4V2N4LX.js → chunk-U6V3HEBB.js} +6 -5
- 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 +4 -0
- package/dist/development.js +1 -1
- 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.js +688 -404
- package/dist/index.js.map +1 -1
- package/dist/scaffold.d.ts +8 -1
- package/dist/scaffold.js +6 -1
- package/dist/scaffold.js.map +1 -1
- package/package.json +8 -4
- package/runtime/local-network.d.mts +4 -6
- package/runtime/public-assets.mjs +62 -0
- package/runtime/source-fence.mjs +182 -0
- package/runtime/vite-config.d.mts +2 -1
- package/runtime/vite-config.mjs +6 -2
- package/runtime/vite-dev.mjs +18 -0
- package/runtime/vite-https.mjs +2 -3
- package/template-assets/bootstrap/AGENTS.md +56 -14
- package/template-assets/bootstrap/CLAUDE.md +0 -8
- package/template-assets/bootstrap/skills/airjam-mcp/SKILL.md +44 -0
- package/template-assets/bootstrap/skills/controller-ui/SKILL.md +2 -0
- package/template-assets/bootstrap/skills/game-architecture/SKILL.md +47 -3
- package/template-assets/bootstrap/skills/index.md +1 -1
- package/template-assets/bootstrap/skills/prefab-authoring/SKILL.md +10 -0
- package/template-assets/bootstrap/skills/r3f-3d-scene/SKILL.md +10 -0
- package/template-assets/documentation/for-agents/controller-ui/page.mdx +41 -2
- package/template-assets/documentation/for-agents/page.mdx +24 -2
- package/template-assets/documentation/for-agents/project-structure/page.mdx +0 -2
- package/template-assets/documentation/for-agents/state-and-rendering/page.mdx +0 -2
- package/template-assets/documentation/for-agents/supported-agents/page.mdx +18 -0
- package/template-assets/documentation/getting-started/debugging/page.mdx +7 -8
- package/template-assets/documentation/getting-started/dev-logs/page.mdx +0 -2
- package/template-assets/documentation/getting-started/game-ideas/page.mdx +1 -3
- package/template-assets/documentation/getting-started/introduction/page.mdx +71 -54
- package/template-assets/documentation/getting-started/migrating-legacy-games/page.mdx +308 -62
- package/template-assets/documentation/getting-started/quick-start/page.mdx +56 -79
- package/template-assets/documentation/how-it-works/architecture/page.mdx +91 -50
- package/template-assets/documentation/how-it-works/host-system/page.mdx +112 -109
- package/template-assets/documentation/manifest.json +112 -42
- package/template-assets/documentation/sdk/assets-and-theme/page.mdx +185 -0
- package/template-assets/documentation/sdk/hooks/page.mdx +106 -171
- package/template-assets/documentation/sdk/input-system/page.mdx +201 -231
- package/template-assets/documentation/sdk/networked-state/page.mdx +134 -52
- package/template-assets/documentation/sdk/settings/page.mdx +155 -0
- package/template-assets/documentation/sdk/ui-components/page.mdx +45 -4
- package/template-assets/managed/.airjam/ai-pack.json +61 -43
- package/template-assets/managed/docs/airjam/agent-gold-path.md +55 -7
- package/template-assets/managed/docs/airjam/agent-mcp.md +44 -0
- package/template-assets/managed/docs/airjam/debug-and-testing.md +18 -0
- package/template-assets/managed/docs/airjam/docs-index.md +18 -12
- package/template-assets/managed/docs/airjam/generated/architecture.md +91 -46
- package/template-assets/managed/docs/airjam/generated/assets-and-theme.md +188 -0
- package/template-assets/managed/docs/airjam/generated/controller-ui.md +41 -0
- package/template-assets/managed/docs/airjam/generated/debugging-and-logs.md +7 -6
- package/template-assets/managed/docs/airjam/generated/for-agents.md +24 -0
- package/template-assets/managed/docs/airjam/generated/game-ideas.md +1 -1
- package/template-assets/managed/docs/airjam/generated/host-system.md +112 -102
- package/template-assets/managed/docs/airjam/generated/input-system.md +199 -218
- package/template-assets/managed/docs/airjam/generated/introduction.md +71 -51
- package/template-assets/managed/docs/airjam/generated/migrating-legacy-games.md +308 -60
- package/template-assets/managed/docs/airjam/generated/networked-state.md +134 -50
- package/template-assets/managed/docs/airjam/generated/quick-start.md +56 -79
- package/template-assets/managed/docs/airjam/generated/sdk-hooks.md +106 -166
- package/template-assets/managed/docs/airjam/generated/settings.md +158 -0
- package/template-assets/managed/docs/airjam/generated/supported-agents.md +21 -0
- package/template-assets/managed/docs/airjam/generated/ui-components.md +45 -2
- package/template-assets/managed/docs/airjam/prefab-authoring.md +100 -0
- package/template-assets/managed/docs/airjam/preview-controllers.md +4 -1
- package/template-assets/managed/docs/airjam/r3f-3d-scene.md +66 -0
- package/template-assets/managed/docs/airjam/state-lanes-cookbook.md +15 -20
- package/dist/chunk-A4V2N4LX.js.map +0 -1
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# Game Assets and Theme
|
|
2
|
+
|
|
3
|
+
Declare a game's sounds, images, other loaded files, colours and fonts in its
|
|
4
|
+
React-free game declaration. Tools read them through `airjam.inspect_game` in
|
|
5
|
+
`declaration.assets` and `declaration.theme`.
|
|
6
|
+
|
|
7
|
+
## Declare loaded files
|
|
8
|
+
|
|
9
|
+
Keep the assets in a leaf module so rendering and audio code can import them
|
|
10
|
+
without importing the config back again.
|
|
11
|
+
|
|
12
|
+
```ts filename="src/game/assets.ts"
|
|
13
|
+
import { defineAirJamAssets } from "@air-jam/sdk/core";
|
|
14
|
+
|
|
15
|
+
export const gameAssets = defineAirJamAssets({
|
|
16
|
+
sounds: {
|
|
17
|
+
hit: { src: ["/sounds/hit.wav"], volume: 0.55 },
|
|
18
|
+
music: {
|
|
19
|
+
src: ["/music/theme.mp3"],
|
|
20
|
+
category: "music",
|
|
21
|
+
html5: true,
|
|
22
|
+
surface: "host",
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
images: { player: { src: "/images/player.png" } },
|
|
26
|
+
files: { animation: { src: "/animations/fire.json" } },
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Pass it as `defineAirJamGame({ assets: gameAssets, ... })`. Every source is a
|
|
31
|
+
served path under the game's `public/` directory: it starts with `/` and has no
|
|
32
|
+
parent segments, query or hash. A sound takes a nonempty `src` array and optional
|
|
33
|
+
`volume` (0 to 1), `loop`, `html5`, `sprite` and `category` (`music` or `sfx`).
|
|
34
|
+
A sprite maps its name to `[offsetMilliseconds, durationMilliseconds]`.
|
|
35
|
+
`surface` selects `host` or `controller`; omitting it makes the sound available
|
|
36
|
+
on both. Images and files each take `{ src }`.
|
|
37
|
+
|
|
38
|
+
The React binding mounts audio for each surface automatically. Remove your
|
|
39
|
+
own `<AudioRuntime>` wrappers. Existing `useAudio` calls and `MusicPlaylist`
|
|
40
|
+
components use the binding's runtime. Host-only music never loads on a phone.
|
|
41
|
+
Read image and file paths from the object, for example
|
|
42
|
+
`gameAssets.images.player.src`, rather than repeating URL strings.
|
|
43
|
+
|
|
44
|
+
Inspection checks every declared source exists and fails with
|
|
45
|
+
`AJ_ASSET_MISSING`, naming the game, entry and missing path. This checks file
|
|
46
|
+
presence, not whether code loads an undeclared URL. Arcade cover, thumbnail and
|
|
47
|
+
preview media remain outside this declaration.
|
|
48
|
+
|
|
49
|
+
## Declare colours and fonts
|
|
50
|
+
|
|
51
|
+
```ts filename="src/game/theme.ts"
|
|
52
|
+
import { defineAirJamTheme } from "@air-jam/sdk/core";
|
|
53
|
+
|
|
54
|
+
export const gameTheme = defineAirJamTheme({
|
|
55
|
+
colors: { background: "#050816", team1: "#f97316", team2: "#38bdf8" },
|
|
56
|
+
fonts: { display: '"Avenir Next", "Trebuchet MS", sans-serif' },
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Pass it as `defineAirJamGame({ theme: gameTheme, ... })`. Keys are lower camel
|
|
61
|
+
case. Colours use hex, `rgb()`, `rgba()`, `hsl()` or `oklch()`; fonts are CSS
|
|
62
|
+
font-family lists. Font files still load through your existing CSS imports or
|
|
63
|
+
declared file paths.
|
|
64
|
+
|
|
65
|
+
The binding renders a `<style>` with each surface on its first render. It
|
|
66
|
+
exposes `--airjam-color-background`, `--airjam-color-team1` and
|
|
67
|
+
`--airjam-font-display`; camel case keys become kebab case. No generation step
|
|
68
|
+
or effect is needed. Map the variables to existing Tailwind names:
|
|
69
|
+
|
|
70
|
+
```css filename="src/index.css"
|
|
71
|
+
@import "tailwindcss";
|
|
72
|
+
|
|
73
|
+
@theme inline {
|
|
74
|
+
--color-game-bg: var(--airjam-color-background);
|
|
75
|
+
--color-game-orange: var(--airjam-color-team1);
|
|
76
|
+
--font-game: var(--airjam-font-display);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Existing classes such as `bg-game-bg` keep their names. Canvas and 3D code read
|
|
81
|
+
`gameTheme.colors.team1` and `gameTheme.fonts.display` directly. Remove the
|
|
82
|
+
literals they replace. Shadows, gradients and layout remain handwritten CSS.
|
|
83
|
+
|
|
84
|
+
Both fields are optional; an omitted field is described as empty maps.
|
|
85
|
+
The declaration graph must remain free of React, the DOM and CSS imports.
|
|
86
|
+
|
|
87
|
+
## Isolated assets and states
|
|
88
|
+
|
|
89
|
+
Prefabs are the catalog identity. Declare `space` and, for animated assets, named
|
|
90
|
+
`states`. `space` is optional: it defaults to `"3d"` when `preview.camera` is set,
|
|
91
|
+
otherwise `"2d"`. A rendered `PrefabDefinition` requires `render`; a renderer-free
|
|
92
|
+
`PrefabDeclaration` omits it. Omitting states creates one static state (`static`). State time is in
|
|
93
|
+
seconds; the SDK owns playback and seeking, while the game draws that exact time.
|
|
94
|
+
A state without `seconds` is a static pose. `loop: true` wraps a timed state;
|
|
95
|
+
otherwise it pauses at the end.
|
|
96
|
+
|
|
97
|
+
```ts filename="src/airjam.config.ts"
|
|
98
|
+
import { defineAirJamGame } from "@air-jam/sdk/core";
|
|
99
|
+
import { createPrefabCatalog, definePrefab } from "@air-jam/sdk/prefabs";
|
|
100
|
+
import { z } from "zod";
|
|
101
|
+
|
|
102
|
+
export const dotProps = z.object({ radius: z.number().positive() });
|
|
103
|
+
export const dot = definePrefab({
|
|
104
|
+
id: "dot",
|
|
105
|
+
label: "Dot",
|
|
106
|
+
category: "effect",
|
|
107
|
+
description: "Pulsing circle",
|
|
108
|
+
tags: ["effect"],
|
|
109
|
+
space: "2d",
|
|
110
|
+
configSchema: dotProps,
|
|
111
|
+
defaultProps: { radius: 40 },
|
|
112
|
+
states: [{ id: "pulse", label: "Pulse", seconds: 1, loop: true }],
|
|
113
|
+
});
|
|
114
|
+
// Add to your existing definition, retaining its metadata and other declarations:
|
|
115
|
+
export const game = defineAirJamGame({
|
|
116
|
+
metadata,
|
|
117
|
+
prefabs: createPrefabCatalog([dot]),
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Keep this declaration module free of React and renderer imports. In the host
|
|
122
|
+
application, the stage selects the renderer for the declared prefab:
|
|
123
|
+
|
|
124
|
+
```tsx filename="src/prefab-stage.tsx"
|
|
125
|
+
import {
|
|
126
|
+
createAirJamApp,
|
|
127
|
+
drawAgentCanvas,
|
|
128
|
+
useHostAgentCapture,
|
|
129
|
+
usePrefabInspection,
|
|
130
|
+
} from "@air-jam/sdk";
|
|
131
|
+
import { resolvePrefab2DTransform } from "@air-jam/sdk/prefabs";
|
|
132
|
+
import { useLayoutEffect, useRef } from "react";
|
|
133
|
+
import { dot, dotProps, game } from "./airjam.config";
|
|
134
|
+
|
|
135
|
+
function Stage() {
|
|
136
|
+
const canvas = useRef<HTMLCanvasElement>(null);
|
|
137
|
+
const { prefab, props, time, view } = usePrefabInspection();
|
|
138
|
+
if (prefab.id !== dot.id) throw new Error(`Unknown prefab: ${prefab.id}`);
|
|
139
|
+
const { radius } = dotProps.parse(props);
|
|
140
|
+
useHostAgentCapture((image, options) =>
|
|
141
|
+
drawAgentCanvas(canvas.current, image, options),
|
|
142
|
+
);
|
|
143
|
+
useLayoutEffect(() => {
|
|
144
|
+
const context = canvas.current?.getContext("2d");
|
|
145
|
+
if (!context) return;
|
|
146
|
+
const { scale, translate } = resolvePrefab2DTransform(view, 512);
|
|
147
|
+
context.resetTransform();
|
|
148
|
+
context.clearRect(0, 0, 512, 512);
|
|
149
|
+
context.translate(256 + translate[0], 256 + translate[1]);
|
|
150
|
+
context.scale(scale, scale);
|
|
151
|
+
context.beginPath();
|
|
152
|
+
context.arc(
|
|
153
|
+
0,
|
|
154
|
+
0,
|
|
155
|
+
radius * (1 + 0.2 * Math.sin(time * Math.PI * 2)),
|
|
156
|
+
0,
|
|
157
|
+
Math.PI * 2,
|
|
158
|
+
);
|
|
159
|
+
context.fillStyle = "#ff6a35";
|
|
160
|
+
context.fill();
|
|
161
|
+
}, [radius, time, view]);
|
|
162
|
+
return <canvas ref={canvas} width={512} height={512} />;
|
|
163
|
+
}
|
|
164
|
+
const app = createAirJamApp(game);
|
|
165
|
+
// In the existing host route (LiveGame is your game's live component):
|
|
166
|
+
<app.Host prefabStage={Stage}>
|
|
167
|
+
<LiveGame />
|
|
168
|
+
</app.Host>;
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
For R3F, use `resolvePrefabOrbitCamera(prefab.preview?.camera, view)` and apply its
|
|
172
|
+
`position`, `target` and `fov` to the game's camera. Capture through
|
|
173
|
+
`useHostAgentCapture`: render the R3F scene immediately before copying `gl.domElement`
|
|
174
|
+
with `drawAgentCanvas`, because WebGL drawing buffers need not survive a frame.
|
|
175
|
+
The helper has no Three dependency. Zoom 1 keeps default framing; yaw/pitch use
|
|
176
|
+
degrees relative to the preview camera. Pan uses fractions of view height, right/up
|
|
177
|
+
positive. The 2D helper returns pixel translation and scale around default framing;
|
|
178
|
+
yaw and pitch have no effect in 2D.
|
|
179
|
+
|
|
180
|
+
Open `?airjamInspect=1&prefab=dot&state=pulse&time=0.3` for a paused isolated frame.
|
|
181
|
+
Stages read parsed default props, state, time, playing and view from
|
|
182
|
+
`usePrefabInspection()`. They must draw only that prefab, without reading live room
|
|
183
|
+
stores, starting physics or advancing their own animation clock. The SDK reports
|
|
184
|
+
missing catalogs, missing stages and rendering errors. `@air-jam/sdk/prefab-inspection`
|
|
185
|
+
exports the validated parent client for lists, selection and batch canvas captures.
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
1
|
# SDK Hooks
|
|
4
2
|
|
|
5
3
|
The Air Jam SDK provides React hooks for building multiplayer games. This page documents all available hooks and their usage.
|
|
@@ -8,21 +6,17 @@ The Air Jam SDK provides React hooks for building multiplayer games. This page d
|
|
|
8
6
|
|
|
9
7
|
### `createAirJamApp`
|
|
10
8
|
|
|
11
|
-
|
|
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`.
|
|
12
13
|
|
|
13
14
|
```tsx
|
|
14
15
|
import { createAirJamApp, env } from "@air-jam/sdk";
|
|
15
|
-
import {
|
|
16
|
+
import { airjam as game } from "./airjam.config";
|
|
16
17
|
|
|
17
|
-
const
|
|
18
|
-
vector: z.object({ x: z.number(), y: z.number() }),
|
|
19
|
-
timestamp: z.number(),
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
const airjam = createAirJamApp({
|
|
18
|
+
export const airjam = createAirJamApp(game, {
|
|
23
19
|
runtime: env.vite(import.meta.env),
|
|
24
|
-
controllerPath: "/controller",
|
|
25
|
-
input: { schema: inputSchema },
|
|
26
20
|
});
|
|
27
21
|
|
|
28
22
|
<Routes>
|
|
@@ -52,6 +46,11 @@ The provider automatically reads from these environment variables if props aren'
|
|
|
52
46
|
- `VITE_AIR_JAM_SERVER_URL` / `NEXT_PUBLIC_AIR_JAM_SERVER_URL` - WebSocket server URL
|
|
53
47
|
- `VITE_AIR_JAM_APP_ID` / `NEXT_PUBLIC_AIR_JAM_APP_ID` - Public app ID
|
|
54
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
|
+
|
|
55
54
|
---
|
|
56
55
|
|
|
57
56
|
## Host Hooks
|
|
@@ -64,11 +63,11 @@ Call `useAirJamHost(selector)` when a component only needs a narrow slice of
|
|
|
64
63
|
host session state and you want to avoid rerendering on unrelated runtime
|
|
65
64
|
fields.
|
|
66
65
|
Selectors receive state fields only; call `useAirJamHost()` when a component
|
|
67
|
-
also needs runtime controls such as `joinUrl
|
|
66
|
+
also needs runtime controls such as `joinUrl` or `sendSignal`.
|
|
68
67
|
|
|
69
68
|
```tsx
|
|
70
69
|
import { useAirJamHost } from "@air-jam/sdk";
|
|
71
|
-
import { airjam } from "../
|
|
70
|
+
import { airjam } from "../app";
|
|
72
71
|
|
|
73
72
|
const HostShell = () => (
|
|
74
73
|
<airjam.Host
|
|
@@ -92,21 +91,18 @@ const HostView = () => {
|
|
|
92
91
|
roomId, // "ABCD" - The room code
|
|
93
92
|
joinUrl, // Full URL for controllers to join
|
|
94
93
|
joinUrlStatus, // "loading" | "ready" | "unavailable"
|
|
95
|
-
connectionStatus, // "connected" | "connecting" | "disconnected" | "idle"
|
|
96
|
-
players, // Array of
|
|
97
|
-
|
|
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
|
|
98
97
|
runtimeState, // "playing" | "paused"
|
|
99
98
|
lastError, // Error message if any
|
|
100
99
|
mode, // "standalone" | "platform"
|
|
101
100
|
|
|
102
101
|
// Functions
|
|
103
|
-
getInput, // (controllerId: string) => Input | undefined
|
|
104
102
|
sendSignal, // Send haptics/toasts to controllers
|
|
105
|
-
sendState, // Publish lightweight controller presentation state
|
|
106
103
|
pauseRuntime, // Set runtimeState to "paused"
|
|
107
104
|
resumeRuntime, // Set runtimeState to "playing"
|
|
108
105
|
setRuntimeState, // Explicitly set "playing" | "paused"
|
|
109
|
-
reconnect, // Force reconnection
|
|
110
106
|
} = host;
|
|
111
107
|
|
|
112
108
|
return (
|
|
@@ -125,33 +121,18 @@ const HostView = () => {
|
|
|
125
121
|
};
|
|
126
122
|
```
|
|
127
123
|
|
|
128
|
-
|
|
129
|
-
|
|
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.
|
|
130
127
|
|
|
131
|
-
|
|
128
|
+
Players' controls reach the game only in its simulation steps, through
|
|
129
|
+
`input(playerId)`; see [Input System](/docs/sdk/input-system).
|
|
132
130
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
host.players.forEach((player) => {
|
|
137
|
-
const input = host.getInput(player.id);
|
|
138
|
-
if (!input) return;
|
|
139
|
-
|
|
140
|
-
// Move player based on joystick
|
|
141
|
-
movePlayer(player.id, input.vector);
|
|
142
|
-
|
|
143
|
-
// Handle button press (tap-safe pulse by default)
|
|
144
|
-
if (input.action) {
|
|
145
|
-
playerShoot(player.id);
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
```
|
|
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.
|
|
150
134
|
|
|
151
|
-
`
|
|
152
|
-
orientation, pause/play status, or short UI messages. It is not the primary
|
|
153
|
-
gameplay sync channel. Authoritative multiplayer state should live in the
|
|
154
|
-
networked stores and flow automatically through those boundaries.
|
|
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.
|
|
155
136
|
|
|
156
137
|
**Sending Haptic Feedback:**
|
|
157
138
|
|
|
@@ -192,37 +173,6 @@ host.sendSignal("TOAST", {
|
|
|
192
173
|
|
|
193
174
|
---
|
|
194
175
|
|
|
195
|
-
### `useGetInput`
|
|
196
|
-
|
|
197
|
-
Lightweight hook for accessing input without triggering re-renders. Use in performance-critical components.
|
|
198
|
-
|
|
199
|
-
```tsx
|
|
200
|
-
import { useGetInput } from "@air-jam/sdk";
|
|
201
|
-
|
|
202
|
-
const Ship = ({ playerId }: { playerId: string }) => {
|
|
203
|
-
const getInput = useGetInput();
|
|
204
|
-
|
|
205
|
-
// This component won't re-render when connection state changes
|
|
206
|
-
useFrame(() => {
|
|
207
|
-
const input = getInput(playerId);
|
|
208
|
-
if (!input) return;
|
|
209
|
-
|
|
210
|
-
// Update ship position
|
|
211
|
-
shipRef.current.position.x += input.vector.x * SPEED;
|
|
212
|
-
shipRef.current.position.y += input.vector.y * SPEED;
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
return <mesh ref={shipRef}>...</mesh>;
|
|
216
|
-
};
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
**When to use `useGetInput` vs `useAirJamHost().getInput`:**
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
<HooksComparisonTable />
|
|
223
|
-
|
|
224
|
-
---
|
|
225
|
-
|
|
226
176
|
### `useSendSignal`
|
|
227
177
|
|
|
228
178
|
Lightweight hook for sending signals without triggering re-renders. Use in collision handlers.
|
|
@@ -259,81 +209,47 @@ updates.
|
|
|
259
209
|
Selectors receive state fields only; call `useAirJamController()` when a
|
|
260
210
|
component also needs controls such as `sendSystemCommand`.
|
|
261
211
|
|
|
262
|
-
|
|
263
|
-
|
|
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.
|
|
264
214
|
|
|
265
215
|
```tsx
|
|
266
|
-
import {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
} from "@air-jam/sdk";
|
|
271
|
-
import { useRef } from "react";
|
|
272
|
-
import { airjam } from "../airjam.config";
|
|
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";
|
|
273
220
|
|
|
274
221
|
const ControllerShell = () => (
|
|
275
|
-
<airjam.Controller
|
|
276
|
-
roomId="ABCD"
|
|
277
|
-
nickname="Player1"
|
|
278
|
-
onState={(state) => {
|
|
279
|
-
if (state.message) {
|
|
280
|
-
showNotification(state.message);
|
|
281
|
-
}
|
|
282
|
-
}}
|
|
283
|
-
>
|
|
222
|
+
<airjam.Controller roomId="ABCD" nickname="Player1">
|
|
284
223
|
<ControllerView />
|
|
285
224
|
</airjam.Controller>
|
|
286
225
|
);
|
|
287
226
|
|
|
288
227
|
const ControllerView = () => {
|
|
289
228
|
const controller = useAirJamController();
|
|
290
|
-
const writeInput = useInputWriter();
|
|
291
|
-
const vectorRef = useRef({ x: 0, y: 0 });
|
|
292
|
-
const actionRef = useRef(false);
|
|
293
229
|
|
|
294
230
|
const {
|
|
295
231
|
roomId, // Room code (from URL or props)
|
|
296
|
-
controllerId, // This
|
|
232
|
+
controllerId, // This phone's seat ("p1", "p2", …)
|
|
297
233
|
connectionStatus, // Connection state
|
|
298
234
|
runtimeState, // "playing" | "paused"
|
|
299
|
-
controllerOrientation, //
|
|
300
|
-
stateMessage, // Optional message from host
|
|
235
|
+
controllerOrientation, // Declared session orientation; always landscape for joystick games
|
|
301
236
|
|
|
302
237
|
// Functions
|
|
303
|
-
sendSystemCommand, // "
|
|
238
|
+
sendSystemCommand, // "pause" | "resume"
|
|
304
239
|
setNickname, // Update nickname
|
|
305
240
|
setAvatarId, // Update avatar selection draft
|
|
306
241
|
updatePlayerProfile, // Patch live label/avatar when connected
|
|
307
242
|
selfPlayer, // This controller's player profile when known
|
|
308
|
-
|
|
243
|
+
leave, // Give up the seat
|
|
244
|
+
input, // This player's declared controls, which phone controls fill
|
|
309
245
|
} = controller;
|
|
310
246
|
|
|
311
|
-
// Canonical cadence for continuous controls:
|
|
312
|
-
// publish at a fixed tick with useControllerTick + useInputWriter.
|
|
313
|
-
useControllerTick(
|
|
314
|
-
() => {
|
|
315
|
-
writeInput({
|
|
316
|
-
vector: vectorRef.current,
|
|
317
|
-
action: actionRef.current,
|
|
318
|
-
timestamp: Date.now(),
|
|
319
|
-
});
|
|
320
|
-
},
|
|
321
|
-
{
|
|
322
|
-
enabled: connectionStatus === "connected" && runtimeState === "playing",
|
|
323
|
-
intervalMs: 16,
|
|
324
|
-
},
|
|
325
|
-
);
|
|
326
|
-
|
|
327
247
|
if (connectionStatus === "connecting") {
|
|
328
248
|
return <div>Connecting to room {roomId}...</div>;
|
|
329
249
|
}
|
|
330
250
|
|
|
331
|
-
if (connectionStatus === "
|
|
332
|
-
return
|
|
333
|
-
<div>
|
|
334
|
-
Disconnected. <button onClick={reconnect}>Retry</button>
|
|
335
|
-
</div>
|
|
336
|
-
);
|
|
251
|
+
if (connectionStatus === "reconnecting") {
|
|
252
|
+
return <div>Reconnecting…</div>;
|
|
337
253
|
}
|
|
338
254
|
|
|
339
255
|
return (
|
|
@@ -342,51 +258,66 @@ const ControllerView = () => {
|
|
|
342
258
|
<div className="mb-2 text-xs uppercase">
|
|
343
259
|
Layout {controllerOrientation}
|
|
344
260
|
</div>
|
|
345
|
-
<
|
|
346
|
-
|
|
347
|
-
vectorRef.current = { x, y };
|
|
348
|
-
}}
|
|
349
|
-
/>
|
|
350
|
-
<FireButton
|
|
351
|
-
onPress={() => (actionRef.current = true)}
|
|
352
|
-
onRelease={() => (actionRef.current = false)}
|
|
353
|
-
/>
|
|
261
|
+
<Stick control={shipInput.move} className="size-48 rounded-full" />
|
|
262
|
+
<ControlButton control={shipInput.fire}>Fire</ControlButton>
|
|
354
263
|
</div>
|
|
355
264
|
);
|
|
356
265
|
};
|
|
357
266
|
```
|
|
358
267
|
|
|
359
|
-
|
|
268
|
+
Phone controls send the declared controls themselves; a controller runs no
|
|
269
|
+
send loop.
|
|
360
270
|
|
|
361
|
-
|
|
271
|
+
### Game simulation
|
|
362
272
|
|
|
363
|
-
|
|
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.
|
|
364
278
|
|
|
365
|
-
|
|
366
|
-
|
|
279
|
+
```ts filename="src/game/engine/simulation.ts"
|
|
280
|
+
import { defineAirJamSimulation } from "@air-jam/sdk/core";
|
|
367
281
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
frame while simulation still advances at the fixed cadence. The frame info
|
|
374
|
-
includes `fixedStepAlpha`, which games can use to interpolate between previous
|
|
375
|
-
and current simulation state.
|
|
376
|
-
|
|
377
|
-
```tsx
|
|
378
|
-
useHostTick({
|
|
379
|
-
mode: "fixed",
|
|
380
|
-
intervalMs: 16,
|
|
381
|
-
onTick: ({ deltaSeconds }) => {
|
|
382
|
-
stepSimulation(deltaSeconds);
|
|
383
|
-
},
|
|
384
|
-
onFrame: ({ fixedStepAlpha }) => {
|
|
385
|
-
renderInterpolatedFrame(fixedStepAlpha);
|
|
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);
|
|
386
287
|
},
|
|
288
|
+
// What agents and tests see of the game, as plain JSON.
|
|
289
|
+
observe: (sim) => ({ fighters: sim.fighters }),
|
|
387
290
|
});
|
|
388
291
|
```
|
|
389
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
|
+
|
|
390
321
|
**Auto Room Join from URL:**
|
|
391
322
|
|
|
392
323
|
Controllers automatically join rooms from URL query parameters:
|
|
@@ -403,16 +334,15 @@ This is how QR code scanning works—the host generates a URL with the room code
|
|
|
403
334
|
|
|
404
335
|
Use these only when integrating platform-specific runtime behavior.
|
|
405
336
|
|
|
406
|
-
###
|
|
337
|
+
### Room session access (escape hatch)
|
|
407
338
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
```
|
|
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.
|
|
412
342
|
|
|
413
343
|
### Internal context hooks
|
|
414
344
|
|
|
415
|
-
`useAirJamContext`, `useAirJamConfig
|
|
345
|
+
`useAirJamContext`, `useAirJamConfig` and `useAirJamState` are intentionally internal and are not part of the root public SDK surface.
|
|
416
346
|
|
|
417
347
|
---
|
|
418
348
|
|
|
@@ -439,27 +369,25 @@ const unsubscribe = onAirJamDiagnostic((diagnostic) => {
|
|
|
439
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. |
|
|
440
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. |
|
|
441
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. |
|
|
442
|
-
| `AJ_STORE_ACTION_SOCKET_DISCONNECTED` | Controller state action dispatch attempted while socket disconnected. | Reconnect before dispatching action. |
|
|
443
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. |
|
|
444
373
|
| `AJ_STORE_ACTION_PAYLOAD_NOT_SERIALIZABLE` | State action payload contained non-serializable values. | Use JSON-safe payload objects only (no functions/symbols/cycles). |
|
|
445
|
-
| `
|
|
446
|
-
| `
|
|
447
|
-
| `AJ_INPUT_WRITER_SESSION_NOT_READY` | Input writer called before room/controller identity was ready. | Start publish loop after controller connection/session is ready. |
|
|
448
|
-
| `AJ_INPUT_WRITER_SOCKET_DISCONNECTED` | Input writer called while controller socket was disconnected. | Reconnect before sending input. |
|
|
449
|
-
| `AJ_INPUT_WRITER_SCHEMA_INVALID` | Input payload failed runtime schema validation. | Match payload shape to configured input schema. |
|
|
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>`. |
|
|
450
376
|
|
|
451
377
|
---
|
|
452
378
|
|
|
453
379
|
## Types
|
|
454
380
|
|
|
455
|
-
### `
|
|
381
|
+
### `AirJamPlayer`
|
|
456
382
|
|
|
457
383
|
```typescript
|
|
458
|
-
interface
|
|
459
|
-
id: string; //
|
|
384
|
+
interface AirJamPlayer {
|
|
385
|
+
id: string; // The seat the room assigned ("p1", "p2", …)
|
|
460
386
|
label: string; // Display name (e.g., "Player 1")
|
|
461
|
-
color
|
|
387
|
+
color: string; // Assigned color (e.g., "#FF5733")
|
|
462
388
|
avatarId?: string; // Preset avatar id chosen by the player
|
|
389
|
+
kind: "phone" | "preview" | "agent";
|
|
390
|
+
connected: boolean; // false while the phone is away
|
|
463
391
|
}
|
|
464
392
|
```
|
|
465
393
|
|
|
@@ -498,3 +426,10 @@ interface ToastSignalPayload {
|
|
|
498
426
|
duration?: number; // Optional duration in ms
|
|
499
427
|
}
|
|
500
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.
|