Skip to content

Commit

Permalink
add event subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sz-piotr committed Jul 21, 2018
1 parent a64dff7 commit eaf77df
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/core/Game.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Query } from './Query'
import { Event } from './Events'
import { World, GameWorld } from './World'
import { System, InternalSystem, toInternalSystem } from './systems'

export class Game {
private _systems: { [key: string]: InternalSystem[] } = {}
private _world: GameWorld
private _subscriptions: ((event: Event) => void)[] = []

constructor (systems: System[], init?: (world: World) => void) {
const queries: Query[] = []
Expand All @@ -26,8 +28,16 @@ export class Game {
update (time: number) {
this._world._internal_tick(time)

let event
while (event = this._world._internal_nextEvent()) {
while (true) {
const event = this._world._internal_nextEvent()
if (!event) {
break
}

for (const listener of this._subscriptions) {
listener(event)
}

if(this._systems[event.type]) {
for (const system of this._systems[event.type]) {
this._world._internal_handleChanges()
Expand All @@ -48,4 +58,14 @@ export class Game {
}
update()
}

subscribe (listener: (event: Event) => void): () => void {
this._subscriptions.push(listener)
return () => {
this._subscriptions.splice(
this._subscriptions.indexOf(listener),
1
)
}
}
}

0 comments on commit eaf77df

Please sign in to comment.