Skip to content

Commit 385b073

Browse files
authored
Hyper-Sense-V2.js -- optimize 'sprites touching' block
1 parent 17ab15d commit 385b073

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

extension-code/Hyper-Sense-V2.js

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

7-
// Version 2.0.0
7+
// Version 2.0.01
88

99
(function (Scratch) {
1010
"use strict";
@@ -612,13 +612,10 @@
612612
return `#${rgb.r.toString(16).padStart(2, "0")}${rgb.g.toString(16).padStart(2, "0")}${rgb.b.toString(16).padStart(2, "0")}`;
613613
}
614614

615-
getTarget(name, util, checkMouse, checkMyself, returnName) {
615+
getTarget(name, util, checkMouse, checkMyself) {
616616
if (checkMouse && name === "_mouse_") return "_mouse_";
617-
if (checkMyself && name === "_myself_") return returnName ? util.target.getName() : util.target;
618-
619-
const target = runtime.getSpriteTargetByName(name);
620-
if (returnName) return target ? target.getName() : name;
621-
else return target;
617+
if (checkMyself && name === "_myself_") return util.target;
618+
return runtime.getSpriteTargetByName(name);
622619
}
623620

624621
updateAskMonitor() {
@@ -761,7 +758,7 @@
761758
}
762759

763760
spritePointing(args, util) {
764-
const target = this.getTarget(args.SPRITE1, util, false, true, false);
761+
const target = this.getTarget(args.SPRITE1, util, false, true);
765762
if (!target) return false;
766763
const oldDir = target.direction;
767764
runtime.ext_scratch3_motion.pointTowards({ TOWARDS: args.SPRITE2 }, { ...util, target, ioQuery: util.ioQuery });
@@ -771,13 +768,13 @@
771768
}
772769

773770
spriteTouchingSprite(args, util) {
774-
const target = this.getTarget(args.SPRITE2, util, false, true, false);
771+
const target = this.getTarget(args.SPRITE2, util, false, true);
775772
if (!target) return false;
776773
return target.sprite.clones.some((t) => t.isTouchingObject(args.SPRITE1));
777774
}
778775

779776
spriteTouchingSpriteType(args, util) {
780-
const target1 = this.getTarget(args.SPRITE1, util, true, true, false);
777+
const target1 = this.getTarget(args.SPRITE1, util, true, true);
781778
const target2 = runtime.getSpriteTargetByName(args.SPRITE2);
782779
if (!target1 || !target2) return false;
783780
if (args.TYPE === "parent") {
@@ -795,7 +792,7 @@
795792
}
796793

797794
spriteTouchingClone(args, util) {
798-
const target1 = this.getTarget(args.SPRITE1, util, true, true, false);
795+
const target1 = this.getTarget(args.SPRITE1, util, true, true);
799796
const target2 = runtime.getSpriteTargetByName(args.SPRITE2);
800797
if (!target1 || !target2) return false;
801798

@@ -815,12 +812,23 @@
815812

816813
spritesTouching(args, util) {
817814
const list = [];
818-
const thisSprite = this.getTarget(args.SPRITE, util, true, true, true);
815+
const thisSprite = this.getTarget(args.SPRITE, util, true, true);
816+
if (!thisSprite) return "[]";
819817
const targets = runtime.targets;
820-
for (let i = 1; i < targets.length; i++) {
821-
const target = targets[i];
822-
const name = `${target.getName()}${target.isOriginal ? "" : " (Clone)"}`;
823-
if (target.isTouchingObject(thisSprite) && name !== thisSprite) list.push(name);
818+
if (thisSprite === "_mouse_") {
819+
for (let i = 1; i < targets.length; i++) {
820+
const target = targets[i];
821+
const name = `${target.getName()}${target.isOriginal ? "" : " (Clone)"}`;
822+
if (target.isTouchingObject(thisSprite)) list.push(name);
823+
}
824+
} else {
825+
for (let i = 1; i < targets.length; i++) {
826+
const target = targets[i];
827+
if (target.id === thisSprite.id) continue;
828+
829+
const name = `${target.getName()}${target.isOriginal ? "" : " (Clone)"}`;
830+
if (render.isTouchingDrawables(thisSprite.drawableID, [target.drawableID])) list.push(name);
831+
}
824832
}
825833
return JSON.stringify(list);
826834
}
@@ -849,7 +857,7 @@
849857
objTouchingColor(args, util) {
850858
if (args.SPRITE === "_mouse_") return this.colorAtPos(util.ioQuery("mouse", "getScratchX"), util.ioQuery("mouse", "getScratchY"));
851859
else {
852-
const target = this.getTarget(args.SPRITE, util, false, true, false);
860+
const target = this.getTarget(args.SPRITE, util, false, true);
853861
if (!target) return "";
854862
const wasVisible = target.visible;
855863
target.setVisible(false);
@@ -860,11 +868,11 @@
860868
}
861869

862870
spriteDragMode(args, util) {
863-
const target = this.getTarget(args.SPRITE, util, false, true, false);
871+
const target = this.getTarget(args.SPRITE, util, false, true);
864872
if (target) target.setDraggable(args.DRAG === "draggable");
865873
}
866874
spriteDraggable(args, util) {
867-
const target = this.getTarget(args.SPRITE, util, false, true, false);
875+
const target = this.getTarget(args.SPRITE, util, false, true);
868876
if (target) return target[args.DRAG === "draggable" ? "draggable" : "dragging"];
869877
return false;
870878
}

0 commit comments

Comments
 (0)