// Main execute loop for a script. Note that all of the logic is built into tile.eval() and tile.continue()
this.executeScriptBody = function() {
for(; this.activeTileNumber < this.tiles.length; this.activeTileNumber++) {
var tile = this.tiles[this.activeTileNumber]
tile.eval()
if (!tile.continue(this)) return;
}
}
// A repeat_n block has two tiles: a startTile and an endTile. It's an object like this:
{
countLimit: aNumber, // when this counter expires. This must be >= 0
counter: 0, // current value of the counter. Initially, 0
script: scriptContainingThisTile, // object which is the script containing this tile
startTile: { // The start tile of the block. Just sets the counter to 0 on eval
eval: function() {
counter = 0;
},
continue: function(aScript) {
return true; // always execute the next tile
}
},
endTile: { // the end tile of the block. increments the counter and checks to see what the next tile number should be
eval: function() {
counter++;
if (counter == countLimit()) {
script.activeTileNumber = script.tileNumber(this) + 1;
} else {
script.activeTileNumber = script.tileNumber(startTile) + 1;
}
}
continue: function() {
return false;
}
}
}
// A repeat_until block is very similar to a repeat_n block. The key difference is that it checks to see if the ending event has occured. It's an object like this:
{
event: anEvent; // the event
script: scriptContainingThisTile, // object which is the script containing this tile
startTile: { // The start tile of the block.
eval: function() {
},
continue: function(aScript) {
return true; // always execute the next tile
}
},
endTile: { // the end tile of the block. checks the event to see what the next tile number should be
eval: function() {
var done = event.hasOccured();
if (done) {
script.activeTileNumber = script.tileNumber(this) + 1;
} else {
script.activeTileNumber = script.tileNumber(startTile) + 1;
}
}
continue: function() {
return false;
}
}
}
// A wait block just waits for a timer. eval() of the startTile just calculates the end time; continue()
// of the endTile just determines if the time has passed
{
duration: aTimeInMilliseconds, // duration of the wait -- must be > 0 (in fact, must be a multiple of the basic iteration time of this loop
endTime: aTimeInMilliseconds, // when the timer ends
startTile: {
eval: function() {
endTime = Date.now() + 1000
}
continue: function() {
return true;
}
},
endTile: {
eval: function() {}
continue: function() {
return Date.now() >= endTime
}
}
}
// wait_event is just as simple
{
event: anEvent; // the event to wait on
startTile: {
eval: function() {
}
continue: function() {
return true;
}
},
endTile: {
eval: function() {}
continue: function() {
return event.hasOccured()
}
}
}
// Finally, forever is just a simplification of repeat:
{
script: scriptContainingThisTile, // object which is the script containing this tile
startTile: { // The start tile of the block.
eval: function() {
},
continue: function(aScript) {
return true; // always execute the next tile
}
},
endTile: { // the end tile of the block. sets the next tile to the first tile after the start block
eval: function() {
script.activeTileNumber = script.tileNumber(startTile) + 1;
}
continue: function() {
return false;
}
}
}