From 6c4fb295230533fb940d351a160bfc06c7ef6f04 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Wed, 12 Dec 2018 12:05:55 -0500 Subject: [PATCH] Move tracking of edge activated hat values into target. --- src/engine/execute.js | 3 +- src/engine/runtime.js | 29 +------------------ src/engine/target.js | 26 +++++++++++++++++ .../hat-threads-run-every-frame.js | 16 +++++++--- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/engine/execute.js b/src/engine/execute.js index 98f85b9b4..9fcc2470a 100644 --- a/src/engine/execute.js +++ b/src/engine/execute.js @@ -63,9 +63,8 @@ const handleReport = function (resolvedValue, sequencer, thread, blockCached, la // true and used to be false, or the stack was activated explicitly // via stack click if (!thread.stackClick) { - const oldEdgeValue = sequencer.runtime.updateEdgeActivatedValue( + const oldEdgeValue = thread.target.updateEdgeActivatedValue( currentBlockId, - thread.target.id, resolvedValue ); const edgeWasActivated = !oldEdgeValue && resolvedValue; diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 978af6ef6..417ded762 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -221,13 +221,6 @@ class Runtime extends EventEmitter { */ this._hats = {}; - /** - * Currently known values for edge-activated hats. - * Keys are block ID for the hat; values are the currently known values. - * @type {Object.} - */ - this._edgeActivatedHatValues = {}; - /** * A list of script block IDs that were glowing during the previous frame. * @type {!Array.} @@ -1230,26 +1223,6 @@ class Runtime extends EventEmitter { this._hats[opcode].edgeActivated; } - /** - * Update an edge-activated hat block value. - * @param {!string} blockId ID of hat to store value for. - * @param {!string} threadTargetId Target ID for the thread that the block belongs to - * @param {*} newValue Value to store for edge-activated hat. - * @return {*} The old value for the edge-activated hat. - */ - updateEdgeActivatedValue (blockId, threadTargetId, newValue) { - const blockAndTargetId = `${blockId}_${threadTargetId}`; - const oldValue = this._edgeActivatedHatValues[blockAndTargetId]; - this._edgeActivatedHatValues[blockAndTargetId] = newValue; - return oldValue; - } - - /** - * Clear all edge-activaed hat values. - */ - clearEdgeActivatedValues () { - this._edgeActivatedHatValues = {}; - } /** * Attach the audio engine @@ -1689,7 +1662,7 @@ class Runtime extends EventEmitter { this.stopAll(); this.emit(Runtime.PROJECT_START); this.ioDevices.clock.resetProjectTimer(); - this.clearEdgeActivatedValues(); + this.targets.forEach(target => target.clearEdgeActivatedValues()); // Inform all targets of the green flag. for (let i = 0; i < this.targets.length; i++) { this.targets[i].onGreenFlag(); diff --git a/src/engine/target.js b/src/engine/target.js index d4b04209d..5b7b3d68f 100644 --- a/src/engine/target.js +++ b/src/engine/target.js @@ -63,6 +63,13 @@ class Target extends EventEmitter { */ this._customState = {}; + /** + * Currently known values for edge-activated hats. + * Keys are block ID for the hat; values are the currently known values. + * @type {Object.} + */ + this._edgeActivatedHatValues = {}; + if (this.runtime) { this.runtime.addExecutable(this); } @@ -84,6 +91,25 @@ class Target extends EventEmitter { return this.id; } + /** + * Update an edge-activated hat block value. + * @param {!string} blockId ID of hat to store value for. + * @param {*} newValue Value to store for edge-activated hat. + * @return {*} The old value for the edge-activated hat. + */ + updateEdgeActivatedValue (blockId, newValue) { + const oldValue = this._edgeActivatedHatValues[blockId]; + this._edgeActivatedHatValues[blockId] = newValue; + return oldValue; + } + + /** + * Clear all edge-activaed hat values. + */ + clearEdgeActivatedValues () { + this._edgeActivatedHatValues = {}; + } + /** * Look up a variable object, first by id, and then by name if the id is not found. * Create a new variable if both lookups fail. diff --git a/test/integration/hat-threads-run-every-frame.js b/test/integration/hat-threads-run-every-frame.js index 80a4711cf..a31f260dd 100644 --- a/test/integration/hat-threads-run-every-frame.js +++ b/test/integration/hat-threads-run-every-frame.js @@ -137,14 +137,18 @@ test('edge activated hat should trigger for both sprites when sprite is duplicat // Run execute on the thread to populate the runtime's // _edgeActivatedHatValues object execute(vm.runtime.sequencer, vm.runtime.threads[0]); - t.equal(Object.keys(vm.runtime._edgeActivatedHatValues).length, 1); + let numTargetEdgeHats = vm.runtime.targets.reduce((val, target) => + val + Object.keys(target._edgeActivatedHatValues).length, 0); + t.equal(numTargetEdgeHats, 1); vm.duplicateSprite(vm.runtime.targets[1].id).then(() => { vm.runtime._step(); // Check that the runtime's _edgeActivatedHatValues object has two separate keys // after execute is run on each thread vm.runtime.threads.forEach(thread => execute(vm.runtime.sequencer, thread)); - t.equal(Object.keys(vm.runtime._edgeActivatedHatValues).length, 2); + numTargetEdgeHats = vm.runtime.targets.reduce((val, target) => + val + Object.keys(target._edgeActivatedHatValues).length, 0); + t.equal(numTargetEdgeHats, 2); t.end(); }); @@ -184,7 +188,9 @@ test('edge activated hat should trigger for both sprites when sprite is cloned', // Run execute on the thread to populate the runtime's // _edgeActivatedHatValues object execute(vm.runtime.sequencer, vm.runtime.threads[0]); - t.equal(Object.keys(vm.runtime._edgeActivatedHatValues).length, 1); + let numTargetEdgeHats = vm.runtime.targets.reduce((val, target) => + val + Object.keys(target._edgeActivatedHatValues).length, 0); + t.equal(numTargetEdgeHats, 1); vm.runtime.targets.push(vm.runtime.targets[1].makeClone()); @@ -192,7 +198,9 @@ test('edge activated hat should trigger for both sprites when sprite is cloned', // Check that the runtime's _edgeActivatedHatValues object has two separate keys // after execute is run on each thread vm.runtime.threads.forEach(thread => execute(vm.runtime.sequencer, thread)); - t.equal(Object.keys(vm.runtime._edgeActivatedHatValues).length, 2); + numTargetEdgeHats = vm.runtime.targets.reduce((val, target) => + val + Object.keys(target._edgeActivatedHatValues).length, 0); + t.equal(numTargetEdgeHats, 2); t.end(); }); });