mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
Add tilt blocks
This commit is contained in:
parent
ba548701e2
commit
a19585af75
1 changed files with 56 additions and 7 deletions
|
@ -85,6 +85,12 @@ const FREEFALL_THRESHOLD = 0.5;
|
||||||
*/
|
*/
|
||||||
const FREEFALL_ROTATION_FACTOR = 0.3;
|
const FREEFALL_ROTATION_FACTOR = 0.3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Threshold in degrees for reporting that the sensor is tilted.
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
const TILT_THRESHOLD = 15;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Acceleration due to gravity, in m/s^2.
|
* Acceleration due to gravity, in m/s^2.
|
||||||
* @type {number}
|
* @type {number}
|
||||||
|
@ -627,6 +633,22 @@ class Scratch3GdxForBlocks {
|
||||||
blockType: BlockType.REPORTER
|
blockType: BlockType.REPORTER
|
||||||
},
|
},
|
||||||
'---',
|
'---',
|
||||||
|
{
|
||||||
|
opcode: 'whenTilted',
|
||||||
|
text: formatMessage({
|
||||||
|
id: 'gdxfor.whenTilted',
|
||||||
|
default: 'when tilted [TILT]',
|
||||||
|
description: 'when the sensor detects tilt'
|
||||||
|
}),
|
||||||
|
blockType: BlockType.HAT,
|
||||||
|
arguments: {
|
||||||
|
TILT: {
|
||||||
|
type: ArgumentType.STRING,
|
||||||
|
menu: 'tiltOptions',
|
||||||
|
defaultValue: TiltAxisValues.FRONT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
opcode: 'whenGesture',
|
opcode: 'whenGesture',
|
||||||
text: formatMessage({
|
text: formatMessage({
|
||||||
|
@ -716,7 +738,22 @@ class Scratch3GdxForBlocks {
|
||||||
description: 'is the device in free fall?'
|
description: 'is the device in free fall?'
|
||||||
}),
|
}),
|
||||||
blockType: BlockType.BOOLEAN
|
blockType: BlockType.BOOLEAN
|
||||||
|
},
|
||||||
|
{
|
||||||
|
opcode: 'isTilted',
|
||||||
|
text: formatMessage({
|
||||||
|
id: 'gdxfor.isTilted',
|
||||||
|
default: 'tilted [TILT]?',
|
||||||
|
description: 'is the device tilted?'
|
||||||
|
}),
|
||||||
|
blockType: BlockType.BOOLEAN,
|
||||||
|
arguments: {
|
||||||
|
TILT: {
|
||||||
|
type: ArgumentType.STRING,
|
||||||
|
menu: 'tiltOptions',
|
||||||
|
defaultValue: TiltAxisValues.FRONT
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
menus: {
|
menus: {
|
||||||
|
@ -757,24 +794,36 @@ class Scratch3GdxForBlocks {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
whenTilted (args) {
|
||||||
|
return this._getTiltAngle(args.TILT) > TILT_THRESHOLD;
|
||||||
|
}
|
||||||
|
|
||||||
getTilt (args) {
|
getTilt (args) {
|
||||||
|
return this._getTiltAngle(args.TILT);
|
||||||
|
}
|
||||||
|
|
||||||
|
isTilted (args) {
|
||||||
|
return this._getTiltAngle(args.TILT) > TILT_THRESHOLD;
|
||||||
|
}
|
||||||
|
|
||||||
|
_getTiltAngle (direction) {
|
||||||
// Tilt values are calculated using acceleration due to gravity,
|
// Tilt values are calculated using acceleration due to gravity,
|
||||||
// so we need to return 0 when the peripheral is not connected.
|
// so we need to return 0 when the peripheral is not connected.
|
||||||
if (!this._peripheral.isConnected()) {
|
if (!this._peripheral.isConnected()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (args.TILT) {
|
switch (direction) {
|
||||||
case TiltAxisValues.FRONT:
|
case TiltAxisValues.FRONT:
|
||||||
return Math.round(this._peripheral.getTiltFrontBack(false));
|
|
||||||
case TiltAxisValues.BACK:
|
|
||||||
return Math.round(this._peripheral.getTiltFrontBack(true));
|
return Math.round(this._peripheral.getTiltFrontBack(true));
|
||||||
|
case TiltAxisValues.BACK:
|
||||||
|
return Math.round(this._peripheral.getTiltFrontBack(false));
|
||||||
case TiltAxisValues.LEFT:
|
case TiltAxisValues.LEFT:
|
||||||
return Math.round(this._peripheral.getTiltLeftRight(false));
|
|
||||||
case TiltAxisValues.RIGHT:
|
|
||||||
return Math.round(this._peripheral.getTiltLeftRight(true));
|
return Math.round(this._peripheral.getTiltLeftRight(true));
|
||||||
|
case TiltAxisValues.RIGHT:
|
||||||
|
return Math.round(this._peripheral.getTiltLeftRight(false));
|
||||||
default:
|
default:
|
||||||
log.warn(`Unknown direction in getTilt: ${args.TILT}`);
|
log.warn(`Unknown direction in getTilt: ${direction}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue