From 30535d8e6ea4f2cbb6c7c1fe51db433d0fca6b3d Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 4 Oct 2016 22:26:59 -0300 Subject: [PATCH] Implement "distance to" block (#239) * Implement "distance-to" block * distance-to in stage should always be 10000 --- src/blocks/scratch3_sensing.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/blocks/scratch3_sensing.js b/src/blocks/scratch3_sensing.js index 5e392ef86..15466e3c0 100644 --- a/src/blocks/scratch3_sensing.js +++ b/src/blocks/scratch3_sensing.js @@ -16,6 +16,7 @@ Scratch3SensingBlocks.prototype.getPrimitives = function() { return { 'sensing_touchingcolor': this.touchingColor, 'sensing_coloristouchingcolor': this.colorTouchingColor, + 'sensing_distanceto': this.distanceTo, 'sensing_timer': this.getTimer, 'sensing_resettimer': this.resetTimer, 'sensing_mousex': this.getMouseX, @@ -37,6 +38,28 @@ Scratch3SensingBlocks.prototype.colorTouchingColor = function (args, util) { return util.target.colorIsTouchingColor(targetColor, maskColor); }; +Scratch3SensingBlocks.prototype.distanceTo = function (args, util) { + if (util.target.isStage) return 10000; + + var targetX = 0; + var targetY = 0; + if (args.DISTANCETOMENU === '_mouse_') { + targetX = util.ioQuery('mouse', 'getX'); + targetY = util.ioQuery('mouse', 'getY'); + } else { + var distTarget = this.runtime.getSpriteTargetByName( + args.DISTANCETOMENU + ); + if (!distTarget) return 10000; + targetX = distTarget.x; + targetY = distTarget.y; + } + + var dx = util.target.x - targetX; + var dy = util.target.y - targetY; + return Math.sqrt((dx * dx) + (dy * dy)); +}; + Scratch3SensingBlocks.prototype.getTimer = function (args, util) { return util.ioQuery('clock', 'projectTimer'); };