boardgame-core/src/core/game.ts

27 lines
1.0 KiB
TypeScript
Raw Normal View History

2026-04-02 10:26:42 +08:00
import {createEntityCollection} from "../utils/entity";
import {Part} from "./part";
import {Region} from "./region";
import {CommandRegistry, CommandRunnerContextExport, createCommandRunnerContext, PromptEvent} from "../utils/command";
import {AsyncQueue} from "../utils/async-queue";
export interface IGameContext {
parts: ReturnType<typeof createEntityCollection<Part>>;
regions: ReturnType<typeof createEntityCollection<Region>>;
commands: CommandRunnerContextExport<IGameContext>;
inputs: AsyncQueue<PromptEvent>;
}
export function createGameContext(commandRegistry: CommandRegistry<IGameContext>) {
const parts = createEntityCollection<Part>();
const regions = createEntityCollection<Region>();
const ctx: IGameContext = {
parts,
regions,
commands: null,
inputs: new AsyncQueue(),
};
ctx.commands = createCommandRunnerContext(commandRegistry, ctx);
ctx.commands.on('prompt', (prompt: PromptEvent) => ctx.inputs.push(prompt));
return ctx;
}