27 lines
1.0 KiB
TypeScript
27 lines
1.0 KiB
TypeScript
|
|
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;
|
||
|
|
}
|