feat: add client entry point, boot scene, and socket client
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { io, Socket } from 'socket.io-client';
|
||||
import type { ServerEvents, ClientEvents, WorldState, StateUpdate, PlayerJoined, PlayerLeft, PlayerInput } from '@dflike/shared';
|
||||
|
||||
type TypedSocket = Socket<ServerEvents, ClientEvents>;
|
||||
|
||||
export class SocketClient {
|
||||
private socket: TypedSocket;
|
||||
private _playerId: string | null = null;
|
||||
private _entityId: number | null = null;
|
||||
|
||||
// Event callbacks
|
||||
onWorldState: ((data: WorldState) => void) | null = null;
|
||||
onStateUpdate: ((data: StateUpdate) => void) | null = null;
|
||||
onPlayerJoined: ((data: PlayerJoined) => void) | null = null;
|
||||
onPlayerLeft: ((data: PlayerLeft) => void) | null = null;
|
||||
|
||||
constructor(url: string) {
|
||||
this.socket = io(url);
|
||||
|
||||
this.socket.on('world-state', (data) => {
|
||||
this.onWorldState?.(data);
|
||||
});
|
||||
|
||||
this.socket.on('state-update', (data) => {
|
||||
this.onStateUpdate?.(data);
|
||||
});
|
||||
|
||||
this.socket.on('player-joined', (data) => {
|
||||
if (!this._playerId) {
|
||||
this._playerId = data.playerId;
|
||||
this._entityId = data.entityId;
|
||||
}
|
||||
this.onPlayerJoined?.(data);
|
||||
});
|
||||
|
||||
this.socket.on('player-left', (data) => {
|
||||
this.onPlayerLeft?.(data);
|
||||
});
|
||||
}
|
||||
|
||||
get playerId(): string | null { return this._playerId; }
|
||||
get entityId(): number | null { return this._entityId; }
|
||||
|
||||
sendInput(input: PlayerInput): void {
|
||||
this.socket.emit('player-input', input);
|
||||
}
|
||||
|
||||
disconnect(): void {
|
||||
this.socket.disconnect();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user