refactor: simplify GameHost constructor to use GameModule
Update `GameHost` to accept a `GameModule` directly instead of individual parameters. This simplifies the `createGameHost` factory and improves type safety by preserving the module type. Also apply `readonly` modifiers to `ContentModule` in the slay-the-spire-like sample.
This commit is contained in:
parent
e8c995f74f
commit
23ac09ff21
|
|
@ -12,6 +12,7 @@ export type GameHostStatus = "created" | "running" | "disposed";
|
||||||
export class GameHost<
|
export class GameHost<
|
||||||
TState extends Record<string, unknown>,
|
TState extends Record<string, unknown>,
|
||||||
TResult = unknown,
|
TResult = unknown,
|
||||||
|
TModule extends GameModule<TState, TResult> = GameModule<TState, TResult>,
|
||||||
> {
|
> {
|
||||||
readonly state: ReadonlySignal<TState>;
|
readonly state: ReadonlySignal<TState>;
|
||||||
readonly status: ReadonlySignal<GameHostStatus>;
|
readonly status: ReadonlySignal<GameHostStatus>;
|
||||||
|
|
@ -29,16 +30,16 @@ export class GameHost<
|
||||||
private _eventListeners: Map<"start" | "dispose", Set<() => void>>;
|
private _eventListeners: Map<"start" | "dispose", Set<() => void>>;
|
||||||
private _isDisposed = false;
|
private _isDisposed = false;
|
||||||
|
|
||||||
constructor(
|
constructor(public readonly gameModule: TModule) {
|
||||||
registry: CommandRegistry<IGameContext<TState>>,
|
const { createInitialState, registry, start } = gameModule;
|
||||||
createInitialState: () => TState,
|
|
||||||
start: (ctx: IGameContext<TState>) => Promise<TResult>,
|
|
||||||
) {
|
|
||||||
this._createInitialState = createInitialState;
|
this._createInitialState = createInitialState;
|
||||||
this._eventListeners = new Map();
|
this._eventListeners = new Map();
|
||||||
|
|
||||||
const initialState = createInitialState();
|
const initialState = createInitialState();
|
||||||
this._context = createGameContext(registry, initialState);
|
this._context = createGameContext(
|
||||||
|
registry ?? createGameCommandRegistry(),
|
||||||
|
initialState,
|
||||||
|
);
|
||||||
this._start = start;
|
this._start = start;
|
||||||
this.state = this._context._state;
|
this.state = this._context._state;
|
||||||
|
|
||||||
|
|
@ -179,10 +180,7 @@ export type GameModule<
|
||||||
export function createGameHost<
|
export function createGameHost<
|
||||||
TState extends Record<string, unknown>,
|
TState extends Record<string, unknown>,
|
||||||
TResult = unknown,
|
TResult = unknown,
|
||||||
>(gameModule: GameModule<TState, TResult>): GameHost<TState, TResult> {
|
TModule extends GameModule<TState, TResult> = GameModule<TState, TResult>,
|
||||||
return new GameHost(
|
>(gameModule: TModule): GameHost<TState, TResult> {
|
||||||
gameModule.registry || createGameCommandRegistry(),
|
return new GameHost(gameModule);
|
||||||
gameModule.createInitialState,
|
|
||||||
gameModule.start,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,14 @@ import type * as desert from "./desert";
|
||||||
import { IRunContext } from "../system/combat";
|
import { IRunContext } from "../system/combat";
|
||||||
|
|
||||||
export type ContentModule = {
|
export type ContentModule = {
|
||||||
getCards: () => desert.Card[];
|
readonly getCards: () => readonly desert.Card[];
|
||||||
getEffects: () => desert.Effect[];
|
readonly getEffects: () => readonly desert.Effect[];
|
||||||
getEncounters: () => desert.Encounter[];
|
readonly getEncounters: () => readonly desert.Encounter[];
|
||||||
getEnemies: () => desert.Enemy[];
|
readonly getEnemies: () => readonly desert.Enemy[];
|
||||||
getIntents: () => desert.Intent[];
|
readonly getIntents: () => readonly desert.Intent[];
|
||||||
getItems: () => desert.Item[];
|
readonly getItems: () => readonly desert.Item[];
|
||||||
getStartingItems: () => desert.Item[];
|
readonly getStartingItems: () => readonly desert.Item[];
|
||||||
|
|
||||||
dialogues: YarnDialogues;
|
readonly dialogues: YarnDialogues;
|
||||||
addTriggers: (triggers: Triggers, run: IRunContext) => void;
|
readonly addTriggers: (triggers: Triggers, run: IRunContext) => void;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -3,27 +3,3 @@ export * from "./factory";
|
||||||
export * from "./prompts";
|
export * from "./prompts";
|
||||||
export * from "./triggers";
|
export * from "./triggers";
|
||||||
export * from "./types";
|
export * from "./types";
|
||||||
|
|
||||||
import { GameHost } from "@/core/game-host";
|
|
||||||
import { ContentModule } from "../types";
|
|
||||||
import { createCommandRegistry } from "@/utils/command";
|
|
||||||
import { createStart, createTriggers, Triggers } from "./triggers";
|
|
||||||
import { CombatState, IRunContext } from "./types";
|
|
||||||
|
|
||||||
export class CombatGameHost extends GameHost<CombatState> {
|
|
||||||
public readonly triggers: Triggers;
|
|
||||||
constructor(
|
|
||||||
private module: ContentModule,
|
|
||||||
private runContext: IRunContext,
|
|
||||||
private initialState: CombatState,
|
|
||||||
) {
|
|
||||||
let triggers: Triggers;
|
|
||||||
super(
|
|
||||||
createCommandRegistry(),
|
|
||||||
() => initialState,
|
|
||||||
createStart((triggers = createTriggers(runContext)), runContext),
|
|
||||||
);
|
|
||||||
module.addTriggers(triggers, runContext);
|
|
||||||
this.triggers = triggers;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue