Move tracking of edge activated hat values into target.

This commit is contained in:
Karishma Chadha 2018-12-12 12:05:55 -05:00
parent 19737d4e39
commit 6c4fb29523
4 changed files with 40 additions and 34 deletions

View file

@ -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;

View file

@ -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.<string, *>}
*/
this._edgeActivatedHatValues = {};
/**
* A list of script block IDs that were glowing during the previous frame.
* @type {!Array.<!string>}
@ -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();

View file

@ -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.<string, *>}
*/
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.

View file

@ -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();
});
});