Skip to content

NexusKitten/moremotion | Add "bounds" option and manual fencing of nx, ny. #2103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 62 additions & 15 deletions extensions/NexusKitten/moremotion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// ID: nkmoremotion
// Description: More motion-related blocks.
// By: NamelessCat <https://scratch.mit.edu/users/NamelessCat/>
// By: Mio <https://scratch.mit.edu/users/0znzw/>
// License: MIT

(function (Scratch) {
Expand Down Expand Up @@ -86,6 +87,26 @@
}),
extensions: ["colours_motion"],
},
{
filter: [Scratch.TargetType.SPRITE],
opcode: "fenceXY",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate({
default: "manually fence new x: [X] new y: [Y]",
description: "Fences a specific new X and new Y for the sprite.",
}),
arguments: {
X: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "0",
},
Y: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: "0",
},
},
extensions: ["colours_motion"],
},
"---",
{
filter: [Scratch.TargetType.SPRITE],
Expand Down Expand Up @@ -255,6 +276,10 @@
text: Scratch.translate("height"),
value: "height",
},
{
text: Scratch.translate("bounds"),
value: "bounds",
},
{
text: Scratch.translate("costume width"),
value: "costume width",
Expand Down Expand Up @@ -294,6 +319,14 @@
);
util.target.setXY(newpos[0], newpos[1]);
}
fenceXY(args, util) {
return JSON.stringify(
util.target.keepInFence(
Scratch.Cast.toNumber(args.X),
Scratch.Cast.toNumber(args.Y)
)
);
}

directionto(args, util) {
// Old version, returns values from -90 to 270
Expand Down Expand Up @@ -425,23 +458,37 @@
}

spritewh(args, util) {
if (args.WHAT === "width" || args.WHAT === "height") {
const bounds = Scratch.vm.renderer.getBounds(util.target.drawableID);
if (args.WHAT === "width") {
if (args.WHAT.startsWith("costume ")) {
const [w, h] =
util.target.sprite.costumes[util.target.currentCostume].size;
if (args.WHAT.endsWith("width")) return Math.ceil(w);
if (args.WHAT.endsWith("height")) return Math.ceil(h);
return 0;
}
// Bounds may not always exist so default to a similar 0 valued object
const bounds = Scratch.vm.renderer.getBounds(util.target.drawableID) ?? {
left: 0,
right: 0,
top: 0,
bottom: 0,
width: 0,
height: 0,
};
switch (args.WHAT) {
case "bounds":
// Useful for more precision or just getting the edges
return JSON.stringify(
Object.assign(
{ width: bounds.width, height: bounds.height },
bounds
)
);
case "width":
return Math.ceil(bounds.width);
} else {
case "height":
return Math.ceil(bounds.height);
}
} else if (
args.WHAT === "costume width" ||
args.WHAT === "costume height"
) {
const costume = util.target.sprite.costumes[util.target.currentCostume];
if (args.WHAT === "costume width") {
return Math.ceil(costume.size[0]);
} else {
return Math.ceil(costume.size[1]);
}
default:
return 0;
}
}
}
Expand Down
Loading