Skip to content

Commit

Permalink
feat: Properly reset simulator
Browse files Browse the repository at this point in the history
This commit provides the ability to properly reset the simulator
(including invalidating all existing handles). This allows scenes to be
re-created at any point.
  • Loading branch information
Zhiquan Yeo committed Jun 25, 2020
1 parent d2d4aa3 commit 48cebbf
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 153 deletions.
20 changes: 18 additions & 2 deletions src/engine/EventRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class EventRegistry {
private _world: World;

private _timeSinceLastUpdate = 0;
private _onDispose: () => void;

/**
* Create a new EventRegistry and connect it to the provided world
Expand All @@ -49,12 +50,27 @@ export class EventRegistry {
constructor(world: World) {
// Hook up the event listeners for the physics engine
// This is for collisions
world.on("begin-contact", this.onBeginContact.bind(this));
world.on("end-contact", this.onEndContact.bind(this));
const onBeginContantFunc: (
contact: Contact
) => void = this.onBeginContact.bind(this);
const onEndContantFunc: (contact: Contact) => void = this.onEndContact.bind(
this
);
world.on("begin-contact", onBeginContantFunc);
world.on("end-contact", onEndContantFunc);

this._onDispose = () => {
world.off("begin-contact", onBeginContantFunc);
world.off("end-contact", onEndContantFunc);
};

this._world = world;
}

dispose(): void {
this._onDispose();
}

// World event handlers
/**
* Event handler when a collision begins to occur (shapes overlapping)
Expand Down
33 changes: 33 additions & 0 deletions src/engine/HandleRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export class HandleRegistry {
private _invalidationCallbacks: Map<string, () => void> = new Map<
string,
() => void
>();

registerHandle(objectGuid: string, invalidateCb: () => void): void {
this._invalidationCallbacks.set(objectGuid, invalidateCb);
}

invalidateHandle(guid: string): void {
if (this._invalidationCallbacks.has(guid)) {
const invalidationCb = this._invalidationCallbacks.get(guid);
invalidationCb();
}
}

deleteHandle(guid: string): void {
this.invalidateHandle(guid);
this._invalidationCallbacks.delete(guid);
}

invalidateAllHandles(): void {
this._invalidationCallbacks.forEach((invalidationCb) => {
invalidationCb();
});
}

deleteAllHandles(): void {
this.invalidateAllHandles();
this._invalidationCallbacks.clear();
}
}
Loading

0 comments on commit 48cebbf

Please sign in to comment.