diff --git a/src/blocks/scratch3.js b/src/blocks/scratch3.js deleted file mode 100644 index a2f132532..000000000 --- a/src/blocks/scratch3.js +++ /dev/null @@ -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.} 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; diff --git a/src/blocks/scratch3_control.js b/src/blocks/scratch3_control.js new file mode 100644 index 000000000..50e7af645 --- /dev/null +++ b/src/blocks/scratch3_control.js @@ -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.} 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; diff --git a/src/blocks/scratch3_event.js b/src/blocks/scratch3_event.js new file mode 100644 index 000000000..ff15416df --- /dev/null +++ b/src/blocks/scratch3_event.js @@ -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.} 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; diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 930fa0ea5..5b4fab48d 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -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') };