Implement counter blocks

This commit is contained in:
Florrie 2018-04-07 16:02:49 -03:00
parent 2d9bd92140
commit 797d16be26
3 changed files with 54 additions and 1 deletions

View file

@ -7,6 +7,12 @@ class Scratch3ControlBlocks {
* @type {Runtime}
*/
this.runtime = runtime;
/**
* The "counter" block value. For compatibility with 2.0.
* @type {number}
*/
this._counter = 0;
}
/**
@ -25,7 +31,10 @@ class Scratch3ControlBlocks {
control_if_else: this.ifElse,
control_stop: this.stop,
control_create_clone_of: this.createClone,
control_delete_this_clone: this.deleteClone
control_delete_this_clone: this.deleteClone,
control_get_counter: this.getCounter,
control_incr_counter: this.incrCounter,
control_clear_counter: this.clearCounter
};
}
@ -146,6 +155,18 @@ class Scratch3ControlBlocks {
this.runtime.disposeTarget(util.target);
this.runtime.stopForTarget(util.target);
}
getCounter () {
return this._counter;
}
clearCounter () {
this._counter = 0;
}
incrCounter () {
this._counter++;
}
}
module.exports = Scratch3ControlBlocks;

View file

@ -815,6 +815,21 @@ const specMap = {
argMap: [
]
},
'COUNT': {
opcode: 'control_get_counter',
argMap: [
]
},
'INCR_COUNT': {
opcode: 'control_incr_counter',
argMap: [
]
},
'CLR_COUNT': {
opcode: 'control_clear_counter',
argMap: [
]
},
'touching:': {
opcode: 'sensing_touchingobject',
argMap: [

View file

@ -169,3 +169,20 @@ test('stop', t => {
t.strictEqual(state.stopThisScript, 1);
t.end();
});
test('counter, incrCounter, clearCounter', t => {
const rt = new Runtime();
const c = new Control(rt);
// Default value
t.strictEqual(c.getCounter(), 0);
c.incrCounter();
c.incrCounter();
t.strictEqual(c.getCounter(), 2);
c.clearCounter();
t.strictEqual(c.getCounter(), 0);
t.end();
});