mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-03-14 01:09:51 -04:00
Hacked implementation of broadcasts
This commit is contained in:
parent
326e03b5d2
commit
9363432456
4 changed files with 86 additions and 8 deletions
|
@ -57,14 +57,26 @@ Scratch3Blocks.prototype.stop = function() {
|
||||||
|
|
||||||
Scratch3Blocks.prototype.whenFlagClicked = function() {
|
Scratch3Blocks.prototype.whenFlagClicked = function() {
|
||||||
console.log('Running: event_whenflagclicked');
|
console.log('Running: event_whenflagclicked');
|
||||||
|
// No-op
|
||||||
};
|
};
|
||||||
|
|
||||||
Scratch3Blocks.prototype.whenBroadcastReceived = function() {
|
Scratch3Blocks.prototype.whenBroadcastReceived = function() {
|
||||||
console.log('Running: event_whenbroadcastreceived');
|
console.log('Running: event_whenbroadcastreceived');
|
||||||
|
// No-op
|
||||||
};
|
};
|
||||||
|
|
||||||
Scratch3Blocks.prototype.broadcast = function() {
|
Scratch3Blocks.prototype.broadcast = function(argValues, util) {
|
||||||
console.log('Running: event_broadcast');
|
console.log('Running: event_broadcast');
|
||||||
|
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;
|
module.exports = Scratch3Blocks;
|
||||||
|
|
|
@ -137,6 +137,32 @@ Sequencer.prototype.stepThread = function (thread) {
|
||||||
thread.stackFrames.pop();
|
thread.stackFrames.pop();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A callback for the primitive to start hats.
|
||||||
|
* @todo very hacked...
|
||||||
|
*/
|
||||||
|
var startHats = function(callback) {
|
||||||
|
for (var i = 0; i < instance.runtime.stacks.length; i++) {
|
||||||
|
var stack = instance.runtime.stacks[i];
|
||||||
|
var stackBlock = instance.runtime.blocks[stack];
|
||||||
|
var result = callback(stackBlock);
|
||||||
|
if (result) {
|
||||||
|
// Check if the stack is already running
|
||||||
|
var stackRunning = false;
|
||||||
|
|
||||||
|
for (var j = 0; j < instance.runtime.threads.length; j++) {
|
||||||
|
if (instance.runtime.threads[j].topBlock == stack) {
|
||||||
|
stackRunning = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!stackRunning) {
|
||||||
|
instance.runtime._pushThread(stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Record whether we have switched stack,
|
* Record whether we have switched stack,
|
||||||
* to avoid proceeding the thread automatically.
|
* to avoid proceeding the thread automatically.
|
||||||
|
@ -191,7 +217,8 @@ Sequencer.prototype.stepThread = function (thread) {
|
||||||
done: threadDoneCallback,
|
done: threadDoneCallback,
|
||||||
timeout: YieldTimers.timeout,
|
timeout: YieldTimers.timeout,
|
||||||
stackFrame: currentStackFrame,
|
stackFrame: currentStackFrame,
|
||||||
startSubstack: threadStartSubstack
|
startSubstack: threadStartSubstack,
|
||||||
|
startHats: startHats
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch(e) {
|
catch(e) {
|
||||||
|
|
43
vm.js
43
vm.js
|
@ -1702,6 +1702,32 @@
|
||||||
thread.stackFrames.pop();
|
thread.stackFrames.pop();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A callback for the primitive to start hats.
|
||||||
|
* @todo very hacked...
|
||||||
|
*/
|
||||||
|
var startHats = function(callback) {
|
||||||
|
for (var i = 0; i < instance.runtime.stacks.length; i++) {
|
||||||
|
var stack = instance.runtime.stacks[i];
|
||||||
|
var stackBlock = instance.runtime.blocks[stack];
|
||||||
|
var result = callback(stackBlock);
|
||||||
|
if (result) {
|
||||||
|
// Check if the stack is already running
|
||||||
|
var stackRunning = false;
|
||||||
|
|
||||||
|
for (var j = 0; j < instance.runtime.threads.length; j++) {
|
||||||
|
if (instance.runtime.threads[j].topBlock == stack) {
|
||||||
|
stackRunning = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!stackRunning) {
|
||||||
|
instance.runtime._pushThread(stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Record whether we have switched stack,
|
* Record whether we have switched stack,
|
||||||
* to avoid proceeding the thread automatically.
|
* to avoid proceeding the thread automatically.
|
||||||
|
@ -1756,7 +1782,8 @@
|
||||||
done: threadDoneCallback,
|
done: threadDoneCallback,
|
||||||
timeout: YieldTimers.timeout,
|
timeout: YieldTimers.timeout,
|
||||||
stackFrame: currentStackFrame,
|
stackFrame: currentStackFrame,
|
||||||
startSubstack: threadStartSubstack
|
startSubstack: threadStartSubstack,
|
||||||
|
startHats: startHats
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch(e) {
|
catch(e) {
|
||||||
|
@ -2038,14 +2065,26 @@
|
||||||
|
|
||||||
Scratch3Blocks.prototype.whenFlagClicked = function() {
|
Scratch3Blocks.prototype.whenFlagClicked = function() {
|
||||||
console.log('Running: event_whenflagclicked');
|
console.log('Running: event_whenflagclicked');
|
||||||
|
// No-op
|
||||||
};
|
};
|
||||||
|
|
||||||
Scratch3Blocks.prototype.whenBroadcastReceived = function() {
|
Scratch3Blocks.prototype.whenBroadcastReceived = function() {
|
||||||
console.log('Running: event_whenbroadcastreceived');
|
console.log('Running: event_whenbroadcastreceived');
|
||||||
|
// No-op
|
||||||
};
|
};
|
||||||
|
|
||||||
Scratch3Blocks.prototype.broadcast = function() {
|
Scratch3Blocks.prototype.broadcast = function(argValues, util) {
|
||||||
console.log('Running: event_broadcast');
|
console.log('Running: event_broadcast');
|
||||||
|
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;
|
module.exports = Scratch3Blocks;
|
||||||
|
|
8
vm.min.js
vendored
8
vm.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue