mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-06-26 14:20:21 -04:00
Implementation of timer > _, broadcast, broadcast and wait
This commit is contained in:
parent
b4cf64009f
commit
fe2ba2a536
1 changed files with 64 additions and 10 deletions
|
@ -1,3 +1,5 @@
|
|||
var Thread = require('../engine/thread');
|
||||
|
||||
function Scratch3EventBlocks(runtime) {
|
||||
/**
|
||||
* The runtime instantiating this block package.
|
||||
|
@ -12,23 +14,75 @@ function Scratch3EventBlocks(runtime) {
|
|||
*/
|
||||
Scratch3EventBlocks.prototype.getPrimitives = function() {
|
||||
return {
|
||||
'event_whenflagclicked': this.whenFlagClicked,
|
||||
'event_whenbroadcastreceived': this.whenBroadcastReceived,
|
||||
'event_broadcast': this.broadcast
|
||||
'event_broadcast': this.broadcast,
|
||||
'event_broadcastandwait': this.broadcastAndWait,
|
||||
'event_whengreaterthan': this.hatGreaterThanPredicate
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Scratch3EventBlocks.prototype.whenFlagClicked = function() {
|
||||
// No-op
|
||||
Scratch3EventBlocks.prototype.getHats = function () {
|
||||
return {
|
||||
'event_whenflagclicked': {
|
||||
restartExistingThreads: true
|
||||
},
|
||||
/*'event_whenkeypressed': {
|
||||
restartExistingThreads: false
|
||||
},
|
||||
'event_whenthisspriteclicked': {
|
||||
restartExistingThreads: true
|
||||
},
|
||||
'event_whenbackdropswitchesto': {
|
||||
restartExistingThreads: true
|
||||
},*/
|
||||
'event_whengreaterthan': {
|
||||
restartExistingThreads: false,
|
||||
edgeTriggered: true
|
||||
},
|
||||
'event_whenbroadcastreceived': {
|
||||
restartExistingThreads: true
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Scratch3EventBlocks.prototype.whenBroadcastReceived = function() {
|
||||
// No-op
|
||||
Scratch3EventBlocks.prototype.hatGreaterThanPredicate = function (args, util) {
|
||||
// @todo: Other cases :)
|
||||
if (args.WHENGREATERTHANMENU == 'TIMER') {
|
||||
return util.ioQuery('clock', 'projectTimer') > args.VALUE;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
Scratch3EventBlocks.prototype.broadcast = function() {
|
||||
// @todo
|
||||
Scratch3EventBlocks.prototype.broadcast = function(args, util) {
|
||||
util.triggerHats('event_whenbroadcastreceived', {
|
||||
'BROADCAST_OPTION': args.BROADCAST_OPTION
|
||||
});
|
||||
};
|
||||
|
||||
Scratch3EventBlocks.prototype.broadcastAndWait = function (args, util) {
|
||||
// Have we run before, triggering threads?
|
||||
if (!util.stackFrame.triggeredThreads) {
|
||||
// No - trigger hats for this broadcast.
|
||||
util.stackFrame.triggeredThreads = util.triggerHats(
|
||||
'event_whenbroadcastreceived', {
|
||||
'BROADCAST_OPTION': args.BROADCAST_OPTION
|
||||
}
|
||||
);
|
||||
if (util.stackFrame.triggeredThreads.length == 0) {
|
||||
// Nothing was started.
|
||||
return;
|
||||
}
|
||||
}
|
||||
// We've run before; check if the wait is still going on.
|
||||
var waiting = false;
|
||||
for (var i = 0; i < util.stackFrame.triggeredThreads.length; i++) {
|
||||
var thread = util.stackFrame.triggeredThreads[i];
|
||||
if (thread.status !== Thread.STATUS_DONE) {
|
||||
waiting = true;
|
||||
}
|
||||
}
|
||||
if (waiting) {
|
||||
util.yieldFrame();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Scratch3EventBlocks;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue