Skip to content

Commit 7189666

Browse files
authored
i hate myself (uhh autostart actually autostarts now) (#519)
1 parent 902f6b3 commit 7189666

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

static/extensions/Gen1x/beat_sync.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
this.vmEventBound = false;
2121

22-
this._loadAutoStart();
22+
this.loadAutoStart();
2323
}
2424

25-
_loadAutoStart() {
25+
loadAutoStart() {
2626
if (!isPenguinMod) {
2727
try {
2828
const vm = Scratch.vm;
@@ -35,7 +35,7 @@
3535
}
3636
}
3737

38-
_saveAutoStart() {
38+
saveAutoStart() {
3939
if (!isPenguinMod) {
4040
try {
4141
const vm = Scratch.vm;
@@ -252,14 +252,15 @@
252252

253253
toggleAutoStart() {
254254
this.autoStart = !this.autoStart;
255-
this._saveAutoStart();
255+
this.saveAutoStart();
256256
Scratch.vm.extensionManager.refreshBlocks('beatSync');
257257
}
258258

259259
startBeat() {
260260
if (!this.isRunning) {
261261
this.isRunning = true;
262-
this.startAudioTime = this.getAudioCtx().currentTime;
262+
const ctx = this.getAudioCtx();
263+
this.startAudioTime = ctx.currentTime;
263264
}
264265
}
265266

@@ -275,7 +276,8 @@
275276
this.totalBeats = 0;
276277
this.beatPosition = 0;
277278
if (this.isRunning) {
278-
this.startAudioTime = this.getAudioCtx().currentTime;
279+
const ctx = this.getAudioCtx();
280+
this.startAudioTime = ctx.currentTime;
279281
}
280282
}
281283

@@ -284,6 +286,7 @@
284286
const startBeat = Math.floor(this.totalBeats);
285287
return new Promise(resolve => {
286288
const poll = () => {
289+
this.updateTime();
287290
if (Math.floor(this.totalBeats) > startBeat) {
288291
resolve();
289292
} else {
@@ -331,29 +334,32 @@
331334
const beatInMeasure = this.totalBeats % this.beatsPerMeasure;
332335
return beatInMeasure < 0.5;
333336
}
337+
338+
onGreenFlag() {
339+
this.resetBeat();
340+
if (this.autoStart) {
341+
this.startBeat();
342+
}
343+
}
344+
345+
onStopAll() {
346+
this.stopBeat();
347+
this.resetBeat();
348+
}
334349
}
335350

336351
const extensionInstance = new BeatSync();
337-
338352
const runtime = Scratch.vm.runtime;
339353

340354
extensionInstance.setupVMEvents();
341355

342-
const originalGreenFlag = runtime.greenFlag;
343-
runtime.greenFlag = function() {
344-
extensionInstance.resetBeat();
345-
if (extensionInstance.autoStart) {
346-
extensionInstance.startBeat();
347-
}
348-
return originalGreenFlag.apply(this, arguments);
349-
};
350-
351-
const originalStopAll = runtime.stopAll;
352-
runtime.stopAll = function() {
353-
extensionInstance.stopBeat();
354-
extensionInstance.resetBeat();
355-
return originalStopAll.apply(this, arguments);
356-
};
356+
runtime.on('PROJECT_START', () => {
357+
extensionInstance.onGreenFlag();
358+
});
359+
360+
runtime.on('PROJECT_STOP_ALL', () => {
361+
extensionInstance.onStopAll();
362+
});
357363

358364
Scratch.extensions.register(extensionInstance);
359365
})(Scratch);

0 commit comments

Comments
 (0)