Move scratch3 blocks into separate packages.

This commit is contained in:
Tim Mickel 2016-06-09 11:45:58 -04:00
parent 200f895b43
commit bd7f3245a2
4 changed files with 96 additions and 76 deletions

View file

@ -1,75 +0,0 @@
function Scratch3Blocks(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.
*/
Scratch3Blocks.prototype.getPrimitives = function() {
return {
'control_repeat': this.repeat,
'control_forever': this.forever,
'control_wait': this.wait,
'control_stop': this.stop,
'event_whenflagclicked': this.whenFlagClicked,
'event_whenbroadcastreceived': this.whenBroadcastReceived,
'event_broadcast': this.broadcast
};
};
Scratch3Blocks.prototype.repeat = function(argValues, util) {
// Initialize loop
if (util.stackFrame.loopCounter === undefined) {
util.stackFrame.loopCounter = parseInt(argValues[0]); // @todo arg
}
// Decrease counter
util.stackFrame.loopCounter--;
// If we still have some left, start the substack
if (util.stackFrame.loopCounter >= 0) {
util.startSubstack();
}
};
Scratch3Blocks.prototype.forever = function(argValues, util) {
util.startSubstack();
};
Scratch3Blocks.prototype.wait = function(argValues, util) {
util.yield();
util.timeout(function() {
util.done();
}, 1000 * parseFloat(argValues[0]));
};
Scratch3Blocks.prototype.stop = function() {
// @todo - don't use this.runtime
this.runtime.stopAll();
};
Scratch3Blocks.prototype.whenFlagClicked = function() {
// No-op
};
Scratch3Blocks.prototype.whenBroadcastReceived = function() {
// No-op
};
Scratch3Blocks.prototype.broadcast = function(argValues, util) {
util.startHats(function(hat) {
if (hat.opcode === 'event_whenbroadcastreceived') {
var shadows = hat.fields.CHOICE.blocks;
for (var sb in shadows) {
var shadowblock = shadows[sb];
return shadowblock.fields.CHOICE.value === argValues[0];
}
}
return false;
});
};
module.exports = Scratch3Blocks;

View file

@ -0,0 +1,51 @@
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,
'control_stop': this.stop
};
};
Scratch3ControlBlocks.prototype.repeat = function(argValues, util) {
// Initialize loop
if (util.stackFrame.loopCounter === undefined) {
util.stackFrame.loopCounter = parseInt(argValues[0]); // @todo arg
}
// Decrease counter
util.stackFrame.loopCounter--;
// If we still have some left, start the substack
if (util.stackFrame.loopCounter >= 0) {
util.startSubstack();
}
};
Scratch3ControlBlocks.prototype.forever = function(argValues, util) {
util.startSubstack();
};
Scratch3ControlBlocks.prototype.wait = function(argValues, util) {
util.yield();
util.timeout(function() {
util.done();
}, 1000 * parseFloat(argValues[0]));
};
Scratch3ControlBlocks.prototype.stop = function() {
// @todo - don't use this.runtime
this.runtime.stopAll();
};
module.exports = Scratch3ControlBlocks;

View file

@ -0,0 +1,43 @@
function Scratch3EventBlocks(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.
*/
Scratch3EventBlocks.prototype.getPrimitives = function() {
return {
'event_whenflagclicked': this.whenFlagClicked,
'event_whenbroadcastreceived': this.whenBroadcastReceived,
'event_broadcast': this.broadcast
};
};
Scratch3EventBlocks.prototype.whenFlagClicked = function() {
// No-op
};
Scratch3EventBlocks.prototype.whenBroadcastReceived = function() {
// No-op
};
Scratch3EventBlocks.prototype.broadcast = function(argValues, util) {
util.startHats(function(hat) {
if (hat.opcode === 'event_whenbroadcastreceived') {
var shadows = hat.fields.CHOICE.blocks;
for (var sb in shadows) {
var shadowblock = shadows[sb];
return shadowblock.fields.CHOICE.value === argValues[0];
}
}
return false;
});
};
module.exports = Scratch3EventBlocks;

View file

@ -4,7 +4,8 @@ var Thread = require('./thread');
var util = require('util');
var defaultBlockPackages = {
'scratch3': require('../blocks/scratch3'),
'scratch3_control': require('../blocks/scratch3_control'),
'scratch3_event': require('../blocks/scratch3_event'),
'wedo2': require('../blocks/wedo2')
};