castle-web-cli 0.4.208 → 0.4.210
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/dist/api.d.ts +5 -0
- package/dist/castle-host/host.js +234 -0
- package/dist/castleJson.d.ts +4 -0
- package/dist/castleJson.js +3 -1
- package/dist/devSessionServer.js +1 -0
- package/dist/editorConfig.d.ts +12 -0
- package/dist/editorConfig.js +72 -6
- package/dist/init.js +1 -1
- package/dist/modulePreload.d.ts +14 -0
- package/dist/modulePreload.js +150 -0
- package/dist/native/loop.js +1 -1
- package/dist/native/playtest-executor.js +36 -4
- package/dist/native/playtest-remote.d.ts +1 -0
- package/dist/native/playtest-remote.js +61 -5
- package/dist/native/playtest.d.ts +35 -2
- package/dist/native/playtest.js +162 -18
- package/dist/platformDoc.d.ts +1 -0
- package/dist/platformDoc.js +24 -3
- package/dist/playUrl.d.ts +3 -0
- package/dist/playUrl.js +12 -0
- package/dist/save-deck.js +1 -0
- package/dist/serve.js +2 -0
- package/dist/shell/assets/{index-b_bebkRb.css → index-BCrgYh76.css} +1 -1
- package/dist/shell/assets/{index-C6SXXPwm.js → index-DzLbNBc7.js} +68 -68
- package/dist/shell/index.html +2 -2
- package/kits/base/CLAUDE.md +1 -0
- package/kits/base/castle.json +1 -1
- package/kits/base/sdk/README.md +70 -7
- package/kits/base/sdk/castle.d.ts +1 -0
- package/kits/base/sdk/commands.d.ts +78 -0
- package/kits/base/sdk/commands.js +25 -0
- package/kits/base/sdk/multiplayer.d.ts +3 -0
- package/kits/base/sdk/multiplayer.js +13 -0
- package/kits/base/sdk/multiplayerVoice.d.ts +91 -0
- package/kits/base/sdk/multiplayerVoice.js +424 -0
- package/kits/base/sdk/transport.d.ts +2 -1
- package/kits/base/sdk/transport.js +15 -1
- package/kits/multiplayer-2d/castle.json +2 -2
- package/kits/multiplayer-2d/code/systems/multiplayer.js +7 -1
- package/kits/multiplayer-3d/castle.json +2 -2
- package/kits/multiplayer-3d/code/systems/multiplayer.js +8 -4
- package/kits/physics-2d/castle.json +1 -1
- package/kits/physics-2d/editors/SceneEditor.jsx +18 -4
- package/kits/physics-2d/systems/media.js +3 -5
- package/kits/physics-3d/castle.json +2 -2
- package/kits/physics-3d/engine3d/PxModelEditor.jsx +2 -2
- package/kits/physics-3d/engine3d/pxmodel.js +7 -0
- package/kits/real-time/CLAUDE.md +3 -0
- package/kits/real-time/castle.json +1 -1
- package/kits/real-time/code/client/connection.js +17 -2
- package/kits/real-time/code/client/gameHooks.js +5 -0
- package/kits/real-time/code/client/joinOverlay.js +23 -5
- package/kits/real-time/code/client/messages.js +4 -0
- package/kits/real-time/code/server/persist.js +19 -10
- package/kits/real-time/code/server/session.js +19 -0
- package/package.json +2 -2
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import { hostRequest, setVoiceEventHandler } from "./transport";
|
|
2
|
+
// Voice chat in a multiplayer session. The host (castle.xyz or the Castle app) owns
|
|
3
|
+
// the microphone, the call and playback, and shows its own prompt and live-mic badge; a
|
|
4
|
+
// deck never touches audio. What a deck gets: whether voice is on, a way to ask the
|
|
5
|
+
// player to join, who is speaking, and a suggested volume and stereo position per
|
|
6
|
+
// player (proximity chat). Push-to-talk and muting are the player's, in the host's UI.
|
|
7
|
+
// A deck driving proximity every frame sends far more than the host needs.
|
|
8
|
+
export const VOICE_MIX_INTERVAL_MS = 100;
|
|
9
|
+
const DEFAULT_MIX = { gain: 1, pan: 0 };
|
|
10
|
+
let activeVoice = null;
|
|
11
|
+
function dispatchVoiceEvent(event) {
|
|
12
|
+
activeVoice?.receive(event);
|
|
13
|
+
}
|
|
14
|
+
// One multiplayer connection is live at a time, so one voice receives host events.
|
|
15
|
+
function setActiveVoice(voice, current) {
|
|
16
|
+
if (current && activeVoice !== current)
|
|
17
|
+
return;
|
|
18
|
+
activeVoice = voice;
|
|
19
|
+
}
|
|
20
|
+
export class MultiplayerVoiceImpl {
|
|
21
|
+
access;
|
|
22
|
+
currentState = "unavailable";
|
|
23
|
+
// The session this voice state belongs to; "" before the first attach or in solo.
|
|
24
|
+
voiceSessionId = "";
|
|
25
|
+
attaching = null;
|
|
26
|
+
// Bumped on every host state event, so a join reply never overrides a newer one.
|
|
27
|
+
hostStateVersion = 0;
|
|
28
|
+
pendingJoin = null;
|
|
29
|
+
joinRequest = null;
|
|
30
|
+
speakingUserIds = [];
|
|
31
|
+
speaking = [];
|
|
32
|
+
pendingMix = {};
|
|
33
|
+
sentMix = {};
|
|
34
|
+
// Spatial state: pending (not yet sent) and sent (re-sent when a call restarts).
|
|
35
|
+
pendingListener = null;
|
|
36
|
+
pendingPositions = {};
|
|
37
|
+
pendingOptions = null;
|
|
38
|
+
sentListener = null;
|
|
39
|
+
sentPositions = {};
|
|
40
|
+
sentOptions = null;
|
|
41
|
+
mixTimer = null;
|
|
42
|
+
lastMixSentAt = 0;
|
|
43
|
+
disposed = false;
|
|
44
|
+
states = new Set();
|
|
45
|
+
speakers = new Set();
|
|
46
|
+
constructor(access) {
|
|
47
|
+
this.access = access;
|
|
48
|
+
}
|
|
49
|
+
get state() {
|
|
50
|
+
return this.currentState;
|
|
51
|
+
}
|
|
52
|
+
// Called by the connection whenever it (re)gains a session. A reconnect keeps the
|
|
53
|
+
// session; a re-acquisition may land in a different one, or in solo, and voice for
|
|
54
|
+
// the old session ends with it. Asks the host where voice stands; a host without
|
|
55
|
+
// voice (an older app, local dev) answers with an error or "unavailable".
|
|
56
|
+
attach() {
|
|
57
|
+
if (this.disposed)
|
|
58
|
+
return Promise.resolve();
|
|
59
|
+
setActiveVoice(this);
|
|
60
|
+
setVoiceEventHandler(dispatchVoiceEvent);
|
|
61
|
+
const sessionId = this.access.isSolo() ? "" : this.access.sessionId();
|
|
62
|
+
if (this.voiceSessionId !== sessionId) {
|
|
63
|
+
this.endSession();
|
|
64
|
+
// The old session's state means nothing for the new one until the host answers.
|
|
65
|
+
this.setState("unavailable");
|
|
66
|
+
}
|
|
67
|
+
this.voiceSessionId = sessionId;
|
|
68
|
+
if (!sessionId) {
|
|
69
|
+
this.setState("unavailable");
|
|
70
|
+
this.attaching = null;
|
|
71
|
+
return Promise.resolve();
|
|
72
|
+
}
|
|
73
|
+
const attaching = hostRequest("voice.getState", { sessionId }).then((result) => {
|
|
74
|
+
if (this.isCurrent(sessionId))
|
|
75
|
+
this.setState(result.state);
|
|
76
|
+
}, () => {
|
|
77
|
+
if (this.isCurrent(sessionId))
|
|
78
|
+
this.setState("unavailable");
|
|
79
|
+
});
|
|
80
|
+
this.attaching = attaching;
|
|
81
|
+
return attaching;
|
|
82
|
+
}
|
|
83
|
+
// A second call while the player is still choosing shares the first one's answer.
|
|
84
|
+
requestJoin() {
|
|
85
|
+
if (!this.joinRequest) {
|
|
86
|
+
const request = this.askToJoin().finally(() => {
|
|
87
|
+
if (this.joinRequest === request)
|
|
88
|
+
this.joinRequest = null;
|
|
89
|
+
});
|
|
90
|
+
this.joinRequest = request;
|
|
91
|
+
}
|
|
92
|
+
return this.joinRequest;
|
|
93
|
+
}
|
|
94
|
+
async askToJoin() {
|
|
95
|
+
// Registered before any waiting, so leave() or a session change can withdraw a
|
|
96
|
+
// request that hasn't reached the host yet.
|
|
97
|
+
const pending = new AbortController();
|
|
98
|
+
this.pendingJoin = pending;
|
|
99
|
+
// `join()` resolves at hello, before the host has said whether voice is on. A
|
|
100
|
+
// reconnect can attach again meanwhile; wait for the latest answer.
|
|
101
|
+
let awaited = null;
|
|
102
|
+
while (this.attaching && this.attaching !== awaited) {
|
|
103
|
+
awaited = this.attaching;
|
|
104
|
+
await awaited;
|
|
105
|
+
}
|
|
106
|
+
if (pending.signal.aborted ||
|
|
107
|
+
this.disposed ||
|
|
108
|
+
!this.voiceSessionId ||
|
|
109
|
+
this.currentState === "unavailable" ||
|
|
110
|
+
this.currentState === "live") {
|
|
111
|
+
if (this.pendingJoin === pending)
|
|
112
|
+
this.pendingJoin = null;
|
|
113
|
+
if (pending.signal.aborted || this.currentState !== "live")
|
|
114
|
+
return "unavailable";
|
|
115
|
+
return "joined";
|
|
116
|
+
}
|
|
117
|
+
const sessionId = this.voiceSessionId;
|
|
118
|
+
const version = this.hostStateVersion;
|
|
119
|
+
this.setState("joining");
|
|
120
|
+
let status;
|
|
121
|
+
try {
|
|
122
|
+
({ status } = await hostRequest("voice.requestJoin", { sessionId }, { signal: pending.signal }));
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
status = "unavailable";
|
|
126
|
+
}
|
|
127
|
+
finally {
|
|
128
|
+
if (this.pendingJoin === pending)
|
|
129
|
+
this.pendingJoin = null;
|
|
130
|
+
}
|
|
131
|
+
if (pending.signal.aborted || !this.isCurrent(sessionId)) {
|
|
132
|
+
// Withdrawn, or the game connection ended, while the player was choosing. The
|
|
133
|
+
// abort already cancelled the request at the host, whose executor undoes a
|
|
134
|
+
// "yes" that lands anyway.
|
|
135
|
+
return "unavailable";
|
|
136
|
+
}
|
|
137
|
+
if (version === this.hostStateVersion) {
|
|
138
|
+
this.setState(status === "joined" ? "live" : status === "unavailable" ? "unavailable" : "off");
|
|
139
|
+
}
|
|
140
|
+
return status;
|
|
141
|
+
}
|
|
142
|
+
leave() {
|
|
143
|
+
if (this.disposed)
|
|
144
|
+
return;
|
|
145
|
+
const inVoice = this.currentState === "live" || this.currentState === "joining";
|
|
146
|
+
if (!inVoice && this.pendingJoin === null)
|
|
147
|
+
return;
|
|
148
|
+
this.withdraw();
|
|
149
|
+
if (inVoice) {
|
|
150
|
+
this.setState("off");
|
|
151
|
+
this.sendLeave(this.voiceSessionId);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
setMix(mix) {
|
|
155
|
+
if (this.disposed || !mix || typeof mix !== "object")
|
|
156
|
+
return;
|
|
157
|
+
const players = this.access.players();
|
|
158
|
+
for (const [playerId, entry] of Object.entries(mix)) {
|
|
159
|
+
const player = players.find((candidate) => candidate.playerId === playerId);
|
|
160
|
+
if (!player || !entry || typeof entry !== "object")
|
|
161
|
+
continue;
|
|
162
|
+
const next = { ...this.pendingMix[player.userId] };
|
|
163
|
+
if (typeof entry.gain === "number" && Number.isFinite(entry.gain)) {
|
|
164
|
+
next.gain = clamp(entry.gain, 0, 1);
|
|
165
|
+
}
|
|
166
|
+
if (typeof entry.pan === "number" && Number.isFinite(entry.pan)) {
|
|
167
|
+
next.pan = clamp(entry.pan, -1, 1);
|
|
168
|
+
}
|
|
169
|
+
this.pendingMix[player.userId] = next;
|
|
170
|
+
}
|
|
171
|
+
this.scheduleMix();
|
|
172
|
+
}
|
|
173
|
+
setListener(listener) {
|
|
174
|
+
if (this.disposed || !listener || typeof listener !== "object")
|
|
175
|
+
return;
|
|
176
|
+
const position = vector(listener.position);
|
|
177
|
+
const forward = vector(listener.forward);
|
|
178
|
+
const up = listener.up === undefined ? undefined : vector(listener.up);
|
|
179
|
+
// A zero direction faces nowhere (a player standing still, say): keep the last
|
|
180
|
+
// listener rather than send one the host would drop.
|
|
181
|
+
if (!position || !forward || up === null || isZero(forward) || (up && isZero(up)))
|
|
182
|
+
return;
|
|
183
|
+
this.pendingListener = up ? { position, forward, up } : { position, forward };
|
|
184
|
+
this.scheduleMix();
|
|
185
|
+
}
|
|
186
|
+
setPositions(positions) {
|
|
187
|
+
if (this.disposed || !positions || typeof positions !== "object")
|
|
188
|
+
return;
|
|
189
|
+
const players = this.access.players();
|
|
190
|
+
for (const [playerId, value] of Object.entries(positions)) {
|
|
191
|
+
const player = players.find((candidate) => candidate.playerId === playerId);
|
|
192
|
+
if (!player)
|
|
193
|
+
continue;
|
|
194
|
+
if (value === null) {
|
|
195
|
+
this.pendingPositions[player.userId] = null;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
const position = vector(value);
|
|
199
|
+
if (position)
|
|
200
|
+
this.pendingPositions[player.userId] = position;
|
|
201
|
+
}
|
|
202
|
+
this.scheduleMix();
|
|
203
|
+
}
|
|
204
|
+
setSpatialOptions(options) {
|
|
205
|
+
if (this.disposed || !options || typeof options !== "object")
|
|
206
|
+
return;
|
|
207
|
+
// Cumulative: the host fills every missing field with its default, so each send
|
|
208
|
+
// carries everything the deck has set so far.
|
|
209
|
+
this.pendingOptions = { ...this.sentOptions, ...this.pendingOptions, ...options };
|
|
210
|
+
this.scheduleMix();
|
|
211
|
+
}
|
|
212
|
+
onState(callback) {
|
|
213
|
+
this.states.add(callback);
|
|
214
|
+
callListener(callback, this.currentState);
|
|
215
|
+
return () => this.states.delete(callback);
|
|
216
|
+
}
|
|
217
|
+
onSpeaking(callback) {
|
|
218
|
+
this.speakers.add(callback);
|
|
219
|
+
callListener(callback, this.speaking.slice());
|
|
220
|
+
return () => this.speakers.delete(callback);
|
|
221
|
+
}
|
|
222
|
+
receive(event) {
|
|
223
|
+
if (this.disposed || !this.voiceSessionId || event.sessionId !== this.voiceSessionId) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (event.type === "state") {
|
|
227
|
+
this.hostStateVersion += 1;
|
|
228
|
+
this.setState(event.state);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
this.speakingUserIds = event.userIds.slice();
|
|
232
|
+
this.emitSpeaking();
|
|
233
|
+
}
|
|
234
|
+
// The roster changed: speakers are re-mapped, and mixes for players who left dropped.
|
|
235
|
+
rosterChanged() {
|
|
236
|
+
if (this.disposed)
|
|
237
|
+
return;
|
|
238
|
+
const userIds = new Set(this.access.players().map((player) => player.userId));
|
|
239
|
+
for (const userId of Object.keys(this.pendingMix)) {
|
|
240
|
+
if (!userIds.has(userId))
|
|
241
|
+
delete this.pendingMix[userId];
|
|
242
|
+
}
|
|
243
|
+
for (const userId of Object.keys(this.sentMix)) {
|
|
244
|
+
if (!userIds.has(userId))
|
|
245
|
+
delete this.sentMix[userId];
|
|
246
|
+
}
|
|
247
|
+
for (const userId of Object.keys(this.pendingPositions)) {
|
|
248
|
+
if (!userIds.has(userId))
|
|
249
|
+
delete this.pendingPositions[userId];
|
|
250
|
+
}
|
|
251
|
+
for (const userId of Object.keys(this.sentPositions)) {
|
|
252
|
+
if (!userIds.has(userId))
|
|
253
|
+
delete this.sentPositions[userId];
|
|
254
|
+
}
|
|
255
|
+
this.emitSpeaking();
|
|
256
|
+
}
|
|
257
|
+
// The game connection ended: voice for its session goes with it.
|
|
258
|
+
dispose() {
|
|
259
|
+
if (this.disposed)
|
|
260
|
+
return;
|
|
261
|
+
this.endSession();
|
|
262
|
+
this.setState("unavailable");
|
|
263
|
+
this.disposed = true;
|
|
264
|
+
if (this.mixTimer !== null)
|
|
265
|
+
clearTimeout(this.mixTimer);
|
|
266
|
+
this.mixTimer = null;
|
|
267
|
+
setActiveVoice(null, this);
|
|
268
|
+
}
|
|
269
|
+
isCurrent(sessionId) {
|
|
270
|
+
return !this.disposed && sessionId !== "" && sessionId === this.voiceSessionId;
|
|
271
|
+
}
|
|
272
|
+
// Cancels a pending request, so a retry starts a fresh prompt.
|
|
273
|
+
withdraw() {
|
|
274
|
+
this.pendingJoin?.abort();
|
|
275
|
+
this.pendingJoin = null;
|
|
276
|
+
this.joinRequest = null;
|
|
277
|
+
}
|
|
278
|
+
// Leaves the current voice session, if the player is in it or being asked.
|
|
279
|
+
endSession() {
|
|
280
|
+
const inVoice = this.currentState === "live" || this.currentState === "joining" || this.pendingJoin !== null;
|
|
281
|
+
this.withdraw();
|
|
282
|
+
if (inVoice && this.voiceSessionId)
|
|
283
|
+
this.sendLeave(this.voiceSessionId);
|
|
284
|
+
this.speakingUserIds = [];
|
|
285
|
+
this.pendingMix = {};
|
|
286
|
+
this.sentMix = {};
|
|
287
|
+
this.pendingListener = null;
|
|
288
|
+
this.pendingPositions = {};
|
|
289
|
+
this.pendingOptions = null;
|
|
290
|
+
this.sentListener = null;
|
|
291
|
+
this.sentPositions = {};
|
|
292
|
+
this.sentOptions = null;
|
|
293
|
+
this.emitSpeaking();
|
|
294
|
+
}
|
|
295
|
+
sendLeave(sessionId) {
|
|
296
|
+
void hostRequest("voice.leave", { sessionId }).catch(() => { });
|
|
297
|
+
}
|
|
298
|
+
setState(state) {
|
|
299
|
+
if (this.currentState === state)
|
|
300
|
+
return;
|
|
301
|
+
this.currentState = state;
|
|
302
|
+
if (state !== "live")
|
|
303
|
+
this.speakingUserIds = [];
|
|
304
|
+
this.emitSpeaking();
|
|
305
|
+
for (const callback of this.states)
|
|
306
|
+
callListener(callback, state);
|
|
307
|
+
if (state === "live") {
|
|
308
|
+
// A rejoined call starts from scratch at the host: send the deck's mix again.
|
|
309
|
+
for (const userId of Object.keys(this.sentMix)) {
|
|
310
|
+
this.pendingMix[userId] ??= {};
|
|
311
|
+
}
|
|
312
|
+
this.pendingListener ??= this.sentListener;
|
|
313
|
+
this.pendingOptions ??= this.sentOptions;
|
|
314
|
+
for (const [userId, position] of Object.entries(this.sentPositions)) {
|
|
315
|
+
if (!(userId in this.pendingPositions))
|
|
316
|
+
this.pendingPositions[userId] = position;
|
|
317
|
+
}
|
|
318
|
+
this.scheduleMix();
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
emitSpeaking() {
|
|
322
|
+
const userIds = new Set(this.speakingUserIds);
|
|
323
|
+
const playerIds = this.access
|
|
324
|
+
.players()
|
|
325
|
+
.filter((player) => userIds.has(player.userId))
|
|
326
|
+
.map((player) => player.playerId)
|
|
327
|
+
.sort();
|
|
328
|
+
if (playerIds.join("\n") === this.speaking.join("\n"))
|
|
329
|
+
return;
|
|
330
|
+
this.speaking = playerIds;
|
|
331
|
+
for (const callback of this.speakers)
|
|
332
|
+
callListener(callback, playerIds.slice());
|
|
333
|
+
}
|
|
334
|
+
hasPending() {
|
|
335
|
+
return (Object.keys(this.pendingMix).length > 0 ||
|
|
336
|
+
Object.keys(this.pendingPositions).length > 0 ||
|
|
337
|
+
this.pendingListener !== null ||
|
|
338
|
+
this.pendingOptions !== null);
|
|
339
|
+
}
|
|
340
|
+
scheduleMix() {
|
|
341
|
+
if (this.mixTimer !== null || !this.hasPending())
|
|
342
|
+
return;
|
|
343
|
+
const wait = Math.max(0, this.lastMixSentAt + VOICE_MIX_INTERVAL_MS - Date.now());
|
|
344
|
+
this.mixTimer = setTimeout(() => {
|
|
345
|
+
this.mixTimer = null;
|
|
346
|
+
this.flushMix();
|
|
347
|
+
}, wait);
|
|
348
|
+
}
|
|
349
|
+
// Held until the player is in voice, then sent: the latest mix is what matters. Each
|
|
350
|
+
// entry goes out whole, so a field the deck didn't name keeps its last value.
|
|
351
|
+
flushMix() {
|
|
352
|
+
if (this.disposed || !this.voiceSessionId || this.currentState !== "live")
|
|
353
|
+
return;
|
|
354
|
+
const userIds = new Set(this.access.players().map((player) => player.userId));
|
|
355
|
+
const mix = {};
|
|
356
|
+
for (const [userId, entry] of Object.entries(this.pendingMix)) {
|
|
357
|
+
if (!userIds.has(userId))
|
|
358
|
+
continue;
|
|
359
|
+
const previous = this.sentMix[userId] ?? DEFAULT_MIX;
|
|
360
|
+
const full = { gain: entry.gain ?? previous.gain, pan: entry.pan ?? previous.pan };
|
|
361
|
+
this.sentMix[userId] = full;
|
|
362
|
+
mix[userId] = full;
|
|
363
|
+
}
|
|
364
|
+
this.pendingMix = {};
|
|
365
|
+
const spatial = this.takeSpatial(userIds);
|
|
366
|
+
if (Object.keys(mix).length === 0 && !spatial)
|
|
367
|
+
return;
|
|
368
|
+
this.lastMixSentAt = Date.now();
|
|
369
|
+
if (Object.keys(mix).length > 0) {
|
|
370
|
+
void hostRequest("voice.setMix", { sessionId: this.voiceSessionId, mix }).catch(() => { });
|
|
371
|
+
}
|
|
372
|
+
if (spatial) {
|
|
373
|
+
void hostRequest("voice.setSpatial", { sessionId: this.voiceSessionId, ...spatial }).catch(() => { });
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
takeSpatial(userIds) {
|
|
377
|
+
const update = {};
|
|
378
|
+
if (this.pendingListener) {
|
|
379
|
+
update.listener = this.pendingListener;
|
|
380
|
+
this.sentListener = this.pendingListener;
|
|
381
|
+
}
|
|
382
|
+
if (this.pendingOptions) {
|
|
383
|
+
update.options = this.pendingOptions;
|
|
384
|
+
this.sentOptions = this.pendingOptions;
|
|
385
|
+
}
|
|
386
|
+
const positions = {};
|
|
387
|
+
for (const [userId, position] of Object.entries(this.pendingPositions)) {
|
|
388
|
+
if (!userIds.has(userId))
|
|
389
|
+
continue;
|
|
390
|
+
positions[userId] = position;
|
|
391
|
+
if (position)
|
|
392
|
+
this.sentPositions[userId] = position;
|
|
393
|
+
else
|
|
394
|
+
delete this.sentPositions[userId];
|
|
395
|
+
}
|
|
396
|
+
if (Object.keys(positions).length > 0)
|
|
397
|
+
update.positions = positions;
|
|
398
|
+
this.pendingListener = null;
|
|
399
|
+
this.pendingOptions = null;
|
|
400
|
+
this.pendingPositions = {};
|
|
401
|
+
return update.listener || update.options || update.positions ? update : null;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
function vector(value) {
|
|
405
|
+
return Array.isArray(value) &&
|
|
406
|
+
value.length === 3 &&
|
|
407
|
+
value.every((component) => typeof component === "number" && Number.isFinite(component))
|
|
408
|
+
? [value[0], value[1], value[2]]
|
|
409
|
+
: null;
|
|
410
|
+
}
|
|
411
|
+
function isZero(value) {
|
|
412
|
+
return value[0] === 0 && value[1] === 0 && value[2] === 0;
|
|
413
|
+
}
|
|
414
|
+
function clamp(value, min, max) {
|
|
415
|
+
return Math.min(max, Math.max(min, value));
|
|
416
|
+
}
|
|
417
|
+
function callListener(callback, value) {
|
|
418
|
+
try {
|
|
419
|
+
callback(value);
|
|
420
|
+
}
|
|
421
|
+
catch {
|
|
422
|
+
// One deck listener must not break voice or other listeners.
|
|
423
|
+
}
|
|
424
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type CommandName, type CommandParams, type CommandResult, type LifecycleEvent } from "./commands";
|
|
1
|
+
import { type CommandName, type CommandParams, type CommandResult, type LifecycleEvent, type VoiceEvent } from "./commands";
|
|
2
2
|
type PostChannel = "mobile" | "web";
|
|
3
3
|
export interface HostStreamOptions {
|
|
4
4
|
onStream?: (event: unknown) => void;
|
|
@@ -20,5 +20,6 @@ export declare function getCommandChannel(): PostChannel | "local";
|
|
|
20
20
|
export declare function hostNotify(event: LifecycleEvent): void;
|
|
21
21
|
export declare function postUnloadDone(unloadId: string, state?: string): void;
|
|
22
22
|
export declare function setUnloadHandler(handler: (unloadId: string) => void): void;
|
|
23
|
+
export declare function setVoiceEventHandler(handler: ((event: VoiceEvent) => void) | null): void;
|
|
23
24
|
export declare function installHostListener(): void;
|
|
24
25
|
export {};
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// A streaming command (agent.chat) gets stream envelopes on its requestId before
|
|
12
12
|
// its response. Each one goes to the caller's onStream and restarts the wait,
|
|
13
13
|
// and the caller's AbortSignal sends the host a cancel envelope.
|
|
14
|
-
import { CASTLE_SDK_PROTOCOL, COMMAND_TIMEOUTS_MS, STREAM_IDLE_TIMEOUT_MS, isResponseEnvelope, isStreamEnvelope, unloadRequestId, } from "./commands";
|
|
14
|
+
import { CASTLE_SDK_PROTOCOL, COMMAND_TIMEOUTS_MS, STREAM_IDLE_TIMEOUT_MS, isResponseEnvelope, isStreamEnvelope, unloadRequestId, voiceEventFrom, } from "./commands";
|
|
15
15
|
import { getCastleEmbed } from "./context";
|
|
16
16
|
import { CastleError } from "./errors";
|
|
17
17
|
import { cancelLocalCommand, releaseLocalCommand, sendLocalCommand } from "./runtime";
|
|
@@ -23,6 +23,8 @@ const INTERACTIVE_COMMANDS = new Set([
|
|
|
23
23
|
"pass.offer",
|
|
24
24
|
"snapshot.offer",
|
|
25
25
|
"portal.open",
|
|
26
|
+
// The host's voice prompt and the browser's microphone permission.
|
|
27
|
+
"voice.requestJoin",
|
|
26
28
|
]);
|
|
27
29
|
// Request ids carry a per-page prefix so that a host serving several decks at
|
|
28
30
|
// once can key a cancel by requestId alone.
|
|
@@ -31,6 +33,7 @@ let nextRequestId = 1;
|
|
|
31
33
|
const pending = new Map();
|
|
32
34
|
let listenersInstalled = false;
|
|
33
35
|
let unloadHandler = null;
|
|
36
|
+
let voiceEventHandler = null;
|
|
34
37
|
export async function hostRequest(command, params, stream = {}) {
|
|
35
38
|
try {
|
|
36
39
|
const env = await sendCommand(resolveChannel(), command, params, stream);
|
|
@@ -93,6 +96,12 @@ export function postUnloadDone(unloadId, state) {
|
|
|
93
96
|
export function setUnloadHandler(handler) {
|
|
94
97
|
unloadHandler = handler;
|
|
95
98
|
}
|
|
99
|
+
// Registered by multiplayerVoice.ts, the same way, for host voice events.
|
|
100
|
+
export function setVoiceEventHandler(handler) {
|
|
101
|
+
voiceEventHandler = handler;
|
|
102
|
+
if (handler)
|
|
103
|
+
installHostListener();
|
|
104
|
+
}
|
|
96
105
|
function resolveChannel() {
|
|
97
106
|
if (typeof window === "undefined")
|
|
98
107
|
return "local";
|
|
@@ -218,6 +227,11 @@ function receive(message) {
|
|
|
218
227
|
deliverStream(message.requestId, message.stream);
|
|
219
228
|
return;
|
|
220
229
|
}
|
|
230
|
+
const voiceEvent = voiceEventFrom(message);
|
|
231
|
+
if (voiceEvent) {
|
|
232
|
+
voiceEventHandler?.(voiceEvent);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
221
235
|
const unloadId = unloadRequestId(message);
|
|
222
236
|
if (unloadId !== null)
|
|
223
237
|
unloadHandler?.(unloadId);
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"imports": {
|
|
8
8
|
"castle.real-time": {
|
|
9
9
|
"deckId": "xFVr-afOLuUQ",
|
|
10
|
-
"version": "2026-09-
|
|
10
|
+
"version": "2026-09-25T17:53:47.749Z"
|
|
11
11
|
},
|
|
12
12
|
"castle.physics-2d": {
|
|
13
13
|
"deckId": "ckRZGFW4iPrx",
|
|
@@ -27,5 +27,5 @@
|
|
|
27
27
|
},
|
|
28
28
|
"deckId": "oiFu96LkwZ09",
|
|
29
29
|
"cardId": "nkJgrvrCby3H",
|
|
30
|
-
"publishedVersion": "2026-09-
|
|
30
|
+
"publishedVersion": "2026-09-25T17:54:26.340Z"
|
|
31
31
|
}
|
|
@@ -157,7 +157,13 @@ class MultiplayerSystem {
|
|
|
157
157
|
this.starting = true;
|
|
158
158
|
this.joinStartedAt ||= performance.now();
|
|
159
159
|
try {
|
|
160
|
-
this.net = await connectSession({
|
|
160
|
+
this.net = await connectSession({
|
|
161
|
+
onLog: (text) => console.log(`[multiplayer] ${text}`),
|
|
162
|
+
|
|
163
|
+
// A deck message can be handled on arrival through the `arrive` hook,
|
|
164
|
+
// which still runs in a hidden tab. State messages always wait for a frame.
|
|
165
|
+
onArrival: (msg) => msg.k !== STATE && msg.k !== WORLD && game.arrive?.(msg, this) === true,
|
|
166
|
+
});
|
|
161
167
|
} catch (err) {
|
|
162
168
|
console.log(`[multiplayer] could not join: ${err?.message ?? err}`);
|
|
163
169
|
this.starting = false;
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"imports": {
|
|
8
8
|
"castle.real-time": {
|
|
9
9
|
"deckId": "xFVr-afOLuUQ",
|
|
10
|
-
"version": "2026-09-
|
|
10
|
+
"version": "2026-09-25T17:53:47.749Z"
|
|
11
11
|
},
|
|
12
12
|
"castle.physics-3d": {
|
|
13
13
|
"deckId": "JH0SclbPVP0y",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"autoUpdateWhenImported": true,
|
|
27
27
|
"deckId": "ZwA_P_-VO-Qh",
|
|
28
28
|
"cardId": "bvRmCzyUpt_a",
|
|
29
|
-
"publishedVersion": "2026-09-
|
|
29
|
+
"publishedVersion": "2026-09-25T17:54:30.097Z",
|
|
30
30
|
"main": "main.jsx",
|
|
31
31
|
"server": {
|
|
32
32
|
"main": "code/server/index.js",
|
|
@@ -214,7 +214,13 @@ class MultiplayerSystem {
|
|
|
214
214
|
this.starting = true;
|
|
215
215
|
this.joinStartedAt ||= performance.now();
|
|
216
216
|
try {
|
|
217
|
-
this.net = await connectSession({
|
|
217
|
+
this.net = await connectSession({
|
|
218
|
+
onLog: (text) => console.log(`[multiplayer] ${text}`),
|
|
219
|
+
|
|
220
|
+
// A deck message can be handled on arrival through the `arrive` hook,
|
|
221
|
+
// which still runs in a hidden tab. State messages always wait for a frame.
|
|
222
|
+
onArrival: (msg) => msg.k !== STATE && msg.k !== WORLD && game.arrive?.(msg, this) === true,
|
|
223
|
+
});
|
|
218
224
|
} catch (err) {
|
|
219
225
|
console.log(`[multiplayer] could not join: ${err?.message ?? err}`);
|
|
220
226
|
this.starting = false;
|
|
@@ -598,9 +604,7 @@ class MultiplayerSystem {
|
|
|
598
604
|
const pose = body
|
|
599
605
|
? Poses.packPose(body.translation(), body.rotation())
|
|
600
606
|
: Poses.poseOf(transform);
|
|
601
|
-
const velocity = body
|
|
602
|
-
? Poses.packVelocity(body.linvel(), body.angvel())
|
|
603
|
-
: [0, 0, 0, 0, 0, 0];
|
|
607
|
+
const velocity = body ? Poses.packVelocity(body.linvel(), body.angvel()) : [0, 0, 0, 0, 0, 0];
|
|
604
608
|
owned.set(id, [id, ...pose, ...velocity, ...this.reportTail(id)]);
|
|
605
609
|
}
|
|
606
610
|
const releases = Ownership.toRelease(this.own, objects, me, Poses.REACH, now);
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"onUpdate": "scripts/onUpdate.mjs",
|
|
69
69
|
"title": "physics-2d",
|
|
70
70
|
"description": "Scene Editor. Works like classic Castle: actors, blueprints, behaviors and drawing tools, with physics built in.",
|
|
71
|
-
"publishedVersion": "2026-09-
|
|
71
|
+
"publishedVersion": "2026-09-25T17:51:55.024Z",
|
|
72
72
|
"imports": {
|
|
73
73
|
"castle.base": {
|
|
74
74
|
"deckId": "yRcmH4_aYllE",
|
|
@@ -1807,10 +1807,21 @@ function useScenePlayLoop({
|
|
|
1807
1807
|
}
|
|
1808
1808
|
raf = requestAnimationFrame(frame);
|
|
1809
1809
|
};
|
|
1810
|
+
// Edit mode draws one runtime built from the current `sceneData`. The effect
|
|
1811
|
+
// reruns when the scene, sprites or files change, and a behavior hot swap
|
|
1812
|
+
// drops it so the next frame builds it again.
|
|
1813
|
+
//
|
|
1814
|
+
// It is built inside a frame, so a broken behavior module throws into the
|
|
1815
|
+
// frame's catch and shows the redbox.
|
|
1816
|
+
let editScene = null;
|
|
1817
|
+
const unsubscribeBehaviors = subscribeBehaviors(() => {
|
|
1818
|
+
editScene = null;
|
|
1819
|
+
});
|
|
1810
1820
|
const drawFrame = (dt, frameViewport) => {
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1821
|
+
if (!isPlaying && !editScene) {
|
|
1822
|
+
editScene = makeScene(sceneData, behaviorClasses, sprites, files, path);
|
|
1823
|
+
}
|
|
1824
|
+
const scene = isPlaying ? runtimeRef.current : editScene;
|
|
1814
1825
|
if (scene) {
|
|
1815
1826
|
const snap = getSnapSettings(sceneData);
|
|
1816
1827
|
if (isPlaying) scene.update(dt);
|
|
@@ -1843,7 +1854,10 @@ function useScenePlayLoop({
|
|
|
1843
1854
|
// fixes the file resets this effect, and the stale redbox must go with it.
|
|
1844
1855
|
setLoopError(null);
|
|
1845
1856
|
raf = requestAnimationFrame(frame);
|
|
1846
|
-
return () =>
|
|
1857
|
+
return () => {
|
|
1858
|
+
cancelAnimationFrame(raf);
|
|
1859
|
+
unsubscribeBehaviors();
|
|
1860
|
+
};
|
|
1847
1861
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- `text` proxies `sceneData` identity; resetting on every parse churns the loop and wipes runtime state.
|
|
1848
1862
|
}, [
|
|
1849
1863
|
sprites,
|
|
@@ -20,11 +20,9 @@ import { initialMedia, mediaFilesOfKind } from '../engine/files';
|
|
|
20
20
|
import { reapUnusedMedia, stopAllMedia } from '../engine/media';
|
|
21
21
|
import { Sound, initSounds, stopAllSounds } from '../engine/sound';
|
|
22
22
|
|
|
23
|
-
// Preloading is the
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
// not a thing to do. Vite evaluates a module once, which is exactly the scope
|
|
27
|
-
// wanted.
|
|
23
|
+
// Preloading is the deck's, not a scene's, so it runs once at module load.
|
|
24
|
+
// The editor builds a new preview runtime on every scene edit; each would
|
|
25
|
+
// decode the deck's sounds again. Vite evaluates a module once.
|
|
28
26
|
initSounds(
|
|
29
27
|
Object.fromEntries(mediaFilesOfKind('audio').map((path) => [path, initialMedia[path]]))
|
|
30
28
|
);
|
|
@@ -54,12 +54,12 @@
|
|
|
54
54
|
},
|
|
55
55
|
"castle.base": {
|
|
56
56
|
"deckId": "yRcmH4_aYllE",
|
|
57
|
-
"version": "2026-09-
|
|
57
|
+
"version": "2026-09-24T21:25:28.679Z"
|
|
58
58
|
}
|
|
59
59
|
},
|
|
60
60
|
"main": "main.jsx",
|
|
61
61
|
"autoUpdateWhenImported": true,
|
|
62
62
|
"deckId": "JH0SclbPVP0y",
|
|
63
63
|
"cardId": "eXfAaUdbSU5A",
|
|
64
|
-
"publishedVersion": "2026-09-
|
|
64
|
+
"publishedVersion": "2026-09-25T17:46:00.903Z"
|
|
65
65
|
}
|
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
} from '@imports/castle.physics-2d/engine/ui';
|
|
20
20
|
import { useEditHistory, useHostUndoRedo } from '@imports/castle.physics-2d/editors/editorHistory';
|
|
21
21
|
import { useLiveDeckFiles3D, useStableSprites, fetchMissingFile } from './modelFiles';
|
|
22
|
-
import { parseModelCached, parseModel, serializeModel, STARTER_MODEL_TEXT } from './pxmodel';
|
|
22
|
+
import { makePart, parseModelCached, parseModel, serializeModel, STARTER_MODEL_TEXT } from './pxmodel';
|
|
23
23
|
import { boxMesh, extrudeFace, roundMeshInPlace } from './meshops';
|
|
24
24
|
import {
|
|
25
25
|
createModelEditorWorld,
|
|
@@ -171,7 +171,7 @@ export function PxModelEditor({ path }) {
|
|
|
171
171
|
part.height = 0.9;
|
|
172
172
|
}
|
|
173
173
|
if (kind === 'box') part.size = [0.6, 0.6, 0.6];
|
|
174
|
-
next.parts.push(part);
|
|
174
|
+
next.parts.push(makePart(part, next.parts.length));
|
|
175
175
|
});
|
|
176
176
|
if (id) setSelectedPartId(id);
|
|
177
177
|
};
|
|
@@ -72,6 +72,13 @@ function normalizePart(raw, index) {
|
|
|
72
72
|
return part;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
// A part built in editor code carries only the fields the editor set. Every
|
|
76
|
+
// other field, the vec3s included, comes from PART_DEFAULTS here; serializeModel
|
|
77
|
+
// reads all of them.
|
|
78
|
+
export function makePart(raw, index = 0) {
|
|
79
|
+
return normalizePart(raw, index);
|
|
80
|
+
}
|
|
81
|
+
|
|
75
82
|
// Parse model text into { name, parts } with defaults applied, or null when
|
|
76
83
|
// it isn't a model. Duplicate ids get suffixed; a parent ref that doesn't
|
|
77
84
|
// resolve (or cycles) is dropped to root rather than rejected.
|