-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#9 prepare eventing and implement it for resource adding and PDA
- Loading branch information
1 parent
afc4169
commit ae28821
Showing
14 changed files
with
212 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,41 @@ | ||
import {PushDownAutomaton} from "./pda.ts"; | ||
import type {IRuntimeWorld} from "../world/runtime/runtime-world.spec.ts"; | ||
import type {TTypeProto} from "../_.spec.ts"; | ||
import {SimECSPDAPushStateEvent} from "../events/internal-events.ts"; | ||
import {SimECSPDAPopStateEvent, SimECSPDAPushStateEvent} from "../events/internal-events.ts"; | ||
import type {IState} from "../state/state.spec.ts"; | ||
import type {ITransitionActions} from "../world/actions.spec.ts"; | ||
|
||
export * from "./pda.ts"; | ||
|
||
export class SimECSPushDownAutomaton<T extends IState> extends PushDownAutomaton<T> { | ||
export class SimECSPushDownAutomaton<T extends IState> { | ||
#pda = new PushDownAutomaton<T>(); | ||
|
||
constructor( | ||
protected world: IRuntimeWorld | ||
) { | ||
super(); | ||
) {} | ||
|
||
get state(): T | undefined { | ||
return this.#pda.state; | ||
} | ||
|
||
clear(actions: Readonly<ITransitionActions>): void { | ||
while (this.#pda.state !== undefined) { | ||
this.#pda.pop()!.destroy(actions); | ||
} | ||
|
||
this.#pda.clear(); | ||
} | ||
|
||
async pop(): Promise<T | undefined> { | ||
const oldState = this.#pda.pop(); | ||
await this.world.eventBus.publish(new SimECSPDAPopStateEvent(oldState, this.#pda.state)) | ||
return oldState as T | undefined; | ||
} | ||
|
||
push<P extends TTypeProto<T>>(State: P): Promise<void> { | ||
super.push(State); | ||
return this.world.eventBus.publish(new SimECSPDAPushStateEvent(State)); | ||
async push<P extends TTypeProto<T>>(State: P): Promise<T> { | ||
const oldState = this.state; | ||
const newState = this.#pda.push(State); | ||
await this.world.eventBus.publish(new SimECSPDAPushStateEvent(oldState, newState)); | ||
return newState; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type {IRuntimeWorld} from "../world/runtime/runtime-world.spec"; | ||
import type {SimECSEvent} from "../events/internal-events.ts"; | ||
import type {TTypeProto} from "../_.spec.ts"; | ||
import type {TSubscriber} from "../events/_.ts"; | ||
|
||
/** @internal */ | ||
export interface ISystemContext { | ||
/** @internal */ | ||
_context: IRuntimeWorld | undefined | ||
/** @internal */ | ||
_handlers: Map<TTypeProto<SimECSEvent>, TSubscriber<TTypeProto<SimECSEvent>>> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {ISystem} from "./system.spec"; | ||
import { | ||
SimECSReplaceResourceEvent, | ||
} from "../events/internal-events.ts"; | ||
import {RuntimeWorld} from "../world/runtime/runtime-world.ts"; | ||
import {TObjectProto, TTypeProto} from "../_.spec.ts"; | ||
import {ISystemContext} from "./system_context.spec.ts"; | ||
import {TSubscriber} from "../events/_.ts"; | ||
|
||
|
||
export function setRuntimeContext(this: ISystem & ISystemContext, context: RuntimeWorld): void { | ||
{ | ||
const handler: TSubscriber<TTypeProto<SimECSReplaceResourceEvent<TObjectProto>>> = handleSimECSReplaceResourceEvent.bind(this); | ||
this._handlers.set(SimECSReplaceResourceEvent, handler); | ||
context.eventBus.subscribe(SimECSReplaceResourceEvent, handler); | ||
} | ||
|
||
this._context = context; | ||
} | ||
|
||
export function unsetRuntimeContext(this: ISystem & ISystemContext, context: RuntimeWorld): void { | ||
let handler; | ||
for (handler of this._handlers) { | ||
context.eventBus.unsubscribe(handler[0], handler[1]); | ||
} | ||
|
||
this._context = undefined; | ||
} | ||
|
||
|
||
function handleSimECSReplaceResourceEvent(this: ISystem, event: Readonly<SimECSReplaceResourceEvent<TObjectProto>>) { | ||
Object.entries(this.parameterDesc) | ||
.filter(param => param[1] instanceof event.resourceType) | ||
.forEach(param => this.parameterDesc[param[0]] = event.resourceObject); | ||
} |
Oops, something went wrong.