2016-06-09 11:45:58 -04:00
|
|
|
function Scratch3ControlBlocks(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.
|
|
|
|
*/
|
|
|
|
Scratch3ControlBlocks.prototype.getPrimitives = function() {
|
|
|
|
return {
|
|
|
|
'control_repeat': this.repeat,
|
|
|
|
'control_forever': this.forever,
|
|
|
|
'control_wait': this.wait,
|
2016-06-10 10:36:05 -04:00
|
|
|
'control_if': this.if,
|
2016-06-10 10:40:15 -04:00
|
|
|
'control_if_else': this.ifElse,
|
2016-06-09 11:45:58 -04:00
|
|
|
'control_stop': this.stop
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-06-10 10:36:05 -04:00
|
|
|
Scratch3ControlBlocks.prototype.repeat = function(args, util) {
|
2016-06-09 11:45:58 -04:00
|
|
|
// Initialize loop
|
|
|
|
if (util.stackFrame.loopCounter === undefined) {
|
2016-06-10 10:36:05 -04:00
|
|
|
util.stackFrame.loopCounter = parseInt(args.TIMES);
|
2016-06-09 11:45:58 -04:00
|
|
|
}
|
|
|
|
// Decrease counter
|
|
|
|
util.stackFrame.loopCounter--;
|
|
|
|
// If we still have some left, start the substack
|
|
|
|
if (util.stackFrame.loopCounter >= 0) {
|
|
|
|
util.startSubstack();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-10 10:36:05 -04:00
|
|
|
Scratch3ControlBlocks.prototype.forever = function(args, util) {
|
2016-06-09 11:45:58 -04:00
|
|
|
util.startSubstack();
|
|
|
|
};
|
|
|
|
|
2016-06-10 10:36:05 -04:00
|
|
|
Scratch3ControlBlocks.prototype.wait = function(args, util) {
|
2016-06-09 11:45:58 -04:00
|
|
|
util.yield();
|
|
|
|
util.timeout(function() {
|
|
|
|
util.done();
|
2016-06-10 10:36:05 -04:00
|
|
|
}, 1000 * args.DURATION);
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3ControlBlocks.prototype.if = function(args, util) {
|
|
|
|
// Only execute one time. `if` will be returned to
|
|
|
|
// when the substack finishes, but it shouldn't execute again.
|
|
|
|
if (util.stackFrame.executed === undefined) {
|
|
|
|
util.stackFrame.executed = true;
|
|
|
|
if (args.CONDITION) {
|
|
|
|
util.startSubstack();
|
|
|
|
}
|
|
|
|
}
|
2016-06-09 11:45:58 -04:00
|
|
|
};
|
|
|
|
|
2016-06-10 10:40:15 -04:00
|
|
|
Scratch3ControlBlocks.prototype.ifElse = function(args, util) {
|
|
|
|
// Only execute one time. `ifElse` will be returned to
|
|
|
|
// when the substack finishes, but it shouldn't execute again.
|
|
|
|
if (util.stackFrame.executed === undefined) {
|
|
|
|
util.stackFrame.executed = true;
|
|
|
|
if (args.CONDITION) {
|
|
|
|
util.startSubstack(1);
|
|
|
|
} else {
|
|
|
|
util.startSubstack(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-09 11:45:58 -04:00
|
|
|
Scratch3ControlBlocks.prototype.stop = function() {
|
|
|
|
// @todo - don't use this.runtime
|
|
|
|
this.runtime.stopAll();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Scratch3ControlBlocks;
|