2016-09-12 10:58:50 -04:00
|
|
|
var Cast = require('../util/cast');
|
|
|
|
|
2016-08-15 21:37:36 -04:00
|
|
|
function Scratch3SensingBlocks(runtime) {
|
|
|
|
/**
|
|
|
|
* The runtime instantiating this block package.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the block primitives implemented by this package.
|
|
|
|
* @return {Object.<string, Function>} Mapping of opcode to Function.
|
|
|
|
*/
|
|
|
|
Scratch3SensingBlocks.prototype.getPrimitives = function() {
|
|
|
|
return {
|
2016-10-17 23:17:55 -04:00
|
|
|
'sensing_touchingobject': this.touchingObject,
|
2016-09-12 10:58:50 -04:00
|
|
|
'sensing_touchingcolor': this.touchingColor,
|
|
|
|
'sensing_coloristouchingcolor': this.colorTouchingColor,
|
2016-10-04 21:26:59 -04:00
|
|
|
'sensing_distanceto': this.distanceTo,
|
2016-08-15 21:37:36 -04:00
|
|
|
'sensing_timer': this.getTimer,
|
|
|
|
'sensing_resettimer': this.resetTimer,
|
|
|
|
'sensing_mousex': this.getMouseX,
|
|
|
|
'sensing_mousey': this.getMouseY,
|
2016-09-01 23:48:14 -04:00
|
|
|
'sensing_mousedown': this.getMouseDown,
|
2016-09-02 11:23:09 -04:00
|
|
|
'sensing_keypressed': this.getKeyPressed,
|
2016-10-17 13:57:25 -04:00
|
|
|
'sensing_current': this.current,
|
|
|
|
'sensing_dayssince2000': this.daysSince2000
|
2016-08-15 21:37:36 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-10-17 23:17:55 -04:00
|
|
|
Scratch3SensingBlocks.prototype.touchingObject = function (args, util) {
|
|
|
|
var requestedObject = args.TOUCHINGOBJECTMENU;
|
|
|
|
if (requestedObject == '_mouse_') {
|
|
|
|
var mouseX = util.ioQuery('mouse', 'getX');
|
|
|
|
var mouseY = util.ioQuery('mouse', 'getY');
|
|
|
|
return util.target.isTouchingPoint(mouseX, mouseY);
|
|
|
|
} else if (requestedObject == '_edge_') {
|
|
|
|
return util.target.isTouchingEdge();
|
|
|
|
} else {
|
|
|
|
return util.target.isTouchingSprite(requestedObject);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-12 10:58:50 -04:00
|
|
|
Scratch3SensingBlocks.prototype.touchingColor = function (args, util) {
|
|
|
|
var color = Cast.toRgbColorList(args.COLOR);
|
|
|
|
return util.target.isTouchingColor(color);
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SensingBlocks.prototype.colorTouchingColor = function (args, util) {
|
|
|
|
var maskColor = Cast.toRgbColorList(args.COLOR);
|
|
|
|
var targetColor = Cast.toRgbColorList(args.COLOR2);
|
|
|
|
return util.target.colorIsTouchingColor(targetColor, maskColor);
|
|
|
|
};
|
|
|
|
|
2016-10-04 21:26:59 -04:00
|
|
|
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));
|
|
|
|
};
|
|
|
|
|
2016-08-15 21:37:36 -04:00
|
|
|
Scratch3SensingBlocks.prototype.getTimer = function (args, util) {
|
|
|
|
return util.ioQuery('clock', 'projectTimer');
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SensingBlocks.prototype.resetTimer = function (args, util) {
|
|
|
|
util.ioQuery('clock', 'resetProjectTimer');
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SensingBlocks.prototype.getMouseX = function (args, util) {
|
|
|
|
return util.ioQuery('mouse', 'getX');
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SensingBlocks.prototype.getMouseY = function (args, util) {
|
|
|
|
return util.ioQuery('mouse', 'getY');
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SensingBlocks.prototype.getMouseDown = function (args, util) {
|
|
|
|
return util.ioQuery('mouse', 'getIsDown');
|
|
|
|
};
|
|
|
|
|
2016-09-02 00:01:17 -04:00
|
|
|
Scratch3SensingBlocks.prototype.current = function (args) {
|
2016-09-12 13:14:16 -04:00
|
|
|
var menuOption = Cast.toString(args.CURRENTMENU).toLowerCase();
|
2016-09-01 23:48:14 -04:00
|
|
|
var date = new Date();
|
2016-09-12 13:14:16 -04:00
|
|
|
switch (menuOption) {
|
2016-09-02 00:01:17 -04:00
|
|
|
case 'year': return date.getFullYear();
|
|
|
|
case 'month': return date.getMonth() + 1; // getMonth is zero-based
|
|
|
|
case 'date': return date.getDate();
|
2016-09-02 09:20:54 -04:00
|
|
|
case 'dayofweek': return date.getDay() + 1; // getDay is zero-based, Sun=0
|
2016-09-02 00:01:17 -04:00
|
|
|
case 'hour': return date.getHours();
|
|
|
|
case 'minute': return date.getMinutes();
|
|
|
|
case 'second': return date.getSeconds();
|
2016-09-01 23:48:14 -04:00
|
|
|
}
|
2016-09-12 13:14:16 -04:00
|
|
|
return 0;
|
2016-09-02 11:23:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3SensingBlocks.prototype.getKeyPressed = function (args, util) {
|
2016-09-07 15:57:53 -04:00
|
|
|
return util.ioQuery('keyboard', 'getKeyIsDown', args.KEY_OPTION);
|
2016-09-02 11:23:09 -04:00
|
|
|
};
|
|
|
|
|
2016-10-17 13:57:25 -04:00
|
|
|
Scratch3SensingBlocks.prototype.daysSince2000 = function()
|
|
|
|
{
|
|
|
|
var msPerDay = 24 * 60 * 60 * 1000;
|
|
|
|
var start = new Date(2000, 1-1, 1);
|
|
|
|
var today = new Date();
|
|
|
|
var dstAdjust = today.getTimezoneOffset() - start.getTimezoneOffset();
|
|
|
|
var mSecsSinceStart = today.valueOf() - start.valueOf();
|
|
|
|
mSecsSinceStart += ((today.getTimezoneOffset() - dstAdjust) * 60 * 1000);
|
|
|
|
return mSecsSinceStart / msPerDay;
|
|
|
|
};
|
|
|
|
|
2016-08-15 21:37:36 -04:00
|
|
|
module.exports = Scratch3SensingBlocks;
|