From f75577bcbe9d344bb2dc8f6dc238ec4a3e821fc3 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 24 Oct 2016 21:37:27 -0300 Subject: [PATCH] Implement '(attribute) of' sensing block (#311) * Implement '(attribute) of' sensing block * Remove unused util parameter --- src/blocks/scratch3_sensing.js | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/blocks/scratch3_sensing.js b/src/blocks/scratch3_sensing.js index be83d99c2..c56861d60 100644 --- a/src/blocks/scratch3_sensing.js +++ b/src/blocks/scratch3_sensing.js @@ -20,6 +20,7 @@ Scratch3SensingBlocks.prototype.getPrimitives = function () { sensing_distanceto: this.distanceTo, sensing_timer: this.getTimer, sensing_resettimer: this.resetTimer, + sensing_of: this.getAttributeOf, sensing_mousex: this.getMouseX, sensing_mousey: this.getMouseY, sensing_mousedown: this.getMouseDown, @@ -124,4 +125,47 @@ Scratch3SensingBlocks.prototype.daysSince2000 = function () { return mSecsSinceStart / msPerDay; }; +Scratch3SensingBlocks.prototype.getAttributeOf = function (args) { + var attrTarget; + + if (args.OBJECT === '_stage_') { + attrTarget = this.runtime.getTargetForStage(); + } else { + attrTarget = this.runtime.getSpriteTargetByName(args.OBJECT); + } + + // Generic attributes + if (attrTarget.isStage) { + switch (args.PROPERTY) { + // Scratch 1.4 support + case 'background #': return attrTarget.currentCostume + 1; + + case 'backdrop #': return attrTarget.currentCostume + 1; + case 'backdrop name': + return attrTarget.sprite.costumes[attrTarget.currentCostume].name; + case 'volume': return; // @todo: Keep this in mind for sound blocks! + } + } else { + switch (args.PROPERTY) { + case 'x position': return attrTarget.x; + case 'y position': return attrTarget.y; + case 'direction': return attrTarget.direction; + case 'costume #': return attrTarget.currentCostume + 1; + case 'costume name': + return attrTarget.sprite.costumes[attrTarget.currentCostume].name; + case 'size': return attrTarget.size; + case 'volume': return; // @todo: above, keep in mind for sound blocks.. + } + } + + // Variables + var varName = args.PROPERTY; + if (attrTarget.variables.hasOwnProperty(varName)) { + return attrTarget.variables[varName].value; + } + + // Otherwise, 0 + return 0; +}; + module.exports = Scratch3SensingBlocks;