2016-09-12 13:14:16 -04:00
|
|
|
var Cast = require('../util/cast');
|
2016-10-17 23:23:16 -04:00
|
|
|
var Timer = require('../util/timer');
|
2016-06-30 17:06:50 -04:00
|
|
|
|
2016-10-23 17:55:31 -04:00
|
|
|
var Scratch3ControlBlocks = function (runtime) {
|
2016-06-09 11:45:58 -04:00
|
|
|
/**
|
|
|
|
* The runtime instantiating this block package.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
2016-06-09 11:45:58 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the block primitives implemented by this package.
|
|
|
|
* @return {Object.<string, Function>} Mapping of opcode to Function.
|
|
|
|
*/
|
2016-10-23 12:41:45 -04:00
|
|
|
Scratch3ControlBlocks.prototype.getPrimitives = function () {
|
2016-06-09 11:45:58 -04:00
|
|
|
return {
|
2016-10-24 11:02:19 -04:00
|
|
|
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
|
2016-09-15 19:37:12 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3ControlBlocks.prototype.getHats = function () {
|
|
|
|
return {
|
2016-10-24 11:02:19 -04:00
|
|
|
control_start_as_clone: {
|
2016-09-15 19:37:12 -04:00
|
|
|
restartExistingThreads: false
|
|
|
|
}
|
2016-06-09 11:45:58 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-10-23 12:41:45 -04:00
|
|
|
Scratch3ControlBlocks.prototype.repeat = function (args, util) {
|
2016-09-12 13:14:16 -04:00
|
|
|
var times = Math.floor(Cast.toNumber(args.TIMES));
|
2016-06-09 11:45:58 -04:00
|
|
|
// Initialize loop
|
2016-10-23 17:55:31 -04:00
|
|
|
if (typeof util.stackFrame.loopCounter === 'undefined') {
|
2016-09-12 13:14:16 -04:00
|
|
|
util.stackFrame.loopCounter = times;
|
2016-06-09 11:45:58 -04:00
|
|
|
}
|
2016-07-01 10:50:31 -04:00
|
|
|
// Only execute once per frame.
|
2016-08-10 11:27:21 -04:00
|
|
|
// When the branch finishes, `repeat` will be executed again and
|
2016-07-01 10:50:31 -04:00
|
|
|
// the second branch will be taken, yielding for the rest of the frame.
|
2016-10-17 23:23:16 -04:00
|
|
|
// Decrease counter
|
|
|
|
util.stackFrame.loopCounter--;
|
|
|
|
// If we still have some left, start the branch.
|
|
|
|
if (util.stackFrame.loopCounter >= 0) {
|
|
|
|
util.startBranch(1, true);
|
2016-06-09 11:45:58 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-23 12:41:45 -04:00
|
|
|
Scratch3ControlBlocks.prototype.repeatUntil = function (args, util) {
|
2016-09-12 13:14:16 -04:00
|
|
|
var condition = Cast.toBoolean(args.CONDITION);
|
2016-10-17 23:23:16 -04:00
|
|
|
// If the condition is true, start the branch.
|
|
|
|
if (!condition) {
|
|
|
|
util.startBranch(1, true);
|
2016-07-01 11:25:26 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-23 12:41:45 -04:00
|
|
|
Scratch3ControlBlocks.prototype.waitUntil = function (args, util) {
|
2016-09-22 17:00:38 -04:00
|
|
|
var condition = Cast.toBoolean(args.CONDITION);
|
|
|
|
if (!condition) {
|
2016-10-17 23:23:16 -04:00
|
|
|
util.yield();
|
2016-09-22 17:00:38 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-23 12:41:45 -04:00
|
|
|
Scratch3ControlBlocks.prototype.forever = function (args, util) {
|
2016-10-17 23:23:16 -04:00
|
|
|
util.startBranch(1, true);
|
2016-06-09 11:45:58 -04:00
|
|
|
};
|
|
|
|
|
2016-10-23 12:41:45 -04:00
|
|
|
Scratch3ControlBlocks.prototype.wait = function (args, util) {
|
2016-10-17 23:23:16 -04:00
|
|
|
if (!util.stackFrame.timer) {
|
|
|
|
util.stackFrame.timer = new Timer();
|
|
|
|
util.stackFrame.timer.start();
|
|
|
|
util.yield();
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
} else {
|
|
|
|
var duration = Math.max(0, 1000 * Cast.toNumber(args.DURATION));
|
|
|
|
if (util.stackFrame.timer.timeElapsed() < duration) {
|
|
|
|
util.yield();
|
|
|
|
}
|
|
|
|
}
|
2016-06-10 10:36:05 -04:00
|
|
|
};
|
|
|
|
|
2016-10-23 12:41:45 -04:00
|
|
|
Scratch3ControlBlocks.prototype.if = function (args, util) {
|
2016-09-12 13:14:16 -04:00
|
|
|
var condition = Cast.toBoolean(args.CONDITION);
|
2016-10-17 23:23:16 -04:00
|
|
|
if (condition) {
|
|
|
|
util.startBranch(1, false);
|
2016-06-10 10:36:05 -04:00
|
|
|
}
|
2016-06-09 11:45:58 -04:00
|
|
|
};
|
|
|
|
|
2016-10-23 12:41:45 -04:00
|
|
|
Scratch3ControlBlocks.prototype.ifElse = function (args, util) {
|
2016-09-12 13:14:16 -04:00
|
|
|
var condition = Cast.toBoolean(args.CONDITION);
|
2016-10-17 23:23:16 -04:00
|
|
|
if (condition) {
|
|
|
|
util.startBranch(1, false);
|
|
|
|
} else {
|
|
|
|
util.startBranch(2, false);
|
2016-06-10 10:40:15 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-23 12:41:45 -04:00
|
|
|
Scratch3ControlBlocks.prototype.stop = function (args, util) {
|
2016-10-13 23:00:46 -04:00
|
|
|
var option = args.STOP_OPTION;
|
2016-10-23 17:55:31 -04:00
|
|
|
if (option === 'all') {
|
2016-10-13 23:00:46 -04:00
|
|
|
util.stopAll();
|
2016-10-23 17:55:31 -04:00
|
|
|
} else if (option === 'other scripts in sprite' ||
|
|
|
|
option === 'other scripts in stage') {
|
2016-10-13 23:00:46 -04:00
|
|
|
util.stopOtherTargetThreads();
|
2016-10-23 17:55:31 -04:00
|
|
|
} else if (option === 'this script') {
|
2016-10-13 23:00:46 -04:00
|
|
|
util.stopThread();
|
|
|
|
}
|
2016-06-09 11:45:58 -04:00
|
|
|
};
|
|
|
|
|
2016-09-15 19:37:12 -04:00
|
|
|
Scratch3ControlBlocks.prototype.createClone = function (args, util) {
|
|
|
|
var cloneTarget;
|
2016-10-23 17:55:31 -04:00
|
|
|
if (args.CLONE_OPTION === '_myself_') {
|
2016-09-15 19:37:12 -04:00
|
|
|
cloneTarget = util.target;
|
|
|
|
} else {
|
|
|
|
cloneTarget = this.runtime.getSpriteTargetByName(args.CLONE_OPTION);
|
|
|
|
}
|
|
|
|
if (!cloneTarget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var newClone = cloneTarget.makeClone();
|
|
|
|
if (newClone) {
|
|
|
|
this.runtime.targets.push(newClone);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3ControlBlocks.prototype.deleteClone = function (args, util) {
|
2016-10-17 13:43:38 -04:00
|
|
|
if (util.target.isOriginal) return;
|
2016-09-15 19:37:12 -04:00
|
|
|
this.runtime.disposeTarget(util.target);
|
|
|
|
this.runtime.stopForTarget(util.target);
|
|
|
|
};
|
|
|
|
|
2016-06-09 11:45:58 -04:00
|
|
|
module.exports = Scratch3ControlBlocks;
|