Skip to content

Commit

Permalink
Implement "distance to" block (#239)
Browse files Browse the repository at this point in the history
* Implement "distance-to" block

* distance-to in stage should always be 10000
  • Loading branch information
Liam authored and tmickel committed Oct 5, 2016
1 parent 9b07889 commit 30535d8
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/blocks/scratch3_sensing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
};
Expand Down

0 comments on commit 30535d8

Please sign in to comment.