@air-jam/cli 0.0.0-stage → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +145 -2
- package/bin/airjam.mjs +66 -0
- package/dist/ai-pack.d.ts +44 -0
- package/dist/ai-pack.js +15 -0
- package/dist/ai-pack.js.map +1 -0
- package/dist/chunk-A4V2N4LX.js +938 -0
- package/dist/chunk-A4V2N4LX.js.map +1 -0
- package/dist/chunk-J5MAU4QZ.js +102 -0
- package/dist/chunk-J5MAU4QZ.js.map +1 -0
- package/dist/chunk-N7AI7HZK.js +529 -0
- package/dist/chunk-N7AI7HZK.js.map +1 -0
- package/dist/development.d.ts +98 -0
- package/dist/development.js +33 -0
- package/dist/development.js.map +1 -0
- package/dist/documentation.d.ts +27 -0
- package/dist/documentation.js +7 -0
- package/dist/documentation.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2184 -0
- package/dist/index.js.map +1 -0
- package/dist/scaffold.d.ts +6 -0
- package/dist/scaffold.js +20 -0
- package/dist/scaffold.js.map +1 -0
- package/package.json +78 -3
- package/runtime/local-network.d.mts +8 -0
- package/runtime/local-network.mjs +9 -0
- package/runtime/vite-config.d.mts +42 -0
- package/runtime/vite-config.mjs +130 -0
- package/runtime/vite-https.d.mts +11 -0
- package/runtime/vite-https.mjs +41 -0
- package/template-assets/bootstrap/.claude/launch.json +12 -0
- package/template-assets/bootstrap/AGENTS.md +248 -0
- package/template-assets/bootstrap/CLAUDE.md +63 -0
- package/template-assets/bootstrap/_gitignore +6 -0
- package/template-assets/bootstrap/skills/airjam-docs-workflow/SKILL.md +49 -0
- package/template-assets/bootstrap/skills/airjam-mcp/SKILL.md +56 -0
- package/template-assets/bootstrap/skills/canvas-2d-gameplay/SKILL.md +47 -0
- package/template-assets/bootstrap/skills/controller-ui/SKILL.md +69 -0
- package/template-assets/bootstrap/skills/debug-and-test/SKILL.md +72 -0
- package/template-assets/bootstrap/skills/game-architecture/SKILL.md +58 -0
- package/template-assets/bootstrap/skills/game-state-and-rendering/SKILL.md +44 -0
- package/template-assets/bootstrap/skills/host-surface/SKILL.md +52 -0
- package/template-assets/bootstrap/skills/index.md +36 -0
- package/template-assets/bootstrap/skills/prefab-authoring/SKILL.md +48 -0
- package/template-assets/bootstrap/skills/r3f-3d-scene/SKILL.md +51 -0
- package/template-assets/documentation/for-agents/controller-ui/page.mdx +60 -0
- package/template-assets/documentation/for-agents/page.mdx +98 -0
- package/template-assets/documentation/for-agents/project-structure/page.mdx +172 -0
- package/template-assets/documentation/for-agents/state-and-rendering/page.mdx +79 -0
- package/template-assets/documentation/getting-started/debugging/page.mdx +165 -0
- package/template-assets/documentation/getting-started/dev-logs/page.mdx +171 -0
- package/template-assets/documentation/getting-started/game-ideas/page.mdx +116 -0
- package/template-assets/documentation/getting-started/introduction/page.mdx +183 -0
- package/template-assets/documentation/getting-started/migrating-legacy-games/page.mdx +427 -0
- package/template-assets/documentation/getting-started/quick-start/page.mdx +333 -0
- package/template-assets/documentation/how-it-works/architecture/page.mdx +189 -0
- package/template-assets/documentation/how-it-works/host-system/page.mdx +267 -0
- package/template-assets/documentation/manifest.json +430 -0
- package/template-assets/documentation/sdk/hooks/page.mdx +500 -0
- package/template-assets/documentation/sdk/input-system/page.mdx +270 -0
- package/template-assets/documentation/sdk/networked-state/page.mdx +213 -0
- package/template-assets/documentation/sdk/ui-components/page.mdx +227 -0
- package/template-assets/managed/.airjam/ai-pack.json +191 -0
- package/template-assets/managed/docs/airjam/agent-gold-path.md +67 -0
- package/template-assets/managed/docs/airjam/agent-mcp.md +135 -0
- package/template-assets/managed/docs/airjam/canvas-2d-gameplay.md +82 -0
- package/template-assets/managed/docs/airjam/composition-shell-contract.md +87 -0
- package/template-assets/managed/docs/airjam/debug-and-testing.md +86 -0
- package/template-assets/managed/docs/airjam/development-loop.md +51 -0
- package/template-assets/managed/docs/airjam/docs-index.md +58 -0
- package/template-assets/managed/docs/airjam/generated/architecture.md +178 -0
- package/template-assets/managed/docs/airjam/generated/controller-ui.md +61 -0
- package/template-assets/managed/docs/airjam/generated/debugging-and-logs.md +164 -0
- package/template-assets/managed/docs/airjam/generated/for-agents.md +99 -0
- package/template-assets/managed/docs/airjam/generated/game-ideas.md +107 -0
- package/template-assets/managed/docs/airjam/generated/host-system.md +251 -0
- package/template-assets/managed/docs/airjam/generated/input-system.md +260 -0
- package/template-assets/managed/docs/airjam/generated/introduction.md +175 -0
- package/template-assets/managed/docs/airjam/generated/migrating-legacy-games.md +426 -0
- package/template-assets/managed/docs/airjam/generated/networked-state.md +214 -0
- package/template-assets/managed/docs/airjam/generated/project-structure.md +171 -0
- package/template-assets/managed/docs/airjam/generated/quick-start.md +334 -0
- package/template-assets/managed/docs/airjam/generated/sdk-hooks.md +490 -0
- package/template-assets/managed/docs/airjam/generated/state-and-rendering.md +80 -0
- package/template-assets/managed/docs/airjam/generated/ui-components.md +222 -0
- package/template-assets/managed/docs/airjam/generated/unified-dev-logs.md +170 -0
- package/template-assets/managed/docs/airjam/iconography.md +55 -0
- package/template-assets/managed/docs/airjam/prefab-authoring.md +112 -0
- package/template-assets/managed/docs/airjam/preview-controllers.md +126 -0
- package/template-assets/managed/docs/airjam/r3f-3d-scene.md +97 -0
- package/template-assets/managed/docs/airjam/state-lanes-cookbook.md +106 -0
- package/template-assets/managed/docs/airjam/visual-system.md +103 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
# Input System
|
|
4
|
+
|
|
5
|
+
The Air Jam input system provides type-safe, validated controller input with clear behavior semantics.
|
|
6
|
+
|
|
7
|
+
## Input Flow
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
<InputFlowDiagram />
|
|
11
|
+
|
|
12
|
+
## Configuration
|
|
13
|
+
|
|
14
|
+
Configure input in your canonical Air Jam app setup:
|
|
15
|
+
|
|
16
|
+
```tsx filename="src/airjam.config.ts"
|
|
17
|
+
import { createAirJamApp, env } from "@air-jam/sdk";
|
|
18
|
+
import { z } from "zod";
|
|
19
|
+
|
|
20
|
+
const gameInputSchema = z.object({
|
|
21
|
+
vector: z.object({ x: z.number(), y: z.number() }),
|
|
22
|
+
action: z.boolean(),
|
|
23
|
+
ability: z.boolean(),
|
|
24
|
+
timestamp: z.number(),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
type GameInput = z.infer<typeof gameInputSchema>;
|
|
28
|
+
|
|
29
|
+
export const airjam = createAirJamApp({
|
|
30
|
+
runtime: env.vite(import.meta.env),
|
|
31
|
+
controllerPath: "/controller",
|
|
32
|
+
input: {
|
|
33
|
+
schema: gameInputSchema,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Default Behavior (No Extra Config)
|
|
39
|
+
|
|
40
|
+
Without any `input.behavior` overrides:
|
|
41
|
+
|
|
42
|
+
- booleans use `pulse` (tap-safe consume-on-read)
|
|
43
|
+
- vectors use `latest` (continuous current value)
|
|
44
|
+
- all other fields use `latest`
|
|
45
|
+
|
|
46
|
+
This default is the canonical path for most games.
|
|
47
|
+
|
|
48
|
+
## Optional Behavior Overrides
|
|
49
|
+
|
|
50
|
+
Use `input.behavior` only when your game has specialized needs.
|
|
51
|
+
|
|
52
|
+
```tsx filename="src/airjam.config.ts"
|
|
53
|
+
export const airjam = createAirJamApp({
|
|
54
|
+
runtime: env.vite(import.meta.env),
|
|
55
|
+
controllerPath: "/controller",
|
|
56
|
+
input: {
|
|
57
|
+
schema: gameInputSchema,
|
|
58
|
+
behavior: {
|
|
59
|
+
pulse: ["action", "ability"],
|
|
60
|
+
latest: ["vector"],
|
|
61
|
+
hold: ["menuVector"],
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Behavior modes:
|
|
68
|
+
|
|
69
|
+
- `pulse`: consume-on-read, ideal for one-shot actions (jump/fire/confirm)
|
|
70
|
+
- `latest`: current value only, ideal for movement sticks
|
|
71
|
+
- `hold`: vectors keep last non-zero direction until next non-zero value
|
|
72
|
+
|
|
73
|
+
## Migration From `latch`
|
|
74
|
+
|
|
75
|
+
Old config:
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
input: {
|
|
79
|
+
schema: gameInputSchema,
|
|
80
|
+
latch: {
|
|
81
|
+
booleanFields: ["action"],
|
|
82
|
+
vectorFields: ["vector"],
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
New config:
|
|
88
|
+
|
|
89
|
+
```tsx
|
|
90
|
+
input: {
|
|
91
|
+
schema: gameInputSchema,
|
|
92
|
+
behavior: {
|
|
93
|
+
pulse: ["action"],
|
|
94
|
+
latest: ["vector"],
|
|
95
|
+
},
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
For most games, remove the override entirely and keep `input: { schema }`.
|
|
100
|
+
|
|
101
|
+
## Schema Validation
|
|
102
|
+
|
|
103
|
+
When you provide a Zod schema, incoming input is validated before your game reads it.
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
// ✅ valid input
|
|
107
|
+
{
|
|
108
|
+
vector: { x: 0.5, y: -0.3 },
|
|
109
|
+
action: true,
|
|
110
|
+
ability: false,
|
|
111
|
+
timestamp: 1703123456789,
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ❌ invalid input (returns undefined + warning)
|
|
115
|
+
{
|
|
116
|
+
vector: { x: "bad", y: 0 },
|
|
117
|
+
action: true,
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Benefits:
|
|
122
|
+
|
|
123
|
+
- typed `getInput()` values
|
|
124
|
+
- malformed payloads blocked before gameplay logic
|
|
125
|
+
- clear runtime warnings during development
|
|
126
|
+
|
|
127
|
+
## Reading Input
|
|
128
|
+
|
|
129
|
+
### Main Host Loop
|
|
130
|
+
|
|
131
|
+
This example uses the starter host surface path.
|
|
132
|
+
Treat it as a recommended default, not a required filename.
|
|
133
|
+
It assumes your app has already mounted `airjam.Host` above this view.
|
|
134
|
+
|
|
135
|
+
```tsx filename="src/host/index.tsx"
|
|
136
|
+
import { useAirJamHost } from "@air-jam/sdk";
|
|
137
|
+
import { useFrame } from "@react-three/fiber";
|
|
138
|
+
|
|
139
|
+
const HostView = () => {
|
|
140
|
+
const host = useAirJamHost();
|
|
141
|
+
|
|
142
|
+
useFrame(() => {
|
|
143
|
+
host.players.forEach((player) => {
|
|
144
|
+
const input = host.getInput(player.id);
|
|
145
|
+
if (!input) return;
|
|
146
|
+
|
|
147
|
+
movePlayer(player.id, input.vector);
|
|
148
|
+
|
|
149
|
+
if (input.action) {
|
|
150
|
+
playerShoot(player.id);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
return <GameScene />;
|
|
156
|
+
};
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Performance-Critical Components
|
|
160
|
+
|
|
161
|
+
Use `useGetInput()` to avoid store-driven re-renders:
|
|
162
|
+
|
|
163
|
+
```tsx filename="src/game/components/ship.tsx"
|
|
164
|
+
import { useGetInput } from "@air-jam/sdk";
|
|
165
|
+
|
|
166
|
+
const Ship = ({ playerId }: { playerId: string }) => {
|
|
167
|
+
const getInput = useGetInput<typeof gameInputSchema>();
|
|
168
|
+
|
|
169
|
+
useFrame(() => {
|
|
170
|
+
const input = getInput(playerId);
|
|
171
|
+
if (!input) return;
|
|
172
|
+
|
|
173
|
+
shipRef.current.position.x += input.vector.x * SPEED;
|
|
174
|
+
shipRef.current.position.y += input.vector.y * SPEED;
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
return <mesh ref={shipRef}>...</mesh>;
|
|
178
|
+
};
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Controller Cadence
|
|
182
|
+
|
|
183
|
+
Use `useInputWriter()` with `useControllerTick()` for fixed-cadence publishing:
|
|
184
|
+
|
|
185
|
+
This example uses the starter controller surface path.
|
|
186
|
+
It assumes your app has already mounted `airjam.Controller` above this view.
|
|
187
|
+
|
|
188
|
+
```tsx filename="src/controller/index.tsx"
|
|
189
|
+
import {
|
|
190
|
+
useAirJamController,
|
|
191
|
+
useControllerTick,
|
|
192
|
+
useInputWriter,
|
|
193
|
+
} from "@air-jam/sdk";
|
|
194
|
+
import { useRef } from "react";
|
|
195
|
+
|
|
196
|
+
const ControllerView = () => {
|
|
197
|
+
const controller = useAirJamController();
|
|
198
|
+
const writeInput = useInputWriter();
|
|
199
|
+
const directionRef = useRef(0);
|
|
200
|
+
|
|
201
|
+
useControllerTick(
|
|
202
|
+
() => {
|
|
203
|
+
writeInput({
|
|
204
|
+
direction: directionRef.current,
|
|
205
|
+
action: false,
|
|
206
|
+
});
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
enabled:
|
|
210
|
+
controller.connectionStatus === "connected" &&
|
|
211
|
+
controller.runtimeState === "playing",
|
|
212
|
+
intervalMs: 16,
|
|
213
|
+
},
|
|
214
|
+
);
|
|
215
|
+
};
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Best Practices
|
|
219
|
+
|
|
220
|
+
### 1. Keep Input and State in Separate Lanes
|
|
221
|
+
|
|
222
|
+
- Input lane: `useInputWriter` + `getInput` / `useGetInput`
|
|
223
|
+
- State lane: `createAirJamStore` + `useActions`
|
|
224
|
+
|
|
225
|
+
Do not send per-frame analog input via store actions.
|
|
226
|
+
|
|
227
|
+
### 2. Keep Quickstart Config Minimal
|
|
228
|
+
|
|
229
|
+
Start with `input: { schema }`. Add `input.behavior` only for non-default needs.
|
|
230
|
+
|
|
231
|
+
### 3. Handle Missing Input Gracefully
|
|
232
|
+
|
|
233
|
+
```tsx filename="src/components/Player.tsx"
|
|
234
|
+
const input = getInput(playerId);
|
|
235
|
+
if (!input) return;
|
|
236
|
+
|
|
237
|
+
movePlayer(input.vector);
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### 4. Include Timestamps for Advanced Physics
|
|
241
|
+
|
|
242
|
+
```tsx filename="src/game/types.ts"
|
|
243
|
+
const inputSchema = z.object({
|
|
244
|
+
vector: z.object({ x: z.number(), y: z.number() }),
|
|
245
|
+
action: z.boolean(),
|
|
246
|
+
timestamp: z.number(),
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
const inputAge = Date.now() - input.timestamp;
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Common Issues
|
|
253
|
+
|
|
254
|
+
### Input Not Received
|
|
255
|
+
|
|
256
|
+
1. Check host connection status (`connected`)
|
|
257
|
+
2. Verify room code matches on host and controller
|
|
258
|
+
3. Check schema warnings in console (`[InputManager] Invalid input ...`)
|
|
259
|
+
|
|
260
|
+
### Input Feels Laggy
|
|
261
|
+
|
|
262
|
+
1. Use `useGetInput()` inside game objects
|
|
263
|
+
2. Verify your controller tick cadence (typically `16ms`)
|
|
264
|
+
3. Check network latency (include timestamps in schema)
|
|
265
|
+
|
|
266
|
+
### Buttons Feel Unreliable
|
|
267
|
+
|
|
268
|
+
1. Confirm the field is boolean in your schema
|
|
269
|
+
2. Keep default behavior (`pulse`) for action buttons
|
|
270
|
+
3. Avoid dispatching button presses through store actions
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
# Networked State
|
|
4
|
+
|
|
5
|
+
`createAirJamStore` is the canonical shared-state lane for Air Jam:
|
|
6
|
+
|
|
7
|
+
- Host is the only source of truth.
|
|
8
|
+
- Controllers dispatch actions through RPC.
|
|
9
|
+
- Host applies actions, then state sync is broadcast back.
|
|
10
|
+
|
|
11
|
+
Use this lane for replicated game state, not per-frame input.
|
|
12
|
+
|
|
13
|
+
## Canonical Action Model
|
|
14
|
+
|
|
15
|
+
Actions always use this shape on the host:
|
|
16
|
+
|
|
17
|
+
1. First argument: `ctx` (identity and role)
|
|
18
|
+
2. Second argument: `payload` (either `undefined` or one typed plain-object payload)
|
|
19
|
+
|
|
20
|
+
Controller-side dispatch always uses:
|
|
21
|
+
|
|
22
|
+
1. `const actions = useGameStore.useActions()`
|
|
23
|
+
2. `actions.someAction()` or `actions.someAction({ ...payload })`
|
|
24
|
+
|
|
25
|
+
No trailing `controllerId` arguments and no multi-argument action payloads.
|
|
26
|
+
No primitive, array, or event-like root payloads.
|
|
27
|
+
|
|
28
|
+
## Create a Store
|
|
29
|
+
|
|
30
|
+
This example uses a starter-friendly `stores/` folder shape.
|
|
31
|
+
The important contract is the action model and ownership boundary, not the exact filename.
|
|
32
|
+
|
|
33
|
+
```tsx filename="src/game/stores/game-store.ts"
|
|
34
|
+
import { createAirJamStore } from "@air-jam/sdk";
|
|
35
|
+
|
|
36
|
+
type Team = "team1" | "team2";
|
|
37
|
+
|
|
38
|
+
interface GameState {
|
|
39
|
+
phase: "lobby" | "playing";
|
|
40
|
+
teamAssignments: Record<string, Team>;
|
|
41
|
+
scores: { team1: number; team2: number };
|
|
42
|
+
actions: {
|
|
43
|
+
joinTeam: (
|
|
44
|
+
ctx: { actorId: string; role: "controller" | "host" },
|
|
45
|
+
payload: { team: Team },
|
|
46
|
+
) => void;
|
|
47
|
+
scorePoint: (
|
|
48
|
+
_ctx: { actorId: string; role: "controller" | "host" },
|
|
49
|
+
payload: { team: Team },
|
|
50
|
+
) => void;
|
|
51
|
+
setPhase: (
|
|
52
|
+
_ctx: { actorId: string; role: "controller" | "host" },
|
|
53
|
+
payload: { phase: "lobby" | "playing" },
|
|
54
|
+
) => void;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const useGameStore = createAirJamStore<GameState>((set) => ({
|
|
59
|
+
phase: "lobby",
|
|
60
|
+
teamAssignments: {},
|
|
61
|
+
scores: { team1: 0, team2: 0 },
|
|
62
|
+
actions: {
|
|
63
|
+
joinTeam: ({ actorId }, { team }) =>
|
|
64
|
+
set((state) => ({
|
|
65
|
+
teamAssignments: {
|
|
66
|
+
...state.teamAssignments,
|
|
67
|
+
[actorId]: team,
|
|
68
|
+
},
|
|
69
|
+
})),
|
|
70
|
+
scorePoint: (_ctx, { team }) =>
|
|
71
|
+
set((state) => ({
|
|
72
|
+
scores: {
|
|
73
|
+
...state.scores,
|
|
74
|
+
[team]: state.scores[team] + 1,
|
|
75
|
+
},
|
|
76
|
+
})),
|
|
77
|
+
setPhase: (_ctx, { phase }) => set({ phase }),
|
|
78
|
+
},
|
|
79
|
+
}));
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Use on Controller
|
|
83
|
+
|
|
84
|
+
This controller example matches the starter template surface layout.
|
|
85
|
+
It assumes your app has already mounted `airjam.Controller`.
|
|
86
|
+
|
|
87
|
+
```tsx filename="src/controller/index.tsx"
|
|
88
|
+
import { useAirJamController } from "@air-jam/sdk";
|
|
89
|
+
import { useGameStore } from "../game/stores/game-store";
|
|
90
|
+
|
|
91
|
+
export const ControllerView = () => {
|
|
92
|
+
const controller = useAirJamController();
|
|
93
|
+
const actions = useGameStore.useActions();
|
|
94
|
+
const teamAssignments = useGameStore((state) => state.teamAssignments);
|
|
95
|
+
|
|
96
|
+
const myTeam = controller.controllerId
|
|
97
|
+
? teamAssignments[controller.controllerId]
|
|
98
|
+
: null;
|
|
99
|
+
|
|
100
|
+
return (
|
|
101
|
+
<div>
|
|
102
|
+
<p>My team: {myTeam ?? "unassigned"}</p>
|
|
103
|
+
<button onClick={() => actions.joinTeam({ team: "team1" })}>
|
|
104
|
+
Join Team 1
|
|
105
|
+
</button>
|
|
106
|
+
</div>
|
|
107
|
+
);
|
|
108
|
+
};
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Use on Host
|
|
112
|
+
|
|
113
|
+
This host example also matches the starter template surface layout.
|
|
114
|
+
It assumes your app has already mounted `airjam.Host`.
|
|
115
|
+
|
|
116
|
+
```tsx filename="src/host/index.tsx"
|
|
117
|
+
import { useGameStore } from "../game/stores/game-store";
|
|
118
|
+
|
|
119
|
+
export const HostView = () => {
|
|
120
|
+
const phase = useGameStore((state) => state.phase);
|
|
121
|
+
const scores = useGameStore((state) => state.scores);
|
|
122
|
+
const actions = useGameStore.useActions();
|
|
123
|
+
|
|
124
|
+
return (
|
|
125
|
+
<div>
|
|
126
|
+
<p>Phase: {phase}</p>
|
|
127
|
+
<p>
|
|
128
|
+
{scores.team1} - {scores.team2}
|
|
129
|
+
</p>
|
|
130
|
+
<button onClick={() => actions.setPhase({ phase: "playing" })}>
|
|
131
|
+
Start
|
|
132
|
+
</button>
|
|
133
|
+
<button onClick={() => actions.scorePoint({ team: "team1" })}>
|
|
134
|
+
Team 1 +1
|
|
135
|
+
</button>
|
|
136
|
+
</div>
|
|
137
|
+
);
|
|
138
|
+
};
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Action Flow
|
|
142
|
+
|
|
143
|
+
1. Controller calls `actions.someAction()` or `actions.someAction({ ...payload })`.
|
|
144
|
+
2. SDK emits `controller:action_rpc`.
|
|
145
|
+
3. Server validates and injects actor identity from socket ownership.
|
|
146
|
+
4. Host receives `airjam:action_rpc` with `{ actor, payload }`.
|
|
147
|
+
5. Host executes action handler `(ctx, payload)` and updates state.
|
|
148
|
+
6. Host emits `host:state_sync`; controllers receive `airjam:state_sync`.
|
|
149
|
+
|
|
150
|
+
## Rules
|
|
151
|
+
|
|
152
|
+
1. Keep all networked mutations inside `actions`.
|
|
153
|
+
2. Always dispatch via `useActions()`; do not call `state.actions.*` directly.
|
|
154
|
+
3. Use one payload object per action for stable evolution.
|
|
155
|
+
4. Root payloads must be omitted or plain objects.
|
|
156
|
+
5. `T | undefined` payload unions are not valid roots. If an action has no payload, omit it entirely.
|
|
157
|
+
6. Nested values must stay RPC-serializable.
|
|
158
|
+
7. Use `ctx.actorId` for identity-aware actions (team join, ownership, etc.).
|
|
159
|
+
8. `ctx.actorId` is always the dispatcher identity. If host code dispatches through `useActions()`, then `ctx.actorId` is the host.
|
|
160
|
+
9. If the host intentionally needs to run the same semantic player action as controller `X`, use `useStore.asPlayer("X")` instead of inventing a target payload for an actor-owned action.
|
|
161
|
+
10. Keep per-frame gameplay input in `useInputWriter` / `host.getInput`, not in this store.
|
|
162
|
+
|
|
163
|
+
## Accept And Reject Examples
|
|
164
|
+
|
|
165
|
+
If a store action returns `void`, Air Jam treats it as accepted.
|
|
166
|
+
|
|
167
|
+
Return `rejectAirJamAction(...)` when the semantic action should fail with a clear machine-readable reason, and return `acceptAirJamAction(result)` when the semantic action should succeed with explicit result data:
|
|
168
|
+
|
|
169
|
+
```tsx
|
|
170
|
+
import {
|
|
171
|
+
acceptAirJamAction,
|
|
172
|
+
createAirJamStore,
|
|
173
|
+
rejectAirJamAction,
|
|
174
|
+
} from "@air-jam/sdk";
|
|
175
|
+
|
|
176
|
+
export const useGameStore = createAirJamStore((set) => ({
|
|
177
|
+
aliveByPlayerId: {} as Record<string, boolean>,
|
|
178
|
+
cooldownMsByPlayerId: {} as Record<string, number>,
|
|
179
|
+
actions: {
|
|
180
|
+
fire: ({ actorId }) =>
|
|
181
|
+
set((state) => {
|
|
182
|
+
if (!actorId || state.aliveByPlayerId[actorId] === false) {
|
|
183
|
+
return rejectAirJamAction("player_dead", "Dead players cannot fire.");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const cooldownMs = 4500;
|
|
187
|
+
return acceptAirJamAction({ cooldownMs });
|
|
188
|
+
}),
|
|
189
|
+
},
|
|
190
|
+
}));
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Use this pattern when the caller needs a real semantic outcome.
|
|
194
|
+
|
|
195
|
+
## Embedded Arcade Runtimes
|
|
196
|
+
|
|
197
|
+
When a game runs inside Arcade, `createAirJamStore` still works the same way in game code.
|
|
198
|
+
|
|
199
|
+
The SDK/runtime automatically resolves the correct replicated store domain from the embedded runtime context, so you should:
|
|
200
|
+
|
|
201
|
+
1. keep normal `createAirJamStore(...)` usage in games
|
|
202
|
+
2. not parse Arcade URL params in app/game code
|
|
203
|
+
3. not pick custom store domains in game code just to work when embedded
|
|
204
|
+
|
|
205
|
+
## API Summary
|
|
206
|
+
|
|
207
|
+
`createAirJamStore<T>(initializer)` returns:
|
|
208
|
+
|
|
209
|
+
1. Zustand-compatible hook for selectors.
|
|
210
|
+
2. `useActions()` dispatch map with `() => void` or `(payloadObject) => void` signatures.
|
|
211
|
+
3. `asPlayer(controllerId)` host-only dispatch map for explicit player impersonation of semantic actions.
|
|
212
|
+
|
|
213
|
+
`T` must include an `actions` object with host handlers `(ctx, payload) => void`, where `payload` is `undefined` or a plain object.
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
# SDK UI Components
|
|
4
|
+
|
|
5
|
+
`@air-jam/sdk` is headless by default. `@air-jam/sdk/ui` provides optional, composable UI primitives you can drop into your own game layout.
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import {
|
|
9
|
+
SurfaceViewport,
|
|
10
|
+
PlayerAvatar,
|
|
11
|
+
RoomQrCode,
|
|
12
|
+
VolumeControls,
|
|
13
|
+
Button,
|
|
14
|
+
Slider,
|
|
15
|
+
} from "@air-jam/sdk/ui";
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Canonical Components
|
|
19
|
+
|
|
20
|
+
### `RoomQrCode`
|
|
21
|
+
|
|
22
|
+
Render a room join QR code from any URL string.
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { RoomQrCode } from "@air-jam/sdk/ui";
|
|
26
|
+
import { useAirJamHost } from "@air-jam/sdk";
|
|
27
|
+
|
|
28
|
+
const LobbyQr = () => {
|
|
29
|
+
const host = useAirJamHost();
|
|
30
|
+
|
|
31
|
+
if (host.joinUrlStatus !== "ready") {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<RoomQrCode
|
|
37
|
+
value={host.joinUrl}
|
|
38
|
+
size={180}
|
|
39
|
+
padding={1}
|
|
40
|
+
className="rounded-xl border border-white/20 bg-black/50 p-2"
|
|
41
|
+
/>
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
If you are rendering before the join URL has been resolved, prefer gating with `host.joinUrlStatus === "ready"` and show your own loading state until then.
|
|
47
|
+
|
|
48
|
+
Props:
|
|
49
|
+
|
|
50
|
+
- `value: string` (required)
|
|
51
|
+
- `size?: number` default `180`
|
|
52
|
+
- `padding?: number` default `1`
|
|
53
|
+
- `foregroundColor?: string` default `#111111`
|
|
54
|
+
- `backgroundColor?: string` default `#ffffff`
|
|
55
|
+
- `errorCorrectionLevel?: "L" | "M" | "Q" | "H"` default `"M"`
|
|
56
|
+
- `className?: string`
|
|
57
|
+
- `style?: CSSProperties`
|
|
58
|
+
- `alt?: string` default `"Room join QR code"`
|
|
59
|
+
|
|
60
|
+
Use it for:
|
|
61
|
+
|
|
62
|
+
- lobby overlays
|
|
63
|
+
- pause overlays
|
|
64
|
+
- spectator join prompts
|
|
65
|
+
|
|
66
|
+
Avoid:
|
|
67
|
+
|
|
68
|
+
- generating URLs manually when `host.joinUrl` / `controller.joinUrl` already exists
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
### `PlayerAvatar`
|
|
73
|
+
|
|
74
|
+
Render a stable avatar ringed by player color from `PlayerProfile`. Set `isBot` when you want the same shell to represent an AI-controlled slot with a bot icon instead of player artwork.
|
|
75
|
+
|
|
76
|
+
```tsx
|
|
77
|
+
import { PlayerAvatar } from "@air-jam/sdk/ui";
|
|
78
|
+
|
|
79
|
+
<PlayerAvatar player={player} size="md" />;
|
|
80
|
+
<PlayerAvatar player={player} isBot size="md" />;
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Props:
|
|
84
|
+
|
|
85
|
+
- `player: PlayerProfile` (required)
|
|
86
|
+
- `isBot?: boolean` render the shared bot icon instead of the player avatar image
|
|
87
|
+
- `size?: "sm" | "md" | "lg"` default `"md"`
|
|
88
|
+
- `className?: string`
|
|
89
|
+
|
|
90
|
+
Use it for:
|
|
91
|
+
|
|
92
|
+
- team strips
|
|
93
|
+
- connected player rows
|
|
94
|
+
- controller headers
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
### `SurfaceViewport`
|
|
99
|
+
|
|
100
|
+
Own safe-area, orientation, and page-level UI scaling for a game surface.
|
|
101
|
+
It should fill the available screen while scaling the UI system proportionally, rather than letterboxing a fixed contained stage.
|
|
102
|
+
Internally it publishes a local `--airjam-ui-scale` into Tailwind's own spacing, text, radius, and container variables so normal Tailwind utilities keep working.
|
|
103
|
+
|
|
104
|
+
```tsx
|
|
105
|
+
import { SurfaceViewport } from "@air-jam/sdk/ui";
|
|
106
|
+
|
|
107
|
+
<SurfaceViewport orientation="portrait" className="bg-zinc-950">
|
|
108
|
+
<ControllerView />
|
|
109
|
+
</SurfaceViewport>;
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Props:
|
|
113
|
+
|
|
114
|
+
- `orientation?: "portrait" | "landscape"`
|
|
115
|
+
- `children: ReactNode` (required)
|
|
116
|
+
- `designWidth?: number`
|
|
117
|
+
- `designHeight?: number`
|
|
118
|
+
- `uiScaleMultiplier?: number`
|
|
119
|
+
- `minScale?: number`
|
|
120
|
+
- `maxScale?: number`
|
|
121
|
+
- `lockOnGesture?: boolean` default `true`
|
|
122
|
+
- `className?: string`
|
|
123
|
+
- `contentClassName?: string`
|
|
124
|
+
- `style?: CSSProperties`
|
|
125
|
+
|
|
126
|
+
Use it for:
|
|
127
|
+
|
|
128
|
+
- controller routes that should keep a stable phone-like UI feel while still filling the screen
|
|
129
|
+
- host routes that should scale proportionally without collapsing into ordinary website behavior
|
|
130
|
+
|
|
131
|
+
Avoid:
|
|
132
|
+
|
|
133
|
+
- nesting multiple surface viewports
|
|
134
|
+
- treating it like a centered black-bar stage wrapper
|
|
135
|
+
- depending on arbitrary raw `px` values for most of the UI chrome when a normal Tailwind scale utility would do
|
|
136
|
+
- assuming native OS/browser lock is guaranteed everywhere
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
### `VolumeControls`
|
|
141
|
+
|
|
142
|
+
Ready-made control block for master/music/sfx sliders bound to the shared platform settings runtime.
|
|
143
|
+
|
|
144
|
+
```tsx
|
|
145
|
+
import { VolumeControls } from "@air-jam/sdk/ui";
|
|
146
|
+
|
|
147
|
+
<VolumeControls className="w-full max-w-xs" compact />;
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Mount it below `PlatformSettingsRuntime` when you want slider changes to persist in Arcade and inherit into embedded games.
|
|
151
|
+
|
|
152
|
+
Props:
|
|
153
|
+
|
|
154
|
+
- `className?: string`
|
|
155
|
+
- `compact?: boolean`
|
|
156
|
+
|
|
157
|
+
Use it for:
|
|
158
|
+
|
|
159
|
+
- in-game settings overlays
|
|
160
|
+
- pause/settings drawers
|
|
161
|
+
- Arcade- or platform-owned settings panels
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## Base Primitives
|
|
166
|
+
|
|
167
|
+
### `Button`
|
|
168
|
+
|
|
169
|
+
Styling primitive for consistent buttons.
|
|
170
|
+
|
|
171
|
+
```tsx
|
|
172
|
+
import { Button } from "@air-jam/sdk/ui";
|
|
173
|
+
|
|
174
|
+
<Button variant="secondary" size="lg">
|
|
175
|
+
Start Match
|
|
176
|
+
</Button>;
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Common props:
|
|
180
|
+
|
|
181
|
+
- `variant?: "default" | "destructive" | "outline" | "secondary" | "ghost" | "link"`
|
|
182
|
+
- `size?: "default" | "sm" | "lg" | "icon" | "icon-sm" | "icon-lg"`
|
|
183
|
+
- `asChild?: boolean`
|
|
184
|
+
|
|
185
|
+
### `Slider`
|
|
186
|
+
|
|
187
|
+
Styling primitive for ranges and tuning inputs.
|
|
188
|
+
|
|
189
|
+
```tsx
|
|
190
|
+
import { Slider } from "@air-jam/sdk/ui";
|
|
191
|
+
|
|
192
|
+
<Slider
|
|
193
|
+
value={[pointsToWin]}
|
|
194
|
+
min={3}
|
|
195
|
+
max={15}
|
|
196
|
+
step={1}
|
|
197
|
+
onValueChange={([value]) => setPointsToWin(value)}
|
|
198
|
+
/>;
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## Composition Patterns
|
|
204
|
+
|
|
205
|
+
### Lobby Overlay
|
|
206
|
+
|
|
207
|
+
Combine:
|
|
208
|
+
|
|
209
|
+
- `RoomQrCode` for quick join
|
|
210
|
+
- `PlayerAvatar` for team roster
|
|
211
|
+
- `Button` for start/ready actions
|
|
212
|
+
|
|
213
|
+
### Controller Route
|
|
214
|
+
|
|
215
|
+
Combine:
|
|
216
|
+
|
|
217
|
+
- `SurfaceViewport` for shared full-screen UI scaling
|
|
218
|
+
- `PlayerAvatar` in a compact header
|
|
219
|
+
- `Button` + `Slider` for controls/options
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## Anti-Patterns
|
|
224
|
+
|
|
225
|
+
- Do not build gameplay input on UI components; keep input lane in `useInputWriter` / `useGetInput`.
|
|
226
|
+
- Do not rely on UI primitives for room/session lifecycle; lifecycle stays in headless SDK hooks.
|
|
227
|
+
- Do not fork component behavior in each game if composition solves it; prefer small wrappers around canonical primitives.
|