Compare commits
2 Commits
bbab3cc43b
...
23ac09ff21
| Author | SHA1 | Date |
|---|---|---|
|
|
23ac09ff21 | |
|
|
e8c995f74f |
|
|
@ -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,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,4 +71,6 @@ export { createRNG, Mulberry32RNG } from "./utils/rng";
|
||||||
export type { MiddlewareChain } from "./utils/middleware";
|
export type { MiddlewareChain } from "./utils/middleware";
|
||||||
export { createMiddlewareChain } from "./utils/middleware";
|
export { createMiddlewareChain } from "./utils/middleware";
|
||||||
|
|
||||||
|
export * from "./utils/spawner";
|
||||||
|
|
||||||
export * from "@preact/signals-core";
|
export * from "@preact/signals-core";
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
import { effect } from "@preact/signals-core";
|
||||||
|
|
||||||
|
export type SpawnerFunction = (id: string) => () => void;
|
||||||
|
|
||||||
|
export function createSpawner(spawner: SpawnerFunction) {
|
||||||
|
const entries = new Map<string, () => void>();
|
||||||
|
|
||||||
|
function update(set: Set<string>) {
|
||||||
|
for (const key of set) {
|
||||||
|
if (!entries.has(key)) {
|
||||||
|
entries.set(key, spawner(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [key, entry] of entries) {
|
||||||
|
if (!set.has(key)) {
|
||||||
|
entry();
|
||||||
|
entries.delete(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return update;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function spawnerEffect(
|
||||||
|
iterable: () => Iterable<string>,
|
||||||
|
spawner: SpawnerFunction,
|
||||||
|
) {
|
||||||
|
const update = createSpawner(spawner);
|
||||||
|
|
||||||
|
return effect(() => {
|
||||||
|
const set = new Set<string>();
|
||||||
|
for (const each of iterable()) {
|
||||||
|
set.add(each);
|
||||||
|
}
|
||||||
|
update(set);
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue