chore: add GameModule type

This commit is contained in:
hypercross 2026-04-04 11:06:41 +08:00
parent be4ff7ae08
commit ae98e0735a
3 changed files with 16 additions and 7 deletions

View File

@ -9,6 +9,11 @@ export interface GameHostOptions {
autoStart?: boolean; autoStart?: boolean;
} }
export interface GameModule<TState extends Record<string, unknown>> {
registry: CommandRegistry<MutableSignal<TState>>;
createInitialState: () => TState;
}
export class GameHost<TState extends Record<string, unknown>> { export class GameHost<TState extends Record<string, unknown>> {
readonly state: ReadonlySignal<TState>; readonly state: ReadonlySignal<TState>;
readonly commands: ReturnType<typeof createGameContext<TState>>['commands']; readonly commands: ReturnType<typeof createGameContext<TState>>['commands'];
@ -142,10 +147,7 @@ export class GameHost<TState extends Record<string, unknown>> {
} }
export function createGameHost<TState extends Record<string, unknown>>( export function createGameHost<TState extends Record<string, unknown>>(
module: { module: GameModule<TState>,
registry: CommandRegistry<MutableSignal<TState>>;
createInitialState: () => TState;
},
setupCommand: string, setupCommand: string,
options?: GameHostOptions options?: GameHostOptions
): GameHost<TState> { ): GameHost<TState> {

View File

@ -10,6 +10,7 @@ import {
parseCommandSchema, parseCommandSchema,
registerCommand registerCommand
} from "@/utils/command"; } from "@/utils/command";
import type { GameModule } from './game-host';
export interface IGameContext<TState extends Record<string, unknown> = {} > { export interface IGameContext<TState extends Record<string, unknown> = {} > {
state: MutableSignal<TState>; state: MutableSignal<TState>;
@ -69,4 +70,10 @@ export function createGameCommand<TState extends Record<string, unknown> = {} ,
} }
export { GameHost, createGameHost } from './game-host'; export { GameHost, createGameHost } from './game-host';
export type { GameHostStatus, GameHostOptions } from './game-host'; export type { GameHostStatus, GameHostOptions, GameModule } from './game-host';
export function createGameModule<TState extends Record<string, unknown>>(
module: GameModule<TState>
): GameModule<TState> {
return module;
}

View File

@ -7,8 +7,8 @@
export type { IGameContext } from './core/game'; export type { IGameContext } from './core/game';
export { createGameContext, createGameCommandRegistry } from './core/game'; export { createGameContext, createGameCommandRegistry } from './core/game';
export type { GameHost, GameHostStatus, GameHostOptions } from './core/game-host'; export type { GameHost, GameHostStatus, GameHostOptions, GameModule } from './core/game';
export { createGameHost } from './core/game-host'; export { createGameHost, createGameModule } from './core/game';
export type { Part } from './core/part'; export type { Part } from './core/part';
export { flip, flipTo, roll, findPartById, isCellOccupied, getPartAtPosition, isCellOccupiedByRegion, getPartAtPositionInRegion } from './core/part'; export { flip, flipTo, roll, findPartById, isCellOccupied, getPartAtPosition, isCellOccupiedByRegion, getPartAtPositionInRegion } from './core/part';