Refactor block management into a separate module.

This commit is contained in:
Tim Mickel 2016-06-06 15:29:34 -04:00
parent 25c3a38c16
commit 9be33089a8
4 changed files with 231 additions and 189 deletions
src/engine

View file

@ -10,25 +10,18 @@ var defaultBlockPackages = {
/**
* Manages blocks, stacks, and the sequencer.
* @param blocks Blocks instance for this runtime.
*/
function Runtime () {
function Runtime (blocks) {
// Bind event emitter
EventEmitter.call(this);
// State for the runtime
/**
* All blocks in the workspace.
* Keys are block IDs, values are metadata about the block.
* @type {Object.<string, Object>}
*/
this.blocks = {};
/**
* All stacks in the workspace.
* A list of block IDs that represent stacks (first block in stack).
* @type {Array.<String>}
* Block management and storage
*/
this.stacks = [];
this.blocks = blocks;
/**
* A list of threads that are currently running in the VM.
@ -83,106 +76,6 @@ util.inherits(Runtime, EventEmitter);
*/
Runtime.THREAD_STEP_INTERVAL = 1000 / 30;
/**
* Block management: create blocks and stacks from a `create` event
* @param {!Object} block Blockly create event to be processed
*/
Runtime.prototype.createBlock = function (block, opt_isFlyoutBlock) {
// Create new block
this.blocks[block.id] = block;
// Push block id to stacks array.
// Blocks are added as a top-level stack if they are marked as a topBlock
// (if they were top-level XML in the event) and if they are not
// flyout blocks.
if (!opt_isFlyoutBlock && block.topBlock) {
this._addStack(block.id);
}
};
/**
* Block management: change block field values
* @param {!Object} args Blockly change event to be processed
*/
Runtime.prototype.changeBlock = function (args) {
// Validate
if (args.element !== 'field') return;
if (typeof this.blocks[args.id] === 'undefined') return;
if (typeof this.blocks[args.id].fields[args.name] === 'undefined') return;
// Update block value
this.blocks[args.id].fields[args.name].value = args.value;
};
/**
* Block management: move blocks from parent to parent
* @param {!Object} e Blockly move event to be processed
*/
Runtime.prototype.moveBlock = function (e) {
var _this = this;
// Remove from any old parent.
if (e.oldParent !== undefined) {
var oldParent = _this.blocks[e.oldParent];
if (e.oldInput !== undefined &&
oldParent.inputs[e.oldInput].block === e.id) {
// This block was connected to the old parent's input.
oldParent.inputs[e.oldInput].block = null;
} else if (oldParent.next === e.id) {
// This block was connected to the old parent's next connection.
oldParent.next = null;
}
}
// Has the block become a top-level block?
if (e.newParent === undefined) {
_this._addStack(e.id);
} else {
// Remove stack, if one exists.
_this._deleteStack(e.id);
// Otherwise, try to connect it in its new place.
if (e.newInput !== undefined) {
// Moved to the new parent's input.
_this.blocks[e.newParent].inputs[e.newInput] = {
name: e.newInput,
block: e.id
};
} else {
// Moved to the new parent's next connection.
_this.blocks[e.newParent].next = e.id;
}
}
};
/**
* Block management: delete blocks and their associated stacks
* @param {!Object} e Blockly delete event to be processed
*/
Runtime.prototype.deleteBlock = function (e) {
// @todo Stop threads running on this stack
// Get block
var block = this.blocks[e.id];
// Delete children
if (block.next !== null) {
this.deleteBlock({id: block.next});
}
// Delete inputs (including substacks)
for (var input in block.inputs) {
// If it's null, the block in this input moved away.
if (block.inputs[input].block !== null) {
this.deleteBlock({id: block.inputs[input].block});
}
}
// Delete stack
this._deleteStack(e.id);
// Delete block
delete this.blocks[e.id];
};
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
@ -268,10 +161,11 @@ Runtime.prototype.greenFlag = function () {
this._removeThread(this.threads[i]);
}
// Add all top stacks with green flag
for (var j = 0; j < this.stacks.length; j++) {
var topBlock = this.stacks[j];
if (this.blocks[topBlock].opcode === 'event_whenflagclicked') {
this._pushThread(this.stacks[j]);
var stacks = this.blocks.getStacks();
for (var j = 0; j < stacks.length; j++) {
var topBlock = stacks[j];
if (this.blocks.getBlock(topBlock).opcode === 'event_whenflagclicked') {
this._pushThread(stacks[j]);
}
}
};
@ -281,9 +175,11 @@ Runtime.prototype.greenFlag = function () {
*/
Runtime.prototype.startDistanceSensors = function () {
// Add all top stacks with distance sensor
for (var j = 0; j < this.stacks.length; j++) {
var topBlock = this.stacks[j];
if (this.blocks[topBlock].opcode === 'wedo_whendistanceclose') {
var stacks = this.blocks.getStacks();
for (var j = 0; j < stacks.length; j++) {
var topBlock = stacks[j];
if (this.blocks.getBlock(topBlock).opcode ===
'wedo_whendistanceclose') {
var alreadyRunning = false;
for (var k = 0; k < this.threads.length; k++) {
if (this.threads[k].topBlock === topBlock) {
@ -291,7 +187,7 @@ Runtime.prototype.startDistanceSensors = function () {
}
}
if (!alreadyRunning) {
this._pushThread(this.stacks[j]);
this._pushThread(stacks[j]);
}
}
}
@ -345,60 +241,4 @@ Runtime.prototype.start = function () {
}.bind(this), Runtime.THREAD_STEP_INTERVAL);
};
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
/**
* Helper to add a stack to `this.stacks`
* @param {?string} id ID of block that starts the stack
*/
Runtime.prototype._addStack = function (id) {
var i = this.stacks.indexOf(id);
if (i > -1) return; // Already in stacks.
this.stacks.push(id);
// Update `topLevel` property on the top block.
this.blocks[id].topLevel = true;
};
/**
* Helper to remove a stack from `this.stacks`
* @param {?string} id ID of block that starts the stack
*/
Runtime.prototype._deleteStack = function (id) {
var i = this.stacks.indexOf(id);
if (i > -1) this.stacks.splice(i, 1);
// Update `topLevel` property on the top block.
if (this.blocks[id]) this.blocks[id].topLevel = false;
};
/**
* Helper to get the next block for a particular block
* @param {?string} id ID of block to get the next block for
* @return {?string} ID of next block in the sequence
*/
Runtime.prototype._getNextBlock = function (id) {
if (typeof this.blocks[id] === 'undefined') return null;
return this.blocks[id].next;
};
/**
* Helper to get the substack for a particular C-shaped block
* @param {?string} id ID for block to get the substack for
* @return {?string} ID of block in the substack
*/
Runtime.prototype._getSubstack = function (id) {
if (typeof this.blocks[id] === 'undefined') return null;
return this.blocks[id].fields['SUBSTACK'];
};
/**
* Helper to get the opcode for a particular block
* @param {?string} id ID of block to query
* @return {?string} the opcode corresponding to that block
*/
Runtime.prototype._getOpcode = function (id) {
if (typeof this.blocks[id] === 'undefined') return null;
return this.blocks[id].opcode;
};
module.exports = Runtime;