Skip to content

Commit

Permalink
list of touching targets
Browse files Browse the repository at this point in the history
  • Loading branch information
jwklong committed Dec 6, 2024
1 parent 39531bb commit 29c0f2c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/extensions/jwTargets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ class Extension {
text: 'all targets',
...jwArray.Block
},
{
opcode: 'touching',
text: 'targets touching [TARGET]',
arguments: {
TARGET: Target.Argument
},
filter: [TargetType.SPRITE],
...jwArray.Block
},
{
opcode: 'clones',
text: 'clones of [TARGET]',
Expand Down Expand Up @@ -235,6 +244,13 @@ class Extension {
return new jwArray.Type(vm.runtime.targets.map(v => new Target.Type(v.id)))
}

touching({TARGET}) {
let targets = vm.runtime.targets
targets.filter(v => v !== TARGET && !v.isStage)
targets.filter(v => TARGET.isTouchingTarget(v))
return new jwArray.Type(targets.map(v => new Target.Type(v.id)))
}

clones({TARGET}) {
TARGET = Target.Type.toTarget(TARGET)
if (TARGET.target) {
Expand Down
18 changes: 17 additions & 1 deletion src/sprites/rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ class RenderedTarget extends Target {
/**
* Return whether touching any of a named sprite's clones.
* @param {string} spriteName Name of the sprite.
* @return {boolean} True iff touching a clone of the sprite.
* @return {boolean} True if touching a clone of the sprite.
*/
isTouchingSprite (spriteName) {
spriteName = Cast.toString(spriteName);
Expand All @@ -938,6 +938,22 @@ class RenderedTarget extends Target {
return this.renderer.isTouchingDrawables(
this.drawableID, drawableCandidates);
}

/**
* Return whether touching a target.
* @param {string} targetId ID of the target
* @return {boolean} True if touching the target
*/
isTouchingTarget (targetId) {
targetId = Cast.toString(targetId);
const target = this.runtime.getSpriteTargetByName(targetId);
if (!target || !this.renderer || target.dragging) {
return false;
}
return this.renderer.isTouchingDrawables(
this.drawableID, [target.drawableID]);
}

/**
* Return whether touching any of a named sprite's unoriginal clones.
* @param {string} spriteName Name of the sprite.
Expand Down

0 comments on commit 29c0f2c

Please sign in to comment.