mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-26 07:52:50 -05:00
135 lines
3.7 KiB
JavaScript
135 lines
3.7 KiB
JavaScript
const Cast = require('../util/cast');
|
|
|
|
class Scratch3ControlBlocks {
|
|
constructor (runtime) {
|
|
/**
|
|
* The runtime instantiating this block package.
|
|
* @type {Runtime}
|
|
*/
|
|
this.runtime = runtime;
|
|
}
|
|
|
|
/**
|
|
* Retrieve the block primitives implemented by this package.
|
|
* @return {object.<string, Function>} Mapping of opcode to Function.
|
|
*/
|
|
getPrimitives () {
|
|
return {
|
|
control_repeat: this.repeat,
|
|
control_repeat_until: this.repeatUntil,
|
|
control_forever: this.forever,
|
|
control_wait: this.wait,
|
|
control_wait_until: this.waitUntil,
|
|
control_if: this.if,
|
|
control_if_else: this.ifElse,
|
|
control_stop: this.stop,
|
|
control_create_clone_of: this.createClone,
|
|
control_delete_this_clone: this.deleteClone
|
|
};
|
|
}
|
|
|
|
getHats () {
|
|
return {
|
|
control_start_as_clone: {
|
|
restartExistingThreads: false
|
|
}
|
|
};
|
|
}
|
|
|
|
repeat (args, util) {
|
|
const times = Math.floor(Cast.toNumber(args.TIMES));
|
|
// Initialize loop
|
|
if (typeof util.stackFrame.loopCounter === 'undefined') {
|
|
util.stackFrame.loopCounter = times;
|
|
}
|
|
// Only execute once per frame.
|
|
// When the branch finishes, `repeat` will be executed again and
|
|
// the second branch will be taken, yielding for the rest of the frame.
|
|
// Decrease counter
|
|
util.stackFrame.loopCounter--;
|
|
// If we still have some left, start the branch.
|
|
if (util.stackFrame.loopCounter >= 0) {
|
|
util.startBranch(1, true);
|
|
}
|
|
}
|
|
|
|
repeatUntil (args, util) {
|
|
const condition = Cast.toBoolean(args.CONDITION);
|
|
// If the condition is true, start the branch.
|
|
if (!condition) {
|
|
util.startBranch(1, true);
|
|
}
|
|
}
|
|
|
|
waitUntil (args, util) {
|
|
const condition = Cast.toBoolean(args.CONDITION);
|
|
if (!condition) {
|
|
util.yield();
|
|
}
|
|
}
|
|
|
|
forever (args, util) {
|
|
util.startBranch(1, true);
|
|
}
|
|
|
|
wait (args) {
|
|
const duration = Math.max(0, 1000 * Cast.toNumber(args.DURATION));
|
|
return new Promise(resolve => {
|
|
setTimeout(() => {
|
|
resolve();
|
|
}, duration);
|
|
});
|
|
}
|
|
|
|
if (args, util) {
|
|
const condition = Cast.toBoolean(args.CONDITION);
|
|
if (condition) {
|
|
util.startBranch(1, false);
|
|
}
|
|
}
|
|
|
|
ifElse (args, util) {
|
|
const condition = Cast.toBoolean(args.CONDITION);
|
|
if (condition) {
|
|
util.startBranch(1, false);
|
|
} else {
|
|
util.startBranch(2, false);
|
|
}
|
|
}
|
|
|
|
stop (args, util) {
|
|
const option = args.STOP_OPTION;
|
|
if (option === 'all') {
|
|
util.stopAll();
|
|
} else if (option === 'other scripts in sprite' ||
|
|
option === 'other scripts in stage') {
|
|
util.stopOtherTargetThreads();
|
|
} else if (option === 'this script') {
|
|
util.stopThisScript();
|
|
}
|
|
}
|
|
|
|
createClone (args, util) {
|
|
let cloneTarget;
|
|
if (args.CLONE_OPTION === '_myself_') {
|
|
cloneTarget = util.target;
|
|
} else {
|
|
cloneTarget = this.runtime.getSpriteTargetByName(args.CLONE_OPTION);
|
|
}
|
|
if (!cloneTarget) {
|
|
return;
|
|
}
|
|
const newClone = cloneTarget.makeClone();
|
|
if (newClone) {
|
|
this.runtime.targets.push(newClone);
|
|
}
|
|
}
|
|
|
|
deleteClone (args, util) {
|
|
if (util.target.isOriginal) return;
|
|
this.runtime.disposeTarget(util.target);
|
|
this.runtime.stopForTarget(util.target);
|
|
}
|
|
}
|
|
|
|
module.exports = Scratch3ControlBlocks;
|