Skip to content

Commit

Permalink
make world run and dispatch take state constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
minecrawler committed Nov 10, 2020
1 parent 9584109 commit 3954768
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/pong/src/main.ts
Expand Up @@ -75,7 +75,7 @@ const runGame = async (world: IWorld) => {
return world.run({
afterStepHandler: afterFrameStep,
beforeStepHandler: beforeFrameStep,
initialState: new MenuState(), // todo: do not require constructor here!
initialState: MenuState,
}).catch(console.error);
}

Expand Down
8 changes: 3 additions & 5 deletions src/state.ts
Expand Up @@ -5,11 +5,9 @@ import {ITransitionActions} from "./world.spec";
export * from './state.spec';

export class State implements IState {
protected _systems: TSystemProto<TSystemData>[];

constructor(systems: TSystemProto<TSystemData>[] = []) {
this._systems = systems;
}
constructor(
protected _systems: TSystemProto<TSystemData>[] = []
) {}

get systems(): TSystemProto<TSystemData>[] {
return this._systems;
Expand Down
8 changes: 4 additions & 4 deletions src/world.spec.ts
@@ -1,7 +1,7 @@
import {IEntity} from "./entity.spec";
import IEntityBuilder from "./entity-builder.spec";
import ISystem, {TSystemData, TSystemProto} from "./system.spec";
import IState from "./state.spec";
import IState, {TStateProto} from "./state.spec";
import {TTypeProto} from "./_.spec";
import {ISaveFormat, TSerializer} from "./save-format.spec";
import {TComponentAccess} from "./queue.spec";
Expand All @@ -15,9 +15,9 @@ export type TPrefab = { [Component: string]: Object }[];
export interface IRunConfiguration {
afterStepHandler?: (actions: ITransitionActions) => Promise<void> | void
beforeStepHandler?: (actions: ITransitionActions) => Promise<void> | void
initialState?: IState,
initialState?: TStateProto,
}
export interface IStaticRunConfiguration extends IRunConfiguration {
export interface IStaticRunConfiguration {
afterStepHandler: (actions: ITransitionActions) => Promise<void> | void
beforeStepHandler: (actions: ITransitionActions) => Promise<void> | void
initialState: IState,
Expand Down Expand Up @@ -173,7 +173,7 @@ export interface IWorld extends IPartialWorld {
* Execute all systems
* @param state
*/
dispatch(state?: IState): Promise<void>
dispatch(state?: TStateProto): Promise<void>

/**
* Execute all systems continuously in a dispatch-loop
Expand Down
9 changes: 5 additions & 4 deletions src/world.ts
Expand Up @@ -13,7 +13,7 @@ import {
} from "./world.spec";
import IEntity from "./entity.spec";
import ISystem, {TSystemData, TSystemProto} from "./system.spec";
import {IState, State} from "./state";
import {IState, State, TStateProto} from "./state";
import {TTypeProto} from "./_.spec";
import {PushDownAutomaton} from "./pda";
import {getDefaultDeserializer, SaveFormat} from "./save-format";
Expand Down Expand Up @@ -205,7 +205,7 @@ export class World implements IWorld {
return entity;
}

async dispatch(state?: IState): Promise<void> {
async dispatch(state?: TStateProto): Promise<void> {
await this.run({
initialState: state,
afterStepHandler: actions => actions.stopRun(),
Expand Down Expand Up @@ -402,9 +402,10 @@ export class World implements IWorld {
}

configuration ||= {};
configuration.initialState ||= new State(Array.from(this.systemInfos.keys()).map(system => system.constructor as TSystemProto<TSystemData>));

const initialState = configuration.initialState;
const initialState = configuration.initialState
? new configuration.initialState()
: new State(Array.from(this.systemInfos.keys()).map(system => system.constructor as TSystemProto<TSystemData>));
const runConfig: IStaticRunConfiguration = {
afterStepHandler: configuration.afterStepHandler ?? (_action => {}),
beforeStepHandler: configuration.beforeStepHandler ?? (_action => {}),
Expand Down

0 comments on commit 3954768

Please sign in to comment.