20 lines
579 B
TypeScript
20 lines
579 B
TypeScript
|
|
import type { GameAction } from './types'
|
||
|
|
import { stateManager } from './StateManager'
|
||
|
|
|
||
|
|
/** All state mutations go through an adapter.
|
||
|
|
* Swap LocalAdapter for WebSocketAdapter to enable multiplayer. */
|
||
|
|
export interface NetworkAdapter {
|
||
|
|
send(action: GameAction): void
|
||
|
|
onAction?: (action: GameAction) => void
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Singleplayer: apply actions immediately and synchronously */
|
||
|
|
export class LocalAdapter implements NetworkAdapter {
|
||
|
|
onAction?: (action: GameAction) => void
|
||
|
|
|
||
|
|
send(action: GameAction): void {
|
||
|
|
stateManager.apply(action)
|
||
|
|
this.onAction?.(action)
|
||
|
|
}
|
||
|
|
}
|