Spaces:
Build error
Build error
File size: 10,495 Bytes
30c32c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
const Cast = require('../util/cast');
const SandboxRunner = require('../util/sandboxed-javascript-runner.js');
class Scratch3ControlBlocks {
constructor (runtime) {
/**
* The runtime instantiating this block package.
* @type {Runtime}
*/
this.runtime = runtime;
/**
* The "counter" block value. For compatibility with 2.0.
* @type {number}
*/
this._counter = 0; // used by compiler
/**
* The "error" block value.
* @type {string}
*/
this._error = ''; // used by compiler
this.runtime.on('RUNTIME_DISPOSED', this.clearCounter.bind(this));
}
/**
* 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_while: this.repeatWhile,
control_for_each: this.forEach,
control_forever: this.forever,
control_wait: this.wait,
control_repeatForSeconds: this.repeatForSeconds,
control_waittick: this.waitTick,
control_waitsecondsoruntil: this.waitOrUntil,
control_wait_until: this.waitUntil,
control_if: this.if,
control_if_else: this.ifElse,
control_stop: this.stop,
control_stop_sprite: this.stopSprite,
control_create_clone_of: this.createClone,
control_delete_this_clone: this.deleteClone,
control_delete_clones_of: this.deleteClonesOf,
control_get_counter: this.getCounter,
control_incr_counter: this.incrCounter,
control_decr_counter: this.decrCounter,
control_set_counter: this.setCounter,
control_clear_counter: this.clearCounter,
control_all_at_once: this.allAtOnce,
control_backToGreenFlag: this.backToGreenFlag,
control_if_return_else_return: this.if_return_else_return,
control_javascript_command: this.runJavascript
};
}
getMonitored () {
return {
control_get_counter: {
getId: () => 'get_counter'
}
};
}
backToGreenFlag(_, util) {
const thisThread = util.thread.topBlock;
this.runtime.emit("PROJECT_START_BEFORE_RESET");
this.runtime.threads
.filter(thread => thread.topBlock !== thisThread)
.forEach(thread => thread.stopThisScript());
// green flag behaviour
this.runtime.emit("PROJECT_START");
this.runtime.updateCurrentMSecs();
this.runtime.ioDevices.clock.resetProjectTimer();
this.runtime.targets.forEach(target => target.clearEdgeActivatedValues());
for (let i = this.runtime.targets.length - 1; i >= 0; i--) {
const thisTarget = this.runtime.targets[i];
thisTarget.onGreenFlag();
if (!thisTarget.isOriginal) {
this.runtime.disposeTarget(thisTarget);
this.runtime.stopForTarget(thisTarget);
}
}
this.runtime.startHats("event_whenflagclicked");
}
if_return_else_return (args) {
return Cast.toBoolean(args.boolean) ? args.TEXT1 : args.TEXT2;
}
getHats () {
return {
control_start_as_clone: {
restartExistingThreads: false
}
};
}
runJavascript(args) {
return new Promise((resolve) => {
const js = Cast.toString(args.JS);
SandboxRunner.execute(js).then(result => {
resolve(result.value);
});
});
}
repeat (args, util) {
const times = Math.round(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 false (repeat UNTIL), start the branch.
if (!condition) {
util.startBranch(1, true);
}
}
repeatWhile (args, util) {
const condition = Cast.toBoolean(args.CONDITION);
// If the condition is true (repeat WHILE), start the branch.
if (condition) {
util.startBranch(1, true);
}
}
forEach (args, util) {
const variable = util.target.lookupOrCreateVariable(
args.VARIABLE.id, args.VARIABLE.name);
if (typeof util.stackFrame.index === 'undefined') {
util.stackFrame.index = 0;
}
if (util.stackFrame.index < Number(args.VALUE)) {
util.stackFrame.index++;
variable.value = util.stackFrame.index;
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, util) {
if (util.stackTimerNeedsInit()) {
const duration = Math.max(0, 1000 * Cast.toNumber(args.DURATION));
util.startStackTimer(duration);
this.runtime.requestRedraw();
util.yield();
} else if (!util.stackTimerFinished()) {
util.yield();
}
}
repeatForSeconds (args, util) {
if (util.stackTimerNeedsInit()) {
const duration = Math.max(0, 1000 * Cast.toNumber(args.TIMES));
util.startStackTimer(duration);
this.runtime.requestRedraw();
util.startBranch(1, true);
util.yield();
} else if (!util.stackTimerFinished()) {
util.startBranch(1, true);
util.yield();
}
}
waitTick (_, util) {
util.yieldTick();
}
waitOrUntil (args, util) {
const condition = Cast.toBoolean(args.CONDITION);
if (!condition) {
if (util.stackTimerNeedsInit()) {
const duration = Math.max(0, 1000 * Cast.toNumber(args.DURATION));
util.startStackTimer(duration);
this.runtime.requestRedraw();
util.yield();
return;
}
if (!util.stackTimerFinished()) {
util.yield();
}
}
}
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();
}
}
stopSprite (args, util) {
const option = args.STOP_OPTION;
// Set target
let target;
if (option === '_myself_') {
target = util.target;
} else if (option === '_stage_') {
target = this.runtime.getTargetForStage();
} else {
target = this.runtime.getSpriteTargetByName(option);
}
if (!target) return;
this.runtime.stopForTarget(target);
}
createClone (args, util) {
this._createClone(Cast.toString(args.CLONE_OPTION), util.target);
}
_createClone (cloneOption, target) { // used by compiler
// Set clone target
let cloneTarget;
if (cloneOption === '_myself_') {
cloneTarget = target;
} else {
cloneTarget = this.runtime.getSpriteTargetByName(cloneOption);
}
// If clone target is not found, return
if (!cloneTarget) return;
// Create clone
const newClone = cloneTarget.makeClone();
if (newClone) {
this.runtime.addTarget(newClone);
// Place behind the original target.
newClone.goBehindOther(cloneTarget);
}
}
deleteClone (args, util) {
if (util.target.isOriginal) return;
this.runtime.disposeTarget(util.target);
this.runtime.stopForTarget(util.target);
}
deleteClonesOf (args, util) {
const cloneOption = Cast.toString(args.CLONE_OPTION);
// Set clone target
let cloneTarget;
if (cloneOption === '_myself_') {
cloneTarget = util.target;
} else {
cloneTarget = this.runtime.getSpriteTargetByName(cloneOption);
}
// If clone target is not found, return
if (!cloneTarget) return;
const sprite = cloneTarget.sprite;
if (!sprite) return;
if (!sprite.clones) return;
const cloneList = [].concat(sprite.clones);
cloneList.forEach(clone => {
if (clone.isOriginal) return;
if (clone.isStage) return;
this.runtime.disposeTarget(clone);
this.runtime.stopForTarget(clone);
})
}
getCounter () {
return this._counter;
}
setCounter (args) {
const num = Cast.toNumber(args.VALUE);
this._counter = num;
}
clearCounter () {
this._counter = 0;
}
incrCounter () {
this._counter++;
}
decrCounter () {
this._counter--;
}
allAtOnce (util) {
util.thread.peekStackFrame().warpMode = false;
util.startBranch(1, false);
util.thread.peekStackFrame().warpMode = true;
}
}
module.exports = Scratch3ControlBlocks;
|