mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-11 10:39:56 -05:00
Add whenMotionGreaterThan hat block to video sensing ext
This commit is contained in:
parent
cfd74a7c7f
commit
e65b7985cb
1 changed files with 45 additions and 34 deletions
|
@ -272,6 +272,19 @@ class Scratch3VideoSensingBlocks {
|
||||||
defaultValue: 1
|
defaultValue: 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// @todo this hat needs to be set itself to restart existing
|
||||||
|
// threads like Scratch 2's behaviour.
|
||||||
|
opcode: 'whenMotionGreaterThan',
|
||||||
|
text: 'when video motion > [REFERENCE]',
|
||||||
|
blockType: BlockType.HAT,
|
||||||
|
arguments: {
|
||||||
|
REFERENCE: {
|
||||||
|
type: ArgumentType.NUMBER,
|
||||||
|
defaultValue: 10
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
menus: {
|
menus: {
|
||||||
|
@ -281,14 +294,32 @@ class Scratch3VideoSensingBlocks {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Analyze a part of the frame that a target overlaps.
|
||||||
|
* @param {Target} target - a target to determine where to analyze
|
||||||
|
* @returns {MotionState} the motion state for the given target
|
||||||
|
*/
|
||||||
|
_analyzeLocalMotion (target) {
|
||||||
|
const drawable = this.runtime.renderer._allDrawables[target.drawableID];
|
||||||
|
const state = this._getMotionState(target);
|
||||||
|
this.detect.getLocalMotion(drawable, state);
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A scratch reporter block handle that analyzes the last two frames and
|
||||||
|
* depending on the arguments, returns the motion or direction for the
|
||||||
|
* whole stage or just the target sprite.
|
||||||
|
* @param {object} args - the block arguments
|
||||||
|
* @param {BlockUtility} util - the block utility
|
||||||
|
* @returns {number} the motion amount or direction of the stage or sprite
|
||||||
|
*/
|
||||||
videoOn (args, util) {
|
videoOn (args, util) {
|
||||||
this.detect.analyzeFrame();
|
this.detect.analyzeFrame();
|
||||||
|
|
||||||
let state = this.detect;
|
let state = this.detect;
|
||||||
if (Number(args.STAGE_SPRITE) === 2) {
|
if (Number(args.STAGE_SPRITE) === 2) {
|
||||||
const drawable = this.runtime.renderer._allDrawables[util.target.drawableID];
|
state = this._analyzeLocalMotion(util.target);
|
||||||
state = this._getMotionState(util.target);
|
|
||||||
this.detect.getLocalMotion(drawable, state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Number(args.MOTION_DIRECTION) === 1) {
|
if (Number(args.MOTION_DIRECTION) === 1) {
|
||||||
|
@ -298,38 +329,18 @@ class Scratch3VideoSensingBlocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the stack timer needs initialization.
|
* A scratch hat block edge handle that analyzes the last two frames where
|
||||||
* @param {object} util - utility object provided by the runtime.
|
* the target sprite overlaps and if it has more motion than the given
|
||||||
* @return {boolean} - true if the stack timer needs to be initialized.
|
* reference value.
|
||||||
* @private
|
* @param {object} args - the block arguments
|
||||||
|
* @param {BlockUtility} util - the block utility
|
||||||
|
* @returns {boolean} true if the sprite overlaps more motion than the
|
||||||
|
* reference
|
||||||
*/
|
*/
|
||||||
_stackTimerNeedsInit (util) {
|
whenMotionGreaterThan (args, util) {
|
||||||
return !util.stackFrame.timer;
|
this.detect.analyzeFrame();
|
||||||
}
|
const state = this._analyzeLocalMotion(util.target);
|
||||||
|
return state.motionAmount > Number(args.REFERENCE);
|
||||||
/**
|
|
||||||
* Start the stack timer and the yield the thread if necessary.
|
|
||||||
* @param {object} util - utility object provided by the runtime.
|
|
||||||
* @param {number} duration - a duration in seconds to set the timer for.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
_startStackTimer (util, duration) {
|
|
||||||
util.stackFrame.timer = new Timer();
|
|
||||||
util.stackFrame.timer.start();
|
|
||||||
util.stackFrame.duration = duration;
|
|
||||||
util.yield();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check the stack timer, and if its time is not up yet, yield the thread.
|
|
||||||
* @param {object} util - utility object provided by the runtime.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
_checkStackTimer (util) {
|
|
||||||
const timeElapsed = util.stackFrame.timer.timeElapsed();
|
|
||||||
if (timeElapsed < util.stackFrame.duration * 1000) {
|
|
||||||
util.yield();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue