mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 23:12:24 -05:00
Make SWITCH COSTUME and SWITCH BACKDROP compatible with 2.0
This commit is contained in:
parent
c81f7ee9a6
commit
3ef61e2248
2 changed files with 61 additions and 38 deletions
|
@ -344,65 +344,86 @@ class Scratch3LooksBlocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility function to set the costume or backdrop of a target.
|
* Utility function to set the costume of a target.
|
||||||
* Matches the behavior of Scratch 2.0 for different types of arguments.
|
* Matches the behavior of Scratch 2.0 for different types of arguments.
|
||||||
* @param {!Target} target Target to set costume/backdrop to.
|
* @param {!Target} target Target to set costume to.
|
||||||
* @param {Any} requestedCostume Costume requested, e.g., 0, 'name', etc.
|
* @param {Any} requestedCostume Costume requested, e.g., 0, 'name', etc.
|
||||||
* @param {boolean=} optZeroIndex Set to zero-index the requestedCostume.
|
* @param {boolean=} optZeroIndex Set to zero-index the requestedCostume.
|
||||||
* @return {Array.<!Thread>} Any threads started by this switch.
|
* @return {Array.<!Thread>} Any threads started by this switch.
|
||||||
*/
|
*/
|
||||||
_setCostumeOrBackdrop (target,
|
_setCostume (target, requestedCostume, optZeroIndex) {
|
||||||
requestedCostume, optZeroIndex) {
|
|
||||||
if (typeof requestedCostume === 'number') {
|
if (typeof requestedCostume === 'number') {
|
||||||
target.setCostume(optZeroIndex ?
|
target.setCostume(optZeroIndex ? requestedCostume : requestedCostume - 1);
|
||||||
requestedCostume : requestedCostume - 1);
|
|
||||||
} else {
|
} else {
|
||||||
const costumeIndex = target.getCostumeIndexByName(requestedCostume);
|
const costumeIndex = target.getCostumeIndexByName(requestedCostume.toString());
|
||||||
if (costumeIndex > -1) {
|
|
||||||
|
if (costumeIndex !== -1) {
|
||||||
target.setCostume(costumeIndex);
|
target.setCostume(costumeIndex);
|
||||||
} else if (requestedCostume === 'previous costume' ||
|
} else if (requestedCostume === 'next costume') {
|
||||||
requestedCostume === 'previous backdrop') {
|
|
||||||
target.setCostume(target.currentCostume - 1);
|
|
||||||
} else if (requestedCostume === 'next costume' ||
|
|
||||||
requestedCostume === 'next backdrop') {
|
|
||||||
target.setCostume(target.currentCostume + 1);
|
target.setCostume(target.currentCostume + 1);
|
||||||
} else if (requestedCostume === 'random backdrop') {
|
} else if (requestedCostume === 'previous costume') {
|
||||||
const numCostumes = target.getCostumes().length;
|
target.setCostume(target.currentCostume - 1);
|
||||||
|
} else if (!isNaN(requestedCostume) &&
|
||||||
|
(typeof requestedCostume !== 'string' || /\d/g.test(requestedCostume))) {
|
||||||
|
target.setCostume(optZeroIndex ? Number(requestedCostume) : Number(requestedCostume) - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Per 2.0, 'switch costume' can't start threads even in the Stage.
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility function to set the backdrop of a target.
|
||||||
|
* Matches the behavior of Scratch 2.0 for different types of arguments.
|
||||||
|
* @param {!Target} stage Target to set backdrop to.
|
||||||
|
* @param {Any} requestedBackdrop Backdrop requested, e.g., 0, 'name', etc.
|
||||||
|
* @param {boolean=} optZeroIndex Set to zero-index the requestedBackdrop.
|
||||||
|
* @return {Array.<!Thread>} Any threads started by this switch.
|
||||||
|
*/
|
||||||
|
_setBackdrop (stage, requestedBackdrop, optZeroIndex) {
|
||||||
|
if (typeof requestedBackdrop === 'number') {
|
||||||
|
stage.setCostume(optZeroIndex ? requestedBackdrop : requestedBackdrop - 1);
|
||||||
|
} else if (requestedBackdrop === 'next backdrop') {
|
||||||
|
stage.setCostume(stage.currentCostume + 1);
|
||||||
|
} else if (requestedBackdrop === 'previous backdrop') {
|
||||||
|
stage.setCostume(stage.currentCostume - 1);
|
||||||
|
} else {
|
||||||
|
const costumeIndex = stage.getCostumeIndexByName(requestedBackdrop.toString());
|
||||||
|
|
||||||
|
if (costumeIndex !== -1) {
|
||||||
|
stage.setCostume(costumeIndex);
|
||||||
|
} else if (requestedBackdrop === 'random backdrop') {
|
||||||
|
const numCostumes = stage.getCostumes().length;
|
||||||
if (numCostumes > 1) {
|
if (numCostumes > 1) {
|
||||||
let selectedIndex = Math.floor(Math.random() * (numCostumes - 1));
|
let selectedIndex = Math.floor(Math.random() * (numCostumes - 1));
|
||||||
if (selectedIndex === target.currentCostume) selectedIndex += 1;
|
if (selectedIndex === stage.currentCostume) selectedIndex += 1;
|
||||||
target.setCostume(selectedIndex);
|
stage.setCostume(selectedIndex);
|
||||||
}
|
}
|
||||||
} else {
|
} else if (!isNaN(requestedBackdrop) &&
|
||||||
const forcedNumber = Number(requestedCostume);
|
(typeof requestedBackdrop !== 'string' || /\d/g.test(requestedBackdrop))) {
|
||||||
if (!isNaN(forcedNumber)) {
|
stage.setCostume(optZeroIndex ? Number(requestedBackdrop) : Number(requestedBackdrop) - 1);
|
||||||
target.setCostume(optZeroIndex ?
|
|
||||||
forcedNumber : forcedNumber - 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (target === this.runtime.getTargetForStage()) {
|
const newName = stage.getCostumes()[stage.currentCostume].name;
|
||||||
// Target is the stage - start hats.
|
|
||||||
const newName = target.getCostumes()[target.currentCostume].name;
|
|
||||||
return this.runtime.startHats('event_whenbackdropswitchesto', {
|
return this.runtime.startHats('event_whenbackdropswitchesto', {
|
||||||
BACKDROP: newName
|
BACKDROP: newName
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
switchCostume (args, util) {
|
switchCostume (args, util) {
|
||||||
this._setCostumeOrBackdrop(util.target, args.COSTUME);
|
this._setCostume(util.target, args.COSTUME);
|
||||||
}
|
}
|
||||||
|
|
||||||
nextCostume (args, util) {
|
nextCostume (args, util) {
|
||||||
this._setCostumeOrBackdrop(
|
this._setCostume(
|
||||||
util.target, util.target.currentCostume + 1, true
|
util.target, util.target.currentCostume + 1, true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
switchBackdrop (args) {
|
switchBackdrop (args) {
|
||||||
this._setCostumeOrBackdrop(this.runtime.getTargetForStage(), args.BACKDROP);
|
this._setBackdrop(this.runtime.getTargetForStage(), args.BACKDROP);
|
||||||
}
|
}
|
||||||
|
|
||||||
switchBackdropAndWait (args, util) {
|
switchBackdropAndWait (args, util) {
|
||||||
|
@ -410,7 +431,7 @@ class Scratch3LooksBlocks {
|
||||||
if (!util.stackFrame.startedThreads) {
|
if (!util.stackFrame.startedThreads) {
|
||||||
// No - switch the backdrop.
|
// No - switch the backdrop.
|
||||||
util.stackFrame.startedThreads = (
|
util.stackFrame.startedThreads = (
|
||||||
this._setCostumeOrBackdrop(
|
this._setBackdrop(
|
||||||
this.runtime.getTargetForStage(),
|
this.runtime.getTargetForStage(),
|
||||||
args.BACKDROP
|
args.BACKDROP
|
||||||
)
|
)
|
||||||
|
@ -440,7 +461,7 @@ class Scratch3LooksBlocks {
|
||||||
|
|
||||||
nextBackdrop () {
|
nextBackdrop () {
|
||||||
const stage = this.runtime.getTargetForStage();
|
const stage = this.runtime.getTargetForStage();
|
||||||
this._setCostumeOrBackdrop(
|
this._setBackdrop(
|
||||||
stage, stage.currentCostume + 1, true
|
stage, stage.currentCostume + 1, true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -463,6 +463,8 @@ class RenderedTarget extends Target {
|
||||||
setCostume (index) {
|
setCostume (index) {
|
||||||
// Keep the costume index within possible values.
|
// Keep the costume index within possible values.
|
||||||
index = Math.round(index);
|
index = Math.round(index);
|
||||||
|
if ([Infinity, -Infinity, NaN].includes(index)) index = 0;
|
||||||
|
|
||||||
this.currentCostume = MathUtil.wrapClamp(
|
this.currentCostume = MathUtil.wrapClamp(
|
||||||
index, 0, this.sprite.costumes.length - 1
|
index, 0, this.sprite.costumes.length - 1
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue