Skip to content

Commit 4b1ec35

Browse files
SharkPool/Camera: Clones should inherit camera from parent, add epsilon (#2057)
Requested by Cubester ![image](https://github.com/user-attachments/assets/56b31eb5-65a7-4f12-b392-c297ed3b02b8) I also added a epsilon for float point issues and some minor optimizations --------- Co-authored-by: DangoCat[bot] <[email protected]>
1 parent 1ae69ed commit 4b1ec35

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

extensions/SharkPool/Camera.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// By: SharkPool
55
// License: MIT
66

7-
// Version V.1.0.07
7+
// Version V.1.0.08
88

99
(function (Scratch) {
1010
"use strict";
@@ -53,6 +53,10 @@
5353
}
5454

5555
// camera utils
56+
const radianConstant = Math.PI / 180;
57+
const epsilon = 1e-12;
58+
const applyEpsilon = (value) => (Math.abs(value) < epsilon ? 0 : value);
59+
5660
function setupState(drawable) {
5761
drawable[cameraSymbol] = {
5862
name: "default",
@@ -65,23 +69,26 @@
6569

6670
function translatePosition(xy, invert, camData) {
6771
if (invert) {
68-
const invRads = (camData.ogDir / 180) * Math.PI;
72+
const invRads = camData.ogDir * radianConstant;
6973
const invSin = Math.sin(invRads),
7074
invCos = Math.cos(invRads);
7175
const scaledX = xy[0] / camData.ogSZ;
7276
const scaledY = xy[1] / camData.ogSZ;
7377
const invOffX = scaledX * invCos + scaledY * invSin;
7478
const invOffY = -scaledX * invSin + scaledY * invCos;
75-
return [invOffX - camData.ogXY[0], invOffY - camData.ogXY[1]];
79+
return [
80+
applyEpsilon(invOffX - camData.ogXY[0]),
81+
applyEpsilon(invOffY - camData.ogXY[1]),
82+
];
7683
} else {
77-
const rads = (camData.dir / 180) * Math.PI;
84+
const rads = camData.dir * radianConstant;
7885
const sin = Math.sin(rads),
7986
cos = Math.cos(rads);
8087
const offX = xy[0] + camData.xy[0];
8188
const offY = xy[1] + camData.xy[1];
8289
return [
83-
camData.zoom * (offX * cos - offY * sin),
84-
camData.zoom * (offX * sin + offY * cos),
90+
applyEpsilon(camData.zoom * (offX * cos - offY * sin)),
91+
applyEpsilon(camData.zoom * (offX * sin + offY * cos)),
8592
];
8693
}
8794
}
@@ -252,6 +259,20 @@
252259
this.skin?.emitWasAltered();
253260
};
254261

262+
// Clones should inherit the parents camera
263+
const ogInitDrawable = vm.exports.RenderedTarget.prototype.initDrawable;
264+
vm.exports.RenderedTarget.prototype.initDrawable = function (layerGroup) {
265+
ogInitDrawable.call(this, layerGroup);
266+
if (this.isOriginal) return;
267+
268+
const parentSprite = this.sprite.clones[0]; // clone[0] is always the original
269+
const parentDrawable = render._allDrawables[parentSprite.drawableID];
270+
const name = parentDrawable[cameraSymbol]?.name ?? "default";
271+
272+
const drawable = render._allDrawables[this.drawableID];
273+
bindDrawable(drawable, name);
274+
};
275+
255276
// Turbowarp Extension Storage
256277
runtime.on("PROJECT_LOADED", () => {
257278
const stored = runtime.extensionStorage["SPcamera"];
@@ -700,10 +721,10 @@
700721
}
701722

702723
translateAngledMovement(xy, steps, direction) {
703-
const radians = direction * (Math.PI / 180);
724+
const radians = direction * radianConstant;
704725
return [
705-
xy[0] + steps * Math.cos(radians),
706-
xy[1] + steps * Math.sin(radians),
726+
applyEpsilon(xy[0] + steps * Math.cos(radians)),
727+
applyEpsilon(xy[1] + steps * Math.sin(radians)),
707728
];
708729
}
709730

0 commit comments

Comments
 (0)