From fbb6d63e0338f681bd624e49f28bd10535ca72c8 Mon Sep 17 00:00:00 2001 From: Florrie Date: Fri, 28 Dec 2018 10:39:42 -0400 Subject: [PATCH] Clamp ghost effect Cherry-picked from 35c88904253f7f8ef494a32c2644157fbe4d45cb (#1887) --- src/blocks/scratch3_looks.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/blocks/scratch3_looks.js b/src/blocks/scratch3_looks.js index ebf40e555..94d70ea5b 100644 --- a/src/blocks/scratch3_looks.js +++ b/src/blocks/scratch3_looks.js @@ -4,6 +4,7 @@ const RenderedTarget = require('../sprites/rendered-target'); const uid = require('../util/uid'); const StageLayering = require('../engine/stage-layering'); const getMonitorIdForBlockWithArgs = require('../util/get-monitor-id'); +const MathUtil = require('../util/math-util'); /** * @typedef {object} BubbleState - the bubble state associated with a particular target. @@ -67,6 +68,14 @@ class Scratch3LooksBlocks { return 330; } + /** + * Limit for ghost effect + * @const {object} + */ + static get EFFECT_GHOST_LIMIT (){ + return {min: 0, max: 100}; + } + /** * @param {Target} target - collect bubble state for this target. Probably, but not necessarily, a RenderedTarget. * @returns {BubbleState} the mutable bubble state associated with that target. This will be created if necessary. @@ -479,13 +488,23 @@ class Scratch3LooksBlocks { const effect = Cast.toString(args.EFFECT).toLowerCase(); const change = Cast.toNumber(args.CHANGE); if (!util.target.effects.hasOwnProperty(effect)) return; - const newValue = change + util.target.effects[effect]; + let newValue = change + util.target.effects[effect]; + if (effect === 'ghost') { + newValue = MathUtil.clamp(newValue, + Scratch3LooksBlocks.EFFECT_GHOST_LIMIT.min, + Scratch3LooksBlocks.EFFECT_GHOST_LIMIT.max); + } util.target.setEffect(effect, newValue); } setEffect (args, util) { const effect = Cast.toString(args.EFFECT).toLowerCase(); - const value = Cast.toNumber(args.VALUE); + let value = Cast.toNumber(args.VALUE); + if (effect === 'ghost') { + value = MathUtil.clamp(value, + Scratch3LooksBlocks.EFFECT_GHOST_LIMIT.min, + Scratch3LooksBlocks.EFFECT_GHOST_LIMIT.max); + } util.target.setEffect(effect, value); }