@watchtower-sdk/core 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +137 -245
- package/dist/index.d.mts +148 -1
- package/dist/index.d.ts +148 -1
- package/dist/index.js +327 -0
- package/dist/index.mjs +326 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -206,6 +206,123 @@ declare class Room {
|
|
|
206
206
|
/** Check if connected */
|
|
207
207
|
get connected(): boolean;
|
|
208
208
|
}
|
|
209
|
+
interface SyncOptions {
|
|
210
|
+
/** Updates per second (default: 20) */
|
|
211
|
+
tickRate?: number;
|
|
212
|
+
/** Enable interpolation for remote entities (default: true) */
|
|
213
|
+
interpolate?: boolean;
|
|
214
|
+
}
|
|
215
|
+
interface JoinOptions {
|
|
216
|
+
/** Create room if it doesn't exist */
|
|
217
|
+
create?: boolean;
|
|
218
|
+
/** Max players (only on create) */
|
|
219
|
+
maxPlayers?: number;
|
|
220
|
+
/** Make room public/discoverable (only on create) */
|
|
221
|
+
public?: boolean;
|
|
222
|
+
/** Room metadata (only on create) */
|
|
223
|
+
metadata?: Record<string, unknown>;
|
|
224
|
+
}
|
|
225
|
+
interface RoomListing {
|
|
226
|
+
id: string;
|
|
227
|
+
players: number;
|
|
228
|
+
maxPlayers?: number;
|
|
229
|
+
metadata?: Record<string, unknown>;
|
|
230
|
+
createdAt: number;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Sync - Automatic state synchronization
|
|
234
|
+
*
|
|
235
|
+
* Point this at your game state object and it becomes multiplayer.
|
|
236
|
+
* No events, no callbacks - just read and write your state.
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* const state = { players: {} }
|
|
241
|
+
* const sync = wt.sync(state)
|
|
242
|
+
*
|
|
243
|
+
* await sync.join('my-room')
|
|
244
|
+
*
|
|
245
|
+
* // Add yourself
|
|
246
|
+
* state.players[sync.myId] = { x: 0, y: 0, name: 'Player1' }
|
|
247
|
+
*
|
|
248
|
+
* // Move (automatically syncs to others)
|
|
249
|
+
* state.players[sync.myId].x = 100
|
|
250
|
+
*
|
|
251
|
+
* // Others appear automatically in state.players!
|
|
252
|
+
* for (const [id, player] of Object.entries(state.players)) {
|
|
253
|
+
* draw(player.x, player.y)
|
|
254
|
+
* }
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
declare class Sync<T extends Record<string, unknown>> {
|
|
258
|
+
/** The synchronized state object */
|
|
259
|
+
readonly state: T;
|
|
260
|
+
/** Your player ID */
|
|
261
|
+
readonly myId: string;
|
|
262
|
+
/** Current room ID (null if not in a room) */
|
|
263
|
+
get roomId(): string | null;
|
|
264
|
+
/** Whether currently connected to a room */
|
|
265
|
+
get connected(): boolean;
|
|
266
|
+
private config;
|
|
267
|
+
private options;
|
|
268
|
+
private _roomId;
|
|
269
|
+
private ws;
|
|
270
|
+
private syncInterval;
|
|
271
|
+
private lastSentState;
|
|
272
|
+
private interpolationTargets;
|
|
273
|
+
private listeners;
|
|
274
|
+
constructor(state: T, config: Required<WatchtowerConfig>, options?: SyncOptions);
|
|
275
|
+
/**
|
|
276
|
+
* Join a room - your state will sync with everyone in this room
|
|
277
|
+
*
|
|
278
|
+
* @param roomId - Room identifier (any string)
|
|
279
|
+
* @param options - Join options
|
|
280
|
+
*/
|
|
281
|
+
join(roomId: string, options?: JoinOptions): Promise<void>;
|
|
282
|
+
/**
|
|
283
|
+
* Leave the current room
|
|
284
|
+
*/
|
|
285
|
+
leave(): Promise<void>;
|
|
286
|
+
/**
|
|
287
|
+
* Send a one-off message to all players in the room
|
|
288
|
+
*
|
|
289
|
+
* @param data - Any JSON-serializable data
|
|
290
|
+
*/
|
|
291
|
+
broadcast(data: unknown): void;
|
|
292
|
+
/**
|
|
293
|
+
* Create a new room and join it
|
|
294
|
+
*
|
|
295
|
+
* @param options - Room creation options
|
|
296
|
+
* @returns The room code/ID
|
|
297
|
+
*/
|
|
298
|
+
create(options?: Omit<JoinOptions, 'create'>): Promise<string>;
|
|
299
|
+
/**
|
|
300
|
+
* List public rooms
|
|
301
|
+
*/
|
|
302
|
+
listRooms(): Promise<RoomListing[]>;
|
|
303
|
+
/**
|
|
304
|
+
* Subscribe to sync events
|
|
305
|
+
*/
|
|
306
|
+
on(event: 'join' | 'leave' | 'error' | 'connected' | 'disconnected' | 'message', callback: Function): void;
|
|
307
|
+
/**
|
|
308
|
+
* Unsubscribe from sync events
|
|
309
|
+
*/
|
|
310
|
+
off(event: string, callback: Function): void;
|
|
311
|
+
private emit;
|
|
312
|
+
private connectWebSocket;
|
|
313
|
+
private handleMessage;
|
|
314
|
+
private applyFullState;
|
|
315
|
+
private applyPlayerState;
|
|
316
|
+
private removePlayer;
|
|
317
|
+
private clearRemotePlayers;
|
|
318
|
+
private findPlayersKey;
|
|
319
|
+
private startSyncLoop;
|
|
320
|
+
private stopSyncLoop;
|
|
321
|
+
private syncMyState;
|
|
322
|
+
private updateInterpolation;
|
|
323
|
+
private generateRoomCode;
|
|
324
|
+
private getHeaders;
|
|
325
|
+
}
|
|
209
326
|
declare class Watchtower {
|
|
210
327
|
/** @internal - Config is non-enumerable to prevent accidental API key exposure */
|
|
211
328
|
private readonly config;
|
|
@@ -291,6 +408,36 @@ declare class Watchtower {
|
|
|
291
408
|
* Note: This returns a promise, use `await wt.stats` or `wt.getStats()`
|
|
292
409
|
*/
|
|
293
410
|
get stats(): Promise<GameStats>;
|
|
411
|
+
/**
|
|
412
|
+
* Create a synchronized state object
|
|
413
|
+
*
|
|
414
|
+
* Point this at your game state and it becomes multiplayer.
|
|
415
|
+
* No events, no callbacks - just read and write your state.
|
|
416
|
+
*
|
|
417
|
+
* @param state - Your game state object (e.g., { players: {} })
|
|
418
|
+
* @param options - Sync options (tickRate, interpolation)
|
|
419
|
+
* @returns A Sync instance
|
|
420
|
+
*
|
|
421
|
+
* @example
|
|
422
|
+
* ```ts
|
|
423
|
+
* const state = { players: {} }
|
|
424
|
+
* const sync = wt.sync(state)
|
|
425
|
+
*
|
|
426
|
+
* await sync.join('my-room')
|
|
427
|
+
*
|
|
428
|
+
* // Add yourself
|
|
429
|
+
* state.players[sync.myId] = { x: 0, y: 0 }
|
|
430
|
+
*
|
|
431
|
+
* // Move (automatically syncs to others)
|
|
432
|
+
* state.players[sync.myId].x = 100
|
|
433
|
+
*
|
|
434
|
+
* // Others appear automatically in state.players!
|
|
435
|
+
* for (const [id, player] of Object.entries(state.players)) {
|
|
436
|
+
* draw(player.x, player.y)
|
|
437
|
+
* }
|
|
438
|
+
* ```
|
|
439
|
+
*/
|
|
440
|
+
sync<T extends Record<string, unknown>>(state: T, options?: SyncOptions): Sync<T>;
|
|
294
441
|
}
|
|
295
442
|
|
|
296
|
-
export { type GameState, type GameStats, type PlayerInfo, type PlayerState, type PlayerStats, type PlayersState, Room, type RoomEventMap, type RoomInfo, type SaveData, Watchtower, type WatchtowerConfig, Watchtower as default };
|
|
443
|
+
export { type GameState, type GameStats, type JoinOptions, type PlayerInfo, type PlayerState, type PlayerStats, type PlayersState, Room, type RoomEventMap, type RoomInfo, type RoomListing, type SaveData, Sync, type SyncOptions, Watchtower, type WatchtowerConfig, Watchtower as default };
|
package/dist/index.js
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
Room: () => Room,
|
|
24
|
+
Sync: () => Sync,
|
|
24
25
|
Watchtower: () => Watchtower,
|
|
25
26
|
default: () => index_default
|
|
26
27
|
});
|
|
@@ -282,6 +283,298 @@ var Room = class {
|
|
|
282
283
|
return this.ws?.readyState === WebSocket.OPEN;
|
|
283
284
|
}
|
|
284
285
|
};
|
|
286
|
+
var Sync = class {
|
|
287
|
+
constructor(state, config, options) {
|
|
288
|
+
this._roomId = null;
|
|
289
|
+
this.ws = null;
|
|
290
|
+
this.syncInterval = null;
|
|
291
|
+
this.lastSentState = "";
|
|
292
|
+
this.interpolationTargets = /* @__PURE__ */ new Map();
|
|
293
|
+
this.listeners = /* @__PURE__ */ new Map();
|
|
294
|
+
this.state = state;
|
|
295
|
+
this.myId = config.playerId;
|
|
296
|
+
this.config = config;
|
|
297
|
+
this.options = {
|
|
298
|
+
tickRate: options?.tickRate ?? 20,
|
|
299
|
+
interpolate: options?.interpolate ?? true
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
/** Current room ID (null if not in a room) */
|
|
303
|
+
get roomId() {
|
|
304
|
+
return this._roomId;
|
|
305
|
+
}
|
|
306
|
+
/** Whether currently connected to a room */
|
|
307
|
+
get connected() {
|
|
308
|
+
return this.ws?.readyState === WebSocket.OPEN;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Join a room - your state will sync with everyone in this room
|
|
312
|
+
*
|
|
313
|
+
* @param roomId - Room identifier (any string)
|
|
314
|
+
* @param options - Join options
|
|
315
|
+
*/
|
|
316
|
+
async join(roomId, options) {
|
|
317
|
+
if (this._roomId) {
|
|
318
|
+
await this.leave();
|
|
319
|
+
}
|
|
320
|
+
this._roomId = roomId;
|
|
321
|
+
await this.connectWebSocket(roomId, options);
|
|
322
|
+
this.startSyncLoop();
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Leave the current room
|
|
326
|
+
*/
|
|
327
|
+
async leave() {
|
|
328
|
+
this.stopSyncLoop();
|
|
329
|
+
if (this.ws) {
|
|
330
|
+
this.ws.close();
|
|
331
|
+
this.ws = null;
|
|
332
|
+
}
|
|
333
|
+
this.clearRemotePlayers();
|
|
334
|
+
this._roomId = null;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Send a one-off message to all players in the room
|
|
338
|
+
*
|
|
339
|
+
* @param data - Any JSON-serializable data
|
|
340
|
+
*/
|
|
341
|
+
broadcast(data) {
|
|
342
|
+
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
343
|
+
this.ws.send(JSON.stringify({ type: "broadcast", data }));
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Create a new room and join it
|
|
348
|
+
*
|
|
349
|
+
* @param options - Room creation options
|
|
350
|
+
* @returns The room code/ID
|
|
351
|
+
*/
|
|
352
|
+
async create(options) {
|
|
353
|
+
const code = this.generateRoomCode();
|
|
354
|
+
await this.join(code, { ...options, create: true });
|
|
355
|
+
return code;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* List public rooms
|
|
359
|
+
*/
|
|
360
|
+
async listRooms() {
|
|
361
|
+
const response = await fetch(`${this.config.apiUrl}/v1/sync/rooms?gameId=${this.config.gameId}`, {
|
|
362
|
+
headers: this.getHeaders()
|
|
363
|
+
});
|
|
364
|
+
if (!response.ok) {
|
|
365
|
+
const data2 = await response.json();
|
|
366
|
+
throw new Error(data2.error || "Failed to list rooms");
|
|
367
|
+
}
|
|
368
|
+
const data = await response.json();
|
|
369
|
+
return data.rooms || [];
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Subscribe to sync events
|
|
373
|
+
*/
|
|
374
|
+
on(event, callback) {
|
|
375
|
+
if (!this.listeners.has(event)) {
|
|
376
|
+
this.listeners.set(event, /* @__PURE__ */ new Set());
|
|
377
|
+
}
|
|
378
|
+
this.listeners.get(event).add(callback);
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Unsubscribe from sync events
|
|
382
|
+
*/
|
|
383
|
+
off(event, callback) {
|
|
384
|
+
this.listeners.get(event)?.delete(callback);
|
|
385
|
+
}
|
|
386
|
+
emit(event, ...args) {
|
|
387
|
+
this.listeners.get(event)?.forEach((cb) => {
|
|
388
|
+
try {
|
|
389
|
+
cb(...args);
|
|
390
|
+
} catch (e) {
|
|
391
|
+
console.error(`Error in sync ${event} handler:`, e);
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
async connectWebSocket(roomId, options) {
|
|
396
|
+
return new Promise((resolve, reject) => {
|
|
397
|
+
const wsUrl = this.config.apiUrl.replace("https://", "wss://").replace("http://", "ws://");
|
|
398
|
+
const params = new URLSearchParams({
|
|
399
|
+
playerId: this.config.playerId,
|
|
400
|
+
gameId: this.config.gameId,
|
|
401
|
+
...this.config.apiKey ? { apiKey: this.config.apiKey } : {},
|
|
402
|
+
...options?.create ? { create: "true" } : {},
|
|
403
|
+
...options?.maxPlayers ? { maxPlayers: String(options.maxPlayers) } : {},
|
|
404
|
+
...options?.public ? { public: "true" } : {},
|
|
405
|
+
...options?.metadata ? { metadata: JSON.stringify(options.metadata) } : {}
|
|
406
|
+
});
|
|
407
|
+
const url = `${wsUrl}/v1/sync/${roomId}/ws?${params}`;
|
|
408
|
+
this.ws = new WebSocket(url);
|
|
409
|
+
const timeout = setTimeout(() => {
|
|
410
|
+
reject(new Error("Connection timeout"));
|
|
411
|
+
this.ws?.close();
|
|
412
|
+
}, 1e4);
|
|
413
|
+
this.ws.onopen = () => {
|
|
414
|
+
clearTimeout(timeout);
|
|
415
|
+
this.emit("connected");
|
|
416
|
+
resolve();
|
|
417
|
+
};
|
|
418
|
+
this.ws.onerror = () => {
|
|
419
|
+
clearTimeout(timeout);
|
|
420
|
+
const error = new Error("WebSocket connection failed");
|
|
421
|
+
this.emit("error", error);
|
|
422
|
+
reject(error);
|
|
423
|
+
};
|
|
424
|
+
this.ws.onclose = () => {
|
|
425
|
+
this.stopSyncLoop();
|
|
426
|
+
this.emit("disconnected");
|
|
427
|
+
};
|
|
428
|
+
this.ws.onmessage = (event) => {
|
|
429
|
+
try {
|
|
430
|
+
const data = JSON.parse(event.data);
|
|
431
|
+
this.handleMessage(data);
|
|
432
|
+
} catch (e) {
|
|
433
|
+
console.error("Failed to parse sync message:", e);
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
handleMessage(data) {
|
|
439
|
+
switch (data.type) {
|
|
440
|
+
case "full_state":
|
|
441
|
+
this.applyFullState(data.state);
|
|
442
|
+
break;
|
|
443
|
+
case "state":
|
|
444
|
+
this.applyPlayerState(data.playerId, data.data);
|
|
445
|
+
break;
|
|
446
|
+
case "join":
|
|
447
|
+
this.emit("join", data.playerId);
|
|
448
|
+
break;
|
|
449
|
+
case "leave":
|
|
450
|
+
this.removePlayer(data.playerId);
|
|
451
|
+
this.emit("leave", data.playerId);
|
|
452
|
+
break;
|
|
453
|
+
case "message":
|
|
454
|
+
this.emit("message", data.from, data.data);
|
|
455
|
+
break;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
applyFullState(fullState) {
|
|
459
|
+
for (const [playerId, playerState] of Object.entries(fullState)) {
|
|
460
|
+
if (playerId !== this.myId) {
|
|
461
|
+
this.applyPlayerState(playerId, playerState);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
applyPlayerState(playerId, playerState) {
|
|
466
|
+
const playersKey = this.findPlayersKey();
|
|
467
|
+
if (!playersKey) return;
|
|
468
|
+
const players = this.state[playersKey];
|
|
469
|
+
if (this.options.interpolate && players[playerId]) {
|
|
470
|
+
this.interpolationTargets.set(playerId, {
|
|
471
|
+
target: { ...playerState },
|
|
472
|
+
current: { ...players[playerId] }
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
players[playerId] = playerState;
|
|
476
|
+
}
|
|
477
|
+
removePlayer(playerId) {
|
|
478
|
+
const playersKey = this.findPlayersKey();
|
|
479
|
+
if (!playersKey) return;
|
|
480
|
+
const players = this.state[playersKey];
|
|
481
|
+
delete players[playerId];
|
|
482
|
+
this.interpolationTargets.delete(playerId);
|
|
483
|
+
}
|
|
484
|
+
clearRemotePlayers() {
|
|
485
|
+
const playersKey = this.findPlayersKey();
|
|
486
|
+
if (!playersKey) return;
|
|
487
|
+
const players = this.state[playersKey];
|
|
488
|
+
for (const playerId of Object.keys(players)) {
|
|
489
|
+
if (playerId !== this.myId) {
|
|
490
|
+
delete players[playerId];
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
this.interpolationTargets.clear();
|
|
494
|
+
}
|
|
495
|
+
findPlayersKey() {
|
|
496
|
+
const candidates = ["players", "entities", "gnomes", "users", "clients"];
|
|
497
|
+
for (const key of candidates) {
|
|
498
|
+
if (key in this.state && typeof this.state[key] === "object") {
|
|
499
|
+
return key;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
for (const key of Object.keys(this.state)) {
|
|
503
|
+
if (typeof this.state[key] === "object" && this.state[key] !== null) {
|
|
504
|
+
return key;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
startSyncLoop() {
|
|
510
|
+
if (this.syncInterval) return;
|
|
511
|
+
const intervalMs = 1e3 / this.options.tickRate;
|
|
512
|
+
this.syncInterval = setInterval(() => {
|
|
513
|
+
this.syncMyState();
|
|
514
|
+
if (this.options.interpolate) {
|
|
515
|
+
this.updateInterpolation();
|
|
516
|
+
}
|
|
517
|
+
}, intervalMs);
|
|
518
|
+
}
|
|
519
|
+
stopSyncLoop() {
|
|
520
|
+
if (this.syncInterval) {
|
|
521
|
+
clearInterval(this.syncInterval);
|
|
522
|
+
this.syncInterval = null;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
syncMyState() {
|
|
526
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return;
|
|
527
|
+
const playersKey = this.findPlayersKey();
|
|
528
|
+
if (!playersKey) return;
|
|
529
|
+
const players = this.state[playersKey];
|
|
530
|
+
const myState = players[this.myId];
|
|
531
|
+
if (!myState) return;
|
|
532
|
+
const stateJson = JSON.stringify(myState);
|
|
533
|
+
if (stateJson === this.lastSentState) return;
|
|
534
|
+
this.lastSentState = stateJson;
|
|
535
|
+
this.ws.send(JSON.stringify({
|
|
536
|
+
type: "state",
|
|
537
|
+
data: myState
|
|
538
|
+
}));
|
|
539
|
+
}
|
|
540
|
+
updateInterpolation() {
|
|
541
|
+
const playersKey = this.findPlayersKey();
|
|
542
|
+
if (!playersKey) return;
|
|
543
|
+
const players = this.state[playersKey];
|
|
544
|
+
const lerpFactor = 0.2;
|
|
545
|
+
for (const [playerId, interp] of this.interpolationTargets) {
|
|
546
|
+
const player = players[playerId];
|
|
547
|
+
if (!player) continue;
|
|
548
|
+
for (const [key, targetValue] of Object.entries(interp.target)) {
|
|
549
|
+
if (typeof targetValue === "number" && typeof interp.current[key] === "number") {
|
|
550
|
+
const current = interp.current[key];
|
|
551
|
+
const newValue = current + (targetValue - current) * lerpFactor;
|
|
552
|
+
interp.current[key] = newValue;
|
|
553
|
+
player[key] = newValue;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
generateRoomCode() {
|
|
559
|
+
const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
|
560
|
+
let code = "";
|
|
561
|
+
for (let i = 0; i < 6; i++) {
|
|
562
|
+
code += chars[Math.floor(Math.random() * chars.length)];
|
|
563
|
+
}
|
|
564
|
+
return code;
|
|
565
|
+
}
|
|
566
|
+
getHeaders() {
|
|
567
|
+
const headers = {
|
|
568
|
+
"Content-Type": "application/json",
|
|
569
|
+
"X-Player-ID": this.config.playerId,
|
|
570
|
+
"X-Game-ID": this.config.gameId
|
|
571
|
+
};
|
|
572
|
+
if (this.config.apiKey) {
|
|
573
|
+
headers["Authorization"] = `Bearer ${this.config.apiKey}`;
|
|
574
|
+
}
|
|
575
|
+
return headers;
|
|
576
|
+
}
|
|
577
|
+
};
|
|
285
578
|
var Watchtower = class {
|
|
286
579
|
constructor(config) {
|
|
287
580
|
Object.defineProperty(this, "config", {
|
|
@@ -458,10 +751,44 @@ var Watchtower = class {
|
|
|
458
751
|
get stats() {
|
|
459
752
|
return this.getStats();
|
|
460
753
|
}
|
|
754
|
+
// ============ SYNC API ============
|
|
755
|
+
/**
|
|
756
|
+
* Create a synchronized state object
|
|
757
|
+
*
|
|
758
|
+
* Point this at your game state and it becomes multiplayer.
|
|
759
|
+
* No events, no callbacks - just read and write your state.
|
|
760
|
+
*
|
|
761
|
+
* @param state - Your game state object (e.g., { players: {} })
|
|
762
|
+
* @param options - Sync options (tickRate, interpolation)
|
|
763
|
+
* @returns A Sync instance
|
|
764
|
+
*
|
|
765
|
+
* @example
|
|
766
|
+
* ```ts
|
|
767
|
+
* const state = { players: {} }
|
|
768
|
+
* const sync = wt.sync(state)
|
|
769
|
+
*
|
|
770
|
+
* await sync.join('my-room')
|
|
771
|
+
*
|
|
772
|
+
* // Add yourself
|
|
773
|
+
* state.players[sync.myId] = { x: 0, y: 0 }
|
|
774
|
+
*
|
|
775
|
+
* // Move (automatically syncs to others)
|
|
776
|
+
* state.players[sync.myId].x = 100
|
|
777
|
+
*
|
|
778
|
+
* // Others appear automatically in state.players!
|
|
779
|
+
* for (const [id, player] of Object.entries(state.players)) {
|
|
780
|
+
* draw(player.x, player.y)
|
|
781
|
+
* }
|
|
782
|
+
* ```
|
|
783
|
+
*/
|
|
784
|
+
sync(state, options) {
|
|
785
|
+
return new Sync(state, this.config, options);
|
|
786
|
+
}
|
|
461
787
|
};
|
|
462
788
|
var index_default = Watchtower;
|
|
463
789
|
// Annotate the CommonJS export names for ESM import in node:
|
|
464
790
|
0 && (module.exports = {
|
|
465
791
|
Room,
|
|
792
|
+
Sync,
|
|
466
793
|
Watchtower
|
|
467
794
|
});
|