Skip to content

Commit

Permalink
Fix hatBlockMatches and reduce kludginess a bit
Browse files Browse the repository at this point in the history
Stop monkey-patching the current thread and just make a new one. A bit
slower but at least it doesn't completely mess up the thread state.
  • Loading branch information
valadaptive committed May 29, 2024
1 parent 94faa8a commit 0b68046
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/audio/audio-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ export default class AudioTarget {
source.connect(this.destinationNode);
this.playingBuffers.set(sound, source);

return new Promise(resolve => {
return new Promise<void>(resolve => {
source.start();
source.addEventListener('ended', resolve, {once: true});
source.addEventListener('ended', () => {
resolve();
}, {once: true});
});
}

Expand Down
19 changes: 9 additions & 10 deletions src/interpreter/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,24 @@ export default class Thread {
}

hatBlockMatches(event: TypedEvent): boolean {
// Make sure the thread runs so we can check the hat block.
const threadWasDone = this.status === ThreadStatus.DONE;
if (threadWasDone) this.restart(event);

const protoBlock = this.topBlock.proto as SomeProtoBlock;
if (protoBlock.hat?.type !== 'event') return false;
if (!(event instanceof protoBlock.hat.event)) return false;

const hatThread = new Thread(
this.script,
this.target,
this.blockContext,
event,
);

// Hats can be started in the middle of executing other scripts (e.g. "broadcast"), so we need to store
// and restore the previous target and thread.
const oldTarget = this.blockContext.target;
const oldThread = this.blockContext.thread;

this.blockContext.target = this.target;
this.blockContext.thread = this;
this.blockContext.thread = hatThread;

const generator = protoBlock.execute(
this.topBlock.inputValues,
Expand All @@ -178,11 +182,6 @@ export default class Thread {
}

// Restore the previous target and thread.
if (threadWasDone) {
this.retire();
} else {
this.resume();
}
this.blockContext.target = oldTarget;
this.blockContext.thread = oldThread;

Expand Down
3 changes: 3 additions & 0 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
- Apply proper viewbox offset to SVG skin rotation centers (done)
- Draw pen lines with instancing (no longer planned; tested and has no performance benefit)
- Really small sprites should trigger "touching" blocks (see: griffpatch's Ball Physics Scroll v0.4)
- Clones' audio shouldn't stop playing on clone deletion, and clone sounds should cut off other clones' sounds
- Tricky because we don't want to leak a bunch of audio nodes
- We can't share the AudioTarget between all clones because the effects are distinct
- Revamp block introspection API
- want to be able to type ctx.evaluate properly
- specify "only accept blocks returning X"
Expand Down

0 comments on commit 0b68046

Please sign in to comment.