Skip to content
Closed
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
101 changes: 101 additions & 0 deletions scratch-js/Sprite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,59 @@ export class Sprite extends SpriteBase {
} while (t < 1);
}

ifOnEdgeBounce() {
const nearestEdge = this.nearestEdge();
if (!nearestEdge) return;
const rad = this.degToRad(this.scratchToDeg(this.direction));
let dx = Math.cos(rad);
let dy = -Math.sin(rad);
switch (nearestEdge) {
case Sprite.Edge.LEFT:
dx = Math.max(0.2, Math.abs(dx));
break;
case Sprite.Edge.RIGHT:
dx = -Math.max(0.2, Math.abs(dx));
break;
case Sprite.Edge.TOP:
dy = Math.max(0.2, Math.abs(dy));
break;
case Sprite.Edge.BOTTOM:
dy = -Math.max(0.2, Math.abs(dy));
break;
}
this.direction = this.degToScratch(this.radToDeg(Math.atan2(dy, dx)));
const {x, y} = this.keepInFence(this.x, this.y);
this.goto(x, y);
}

keepInFence(newX, newY) {
// https://github.com/LLK/scratch-vm/blob/develop/src/sprites/rendered-target.js#L949
const fence = this.stage.fence;
const bounds = this._project.renderer.getBoundingBox(this);
bounds.left += (newX - this.x);
bounds.right += (newX - this.x);
bounds.top += (newY - this.y);
bounds.bottom += (newY - this.y);

let dx = 0, dy = 0;
if (bounds.left < fence.left) {
dx += fence.left - bounds.left;
}
if (bounds.right > fence.right) {
dx += fence.right - bounds.right;
}
if (bounds.top > fence.top) {
dy += fence.top - bounds.top;
}
if (bounds.bottom < fence.bottom) {
dy += fence.bottom - bounds.bottom;
}
return ({
x: newX + dx,
y: newY + dy
});
}

get penDown() {
return this._penDown;
}
Expand Down Expand Up @@ -500,6 +553,8 @@ export class Sprite extends SpriteBase {
},
fast
);
case "edge":
return !!this.nearestEdge();
default:
console.error(
`Cannot find target "${target}" in "touching". Did you mean to pass a sprite class instead?`
Expand Down Expand Up @@ -542,6 +597,38 @@ export class Sprite extends SpriteBase {
}
}

nearestEdge() {
const bounds = this._project.renderer.getBoundingBox(this);
const {width: stageWidth, height: stageHeight} = this.stage;
const distLeft = Math.max(0, (stageWidth / 2) + bounds.left);
const distTop = Math.max(0, (stageHeight / 2) - bounds.top);
const distRight = Math.max(0, (stageWidth / 2) - bounds.right);
const distBottom = Math.max(0, (stageHeight / 2) + bounds.bottom);
// Find the nearest edge.
let nearestEdge = '';
let minDist = Infinity;
if (distLeft < minDist) {
minDist = distLeft;
nearestEdge = Sprite.Edge.LEFT;
}
if (distTop < minDist) {
minDist = distTop;
nearestEdge = Sprite.Edge.TOP;
}
if (distRight < minDist) {
minDist = distRight;
nearestEdge = Sprite.Edge.RIGHT;
}
if (distBottom < minDist) {
minDist = distBottom;
nearestEdge = Sprite.Edge.BOTTOM;
}
if (minDist > 0) {
nearestEdge = null;
}
return nearestEdge;
}

say(text) {
clearTimeout(this._speechBubble.timeout);
this._speechBubble = { text, style: "say", timeout: null };
Expand Down Expand Up @@ -587,6 +674,13 @@ Sprite.RotationStyle = Object.freeze({
DONT_ROTATE: Symbol("DONT_ROTATE")
});

Sprite.Edge = Object.freeze({
BOTTOM: Symbol("BOTTOM"),
LEFT: Symbol("LEFT"),
RIGHT: Symbol("RIGHT"),
TOP: Symbol("TOP")
});

export class Stage extends SpriteBase {
constructor(initialConditions, ...args) {
super(initialConditions, ...args);
Expand All @@ -604,6 +698,13 @@ export class Stage extends SpriteBase {
}
});

this.fence = {
left: -this.width / 2,
right: this.width / 2,
top: this.height / 2,
bottom: -this.height / 2
};

this.name = "Stage";

// For obsolete counter blocks.
Expand Down