@air-jam/cli 0.9.3 → 1.0.0-beta.10
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 +19 -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 +108 -172
- 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 +166 -0
- package/template-assets/documentation/sdk/ui-components/page.mdx +38 -27
- package/template-assets/managed/.airjam/ai-pack.json +65 -47
- 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/composition-shell-contract.md +4 -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 +108 -167
- package/template-assets/managed/docs/airjam/generated/settings.md +169 -0
- package/template-assets/managed/docs/airjam/generated/state-and-rendering.md +19 -0
- package/template-assets/managed/docs/airjam/generated/supported-agents.md +21 -0
- package/template-assets/managed/docs/airjam/generated/ui-components.md +38 -25
- package/template-assets/managed/docs/airjam/prefab-authoring.md +100 -0
- package/template-assets/managed/docs/airjam/preview-controllers.md +5 -2
- 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
|
|
@@ -90,23 +89,21 @@ const HostView = () => {
|
|
|
90
89
|
// Return values
|
|
91
90
|
const {
|
|
92
91
|
roomId, // "ABCD" - The room code
|
|
93
|
-
joinUrl, //
|
|
92
|
+
joinUrl, // Phone join URL; empty when topology has no phone reach
|
|
93
|
+
localControllerUrl, // On-screen controllers on this computer only
|
|
94
94
|
joinUrlStatus, // "loading" | "ready" | "unavailable"
|
|
95
|
-
connectionStatus, // "connected" | "connecting" | "disconnected" | "idle"
|
|
96
|
-
players, // Array of
|
|
97
|
-
|
|
95
|
+
connectionStatus, // "connected" | "connecting" | "reconnecting" | "disconnected" | "idle"
|
|
96
|
+
players, // Array of AirJamPlayer, including phones that are away
|
|
97
|
+
queuePosition, // Place in a busy hosted service's queue, or null
|
|
98
98
|
runtimeState, // "playing" | "paused"
|
|
99
99
|
lastError, // Error message if any
|
|
100
100
|
mode, // "standalone" | "platform"
|
|
101
101
|
|
|
102
102
|
// Functions
|
|
103
|
-
getInput, // (controllerId: string) => Input | undefined
|
|
104
103
|
sendSignal, // Send haptics/toasts to controllers
|
|
105
|
-
sendState, // Publish lightweight controller presentation state
|
|
106
104
|
pauseRuntime, // Set runtimeState to "paused"
|
|
107
105
|
resumeRuntime, // Set runtimeState to "playing"
|
|
108
106
|
setRuntimeState, // Explicitly set "playing" | "paused"
|
|
109
|
-
reconnect, // Force reconnection
|
|
110
107
|
} = host;
|
|
111
108
|
|
|
112
109
|
return (
|
|
@@ -125,33 +122,18 @@ const HostView = () => {
|
|
|
125
122
|
};
|
|
126
123
|
```
|
|
127
124
|
|
|
128
|
-
|
|
129
|
-
|
|
125
|
+
Each player is a seat the room assigned (`p1`, `p2`, …). A phone that drops
|
|
126
|
+
keeps its seat for two minutes and shows `connected: false` meanwhile;
|
|
127
|
+
reconnecting is automatic.
|
|
130
128
|
|
|
131
|
-
|
|
129
|
+
Players' controls reach the game only in its simulation steps, through
|
|
130
|
+
`input(playerId)`; see [Input System](/docs/sdk/input-system).
|
|
132
131
|
|
|
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
|
-
```
|
|
132
|
+
Pause and orientation are the only presentation the host sends phones
|
|
133
|
+
directly. Authoritative multiplayer state lives in the networked stores; a
|
|
134
|
+
short message for the players is a toast signal or a store field.
|
|
150
135
|
|
|
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.
|
|
136
|
+
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
137
|
|
|
156
138
|
**Sending Haptic Feedback:**
|
|
157
139
|
|
|
@@ -192,37 +174,6 @@ host.sendSignal("TOAST", {
|
|
|
192
174
|
|
|
193
175
|
---
|
|
194
176
|
|
|
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
177
|
### `useSendSignal`
|
|
227
178
|
|
|
228
179
|
Lightweight hook for sending signals without triggering re-renders. Use in collision handlers.
|
|
@@ -259,81 +210,47 @@ updates.
|
|
|
259
210
|
Selectors receive state fields only; call `useAirJamController()` when a
|
|
260
211
|
component also needs controls such as `sendSystemCommand`.
|
|
261
212
|
|
|
262
|
-
|
|
263
|
-
|
|
213
|
+
A controller page keeps its seat's key for each room it joins, so a reload or a
|
|
214
|
+
dropped network returns the phone to the same seat.
|
|
264
215
|
|
|
265
216
|
```tsx
|
|
266
|
-
import {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
} from "@air-jam/sdk";
|
|
271
|
-
import { useRef } from "react";
|
|
272
|
-
import { airjam } from "../airjam.config";
|
|
217
|
+
import { useAirJamController } from "@air-jam/sdk";
|
|
218
|
+
import { ControlButton, Stick } from "@air-jam/sdk/ui";
|
|
219
|
+
import { airjam } from "../app";
|
|
220
|
+
import { shipInput } from "../game/contracts/input";
|
|
273
221
|
|
|
274
222
|
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
|
-
>
|
|
223
|
+
<airjam.Controller roomId="ABCD" nickname="Player1">
|
|
284
224
|
<ControllerView />
|
|
285
225
|
</airjam.Controller>
|
|
286
226
|
);
|
|
287
227
|
|
|
288
228
|
const ControllerView = () => {
|
|
289
229
|
const controller = useAirJamController();
|
|
290
|
-
const writeInput = useInputWriter();
|
|
291
|
-
const vectorRef = useRef({ x: 0, y: 0 });
|
|
292
|
-
const actionRef = useRef(false);
|
|
293
230
|
|
|
294
231
|
const {
|
|
295
232
|
roomId, // Room code (from URL or props)
|
|
296
|
-
controllerId, // This
|
|
233
|
+
controllerId, // This phone's seat ("p1", "p2", …)
|
|
297
234
|
connectionStatus, // Connection state
|
|
298
235
|
runtimeState, // "playing" | "paused"
|
|
299
|
-
controllerOrientation, //
|
|
300
|
-
stateMessage, // Optional message from host
|
|
236
|
+
controllerOrientation, // Declared session orientation; always landscape for joystick games
|
|
301
237
|
|
|
302
238
|
// Functions
|
|
303
|
-
sendSystemCommand, // "
|
|
239
|
+
sendSystemCommand, // "pause" | "resume"
|
|
304
240
|
setNickname, // Update nickname
|
|
305
241
|
setAvatarId, // Update avatar selection draft
|
|
306
242
|
updatePlayerProfile, // Patch live label/avatar when connected
|
|
307
243
|
selfPlayer, // This controller's player profile when known
|
|
308
|
-
|
|
244
|
+
leave, // Give up the seat
|
|
245
|
+
input, // This player's declared controls, which phone controls fill
|
|
309
246
|
} = controller;
|
|
310
247
|
|
|
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
248
|
if (connectionStatus === "connecting") {
|
|
328
249
|
return <div>Connecting to room {roomId}...</div>;
|
|
329
250
|
}
|
|
330
251
|
|
|
331
|
-
if (connectionStatus === "
|
|
332
|
-
return
|
|
333
|
-
<div>
|
|
334
|
-
Disconnected. <button onClick={reconnect}>Retry</button>
|
|
335
|
-
</div>
|
|
336
|
-
);
|
|
252
|
+
if (connectionStatus === "reconnecting") {
|
|
253
|
+
return <div>Reconnecting…</div>;
|
|
337
254
|
}
|
|
338
255
|
|
|
339
256
|
return (
|
|
@@ -342,51 +259,66 @@ const ControllerView = () => {
|
|
|
342
259
|
<div className="mb-2 text-xs uppercase">
|
|
343
260
|
Layout {controllerOrientation}
|
|
344
261
|
</div>
|
|
345
|
-
<
|
|
346
|
-
|
|
347
|
-
vectorRef.current = { x, y };
|
|
348
|
-
}}
|
|
349
|
-
/>
|
|
350
|
-
<FireButton
|
|
351
|
-
onPress={() => (actionRef.current = true)}
|
|
352
|
-
onRelease={() => (actionRef.current = false)}
|
|
353
|
-
/>
|
|
262
|
+
<Stick control={shipInput.move} className="size-48 rounded-full" />
|
|
263
|
+
<ControlButton control={shipInput.fire}>Fire</ControlButton>
|
|
354
264
|
</div>
|
|
355
265
|
);
|
|
356
266
|
};
|
|
357
267
|
```
|
|
358
268
|
|
|
359
|
-
|
|
269
|
+
Phone controls send the declared controls themselves; a controller runs no
|
|
270
|
+
send loop.
|
|
360
271
|
|
|
361
|
-
|
|
272
|
+
### Game simulation
|
|
362
273
|
|
|
363
|
-
|
|
274
|
+
A game's authoritative state that changes every step, such as positions,
|
|
275
|
+
timers and physics, lives in a declared simulation. The SDK steps it on the
|
|
276
|
+
host at a fixed rate with seeded randomness, so the same seed and inputs replay
|
|
277
|
+
the same game, in a browser or headless and faster than real time. It only
|
|
278
|
+
steps while the room is playing: pausing freezes the game and its game time.
|
|
364
279
|
|
|
365
|
-
|
|
366
|
-
|
|
280
|
+
```ts filename="src/game/engine/simulation.ts"
|
|
281
|
+
import { defineAirJamSimulation } from "@air-jam/sdk/core";
|
|
367
282
|
|
|
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);
|
|
283
|
+
export const simulation = defineAirJamSimulation<Sim, GameInput, SimEvent>({
|
|
284
|
+
stepMs: 1000 / 60,
|
|
285
|
+
create: ({ random }) => createSim(random),
|
|
286
|
+
step: (sim, { input, players, random, emit }) => {
|
|
287
|
+
advance(sim, players, input);
|
|
386
288
|
},
|
|
289
|
+
// What agents and tests see of the game, as plain JSON.
|
|
290
|
+
observe: (sim) => ({ fighters: sim.fighters }),
|
|
387
291
|
});
|
|
388
292
|
```
|
|
389
293
|
|
|
294
|
+
Declare it with `defineAirJamGame({ simulation })`. When its code is heavy, such
|
|
295
|
+
as a physics engine, declare a loader instead,
|
|
296
|
+
`simulation: () => import("./game/engine/simulation").then((m) => m.simulation)`,
|
|
297
|
+
so only the host downloads it and controllers stay small. The host draws and
|
|
298
|
+
plays feedback from it, never advances it:
|
|
299
|
+
|
|
300
|
+
- `useSimulationFrame(({ sim, alpha }) => draw(sim, alpha))` runs after every
|
|
301
|
+
display frame; `alpha` interpolates between steps.
|
|
302
|
+
- `useSimulationEvents((event) => playSound(event))` receives what `step`
|
|
303
|
+
emitted, such as a hit.
|
|
304
|
+
- `useSimulationSelector((sim) => value, isEqual?)` reads a value for UI, such
|
|
305
|
+
as a health bar or an entity list, re-rendering only when it changes.
|
|
306
|
+
- `useSimulation()` returns the clock itself.
|
|
307
|
+
- `const captureFrame = useHostAgentCapture((target, options) => drawAgentCanvas(canvas, target, options))`
|
|
308
|
+
optionally gives agents a sharper host render surface. Supporting browsers
|
|
309
|
+
already capture the visible host stage, including HTML lobbies and nested canvases;
|
|
310
|
+
this callback takes priority when it returns true. Returning false (for example,
|
|
311
|
+
before a game canvas mounts) uses the native stage instead.
|
|
312
|
+
Call `captureFrame()` immediately after
|
|
313
|
+
drawing the game and its HUD. The SDK owns the persistent capture canvas and
|
|
314
|
+
records native video at 30 fps; still observations use the same drawing callback.
|
|
315
|
+
Still observations and frame-based test clips encode JPEGs asynchronously. The
|
|
316
|
+
clip sampler skips ticks while encoding is pending rather than queueing frames.
|
|
317
|
+
If you call `captureAgentCanvas()` directly, await its image result.
|
|
318
|
+
|
|
319
|
+
Step code reads stores with `gameStore.getState()` and dispatches with
|
|
320
|
+
`gameStore.actions`; it never reads `Date.now()` or `Math.random()`.
|
|
321
|
+
|
|
390
322
|
**Auto Room Join from URL:**
|
|
391
323
|
|
|
392
324
|
Controllers automatically join rooms from URL query parameters:
|
|
@@ -403,16 +335,15 @@ This is how QR code scanning works—the host generates a URL with the room code
|
|
|
403
335
|
|
|
404
336
|
Use these only when integrating platform-specific runtime behavior.
|
|
405
337
|
|
|
406
|
-
###
|
|
338
|
+
### Room session access (escape hatch)
|
|
407
339
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
```
|
|
340
|
+
`host.room` and `controller.session` are the page's sessions in the room
|
|
341
|
+
(`@air-jam/sdk/room-session`). Networked stores and signals cover what games
|
|
342
|
+
need; reach for the session only for a channel of your own.
|
|
412
343
|
|
|
413
344
|
### Internal context hooks
|
|
414
345
|
|
|
415
|
-
`useAirJamContext`, `useAirJamConfig
|
|
346
|
+
`useAirJamContext`, `useAirJamConfig` and `useAirJamState` are intentionally internal and are not part of the root public SDK surface.
|
|
416
347
|
|
|
417
348
|
---
|
|
418
349
|
|
|
@@ -439,27 +370,25 @@ const unsubscribe = onAirJamDiagnostic((diagnostic) => {
|
|
|
439
370
|
| `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
371
|
| `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
372
|
| `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
373
|
| `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
374
|
| `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. |
|
|
375
|
+
| `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. |
|
|
376
|
+
| `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
377
|
|
|
451
378
|
---
|
|
452
379
|
|
|
453
380
|
## Types
|
|
454
381
|
|
|
455
|
-
### `
|
|
382
|
+
### `AirJamPlayer`
|
|
456
383
|
|
|
457
384
|
```typescript
|
|
458
|
-
interface
|
|
459
|
-
id: string; //
|
|
385
|
+
interface AirJamPlayer {
|
|
386
|
+
id: string; // The seat the room assigned ("p1", "p2", …)
|
|
460
387
|
label: string; // Display name (e.g., "Player 1")
|
|
461
|
-
color
|
|
388
|
+
color: string; // Assigned color (e.g., "#FF5733")
|
|
462
389
|
avatarId?: string; // Preset avatar id chosen by the player
|
|
390
|
+
kind: "phone" | "preview" | "agent";
|
|
391
|
+
connected: boolean; // false while the phone is away
|
|
463
392
|
}
|
|
464
393
|
```
|
|
465
394
|
|
|
@@ -498,3 +427,10 @@ interface ToastSignalPayload {
|
|
|
498
427
|
duration?: number; // Optional duration in ms
|
|
499
428
|
}
|
|
500
429
|
```
|
|
430
|
+
|
|
431
|
+
## useAirJamSettings
|
|
432
|
+
|
|
433
|
+
Host rendering reads declared settings with `useAirJamSettings(gameSettings)`.
|
|
434
|
+
The hook returns typed current values and subscribes to accepted host updates.
|
|
435
|
+
Controllers read display values copied to replicated store state. See
|
|
436
|
+
[Game Settings](/docs/sdk/settings) for the full contract.
|