Handle coordinate precision the same as Scratch 2 (#1722)

This commit is contained in:
Katie Broida 2018-11-15 15:50:56 -05:00 committed by GitHub
parent 2ca735eab4
commit bb82c46f8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 17 deletions

View file

@ -262,16 +262,25 @@ class Scratch3MotionBlocks {
} }
getX (args, util) { getX (args, util) {
return util.target.x; return this.limitPrecision(util.target.x);
} }
getY (args, util) { getY (args, util) {
return util.target.y; return this.limitPrecision(util.target.y);
} }
getDirection (args, util) { getDirection (args, util) {
return util.target.direction; return util.target.direction;
} }
// This corresponds to snapToInteger in Scratch 2
limitPrecision (coordinate) {
const rounded = Math.round(coordinate);
const delta = coordinate - rounded;
const limitedCoord = (Math.abs(delta) < 1e-9) ? rounded : coordinate;
return limitedCoord;
}
} }
module.exports = Scratch3MotionBlocks; module.exports = Scratch3MotionBlocks;

View file

@ -256,17 +256,6 @@ class RenderedTarget extends Target {
}; };
} }
/**
* Round a number to n digits
* @param {number} value The number to be rounded
* @param {number} places The number of decimal places to round to
* @return {number} The rounded number
*/
_roundCoord (value, places) {
const power = Math.pow(10, places);
return Math.round(value * power) / power;
}
/** /**
* Set the X and Y coordinates. * Set the X and Y coordinates.
* @param {!number} x New X coordinate, in Scratch coordinates. * @param {!number} x New X coordinate, in Scratch coordinates.
@ -280,8 +269,6 @@ class RenderedTarget extends Target {
const oldY = this.y; const oldY = this.y;
if (this.renderer) { if (this.renderer) {
const position = this.renderer.getFencedPositionOfDrawable(this.drawableID, [x, y]); const position = this.renderer.getFencedPositionOfDrawable(this.drawableID, [x, y]);
position[0] = this._roundCoord(position[0], 8);
position[1] = this._roundCoord(position[1], 8);
this.x = position[0]; this.x = position[0];
this.y = position[1]; this.y = position[1];
@ -293,8 +280,8 @@ class RenderedTarget extends Target {
this.runtime.requestRedraw(); this.runtime.requestRedraw();
} }
} else { } else {
this.x = this._roundCoord(x, 8); this.x = x;
this.y = this._roundCoord(y, 8); this.y = y;
} }
this.emit(RenderedTarget.EVENT_TARGET_MOVED, this, oldX, oldY, force); this.emit(RenderedTarget.EVENT_TARGET_MOVED, this, oldX, oldY, force);
this.runtime.requestTargetsUpdate(this); this.runtime.requestTargetsUpdate(this);

View file

@ -0,0 +1,26 @@
const test = require('tap').test;
const Motion = require('../../src/blocks/scratch3_motion');
const Runtime = require('../../src/engine/runtime');
const Sprite = require('../../src/sprites/sprite.js');
const RenderedTarget = require('../../src/sprites/rendered-target.js');
test('getPrimitives', t => {
const rt = new Runtime();
const motion = new Motion(rt);
t.type(motion.getPrimitives(), 'object');
t.end();
});
test('Coordinates have limited precision', t => {
const rt = new Runtime();
const motion = new Motion(rt);
const sprite = new Sprite(null, rt);
const target = new RenderedTarget(sprite, rt);
const util = {target};
motion.goToXY({X: 0.999999999, Y: 0.999999999}, util);
t.equals(motion.getX({}, util), 1);
t.equals(motion.getY({}, util), 1);
t.end();
});