Skip to content

Commit f5e6e99

Browse files
committed
Handle the edge case of checking if a sprite touches itself
1 parent 5f3553d commit f5e6e99

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/scratch/target.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,11 @@ bool Target::touchingSprite(Sprite *sprite) const
443443
assert(firstClone);
444444
std::vector<Sprite *> clones;
445445

446-
if (true) // TODO: Filter clones that are being dragged, including firstClone
446+
if (firstClone != this) // TODO: Filter clones that are being dragged, including firstClone
447447
clones.push_back(firstClone);
448448

449449
for (auto clone : firstClone->clones()) {
450-
if (true) // TODO: Filter clones that are being dragged
450+
if (clone.get() != this) // TODO: Filter clones that are being dragged
451451
clones.push_back(clone.get());
452452
}
453453

test/scratch_classes/sprite_test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,30 @@ TEST(SpriteTest, TouchingSprite)
649649

650650
EXPECT_CALL(iface, touchingClones).WillOnce(Return(true));
651651
ASSERT_TRUE(sprite.touchingSprite(&another));
652+
653+
SpriteHandlerMock iface2;
654+
EXPECT_CALL(iface2, init);
655+
another.setInterface(&iface2);
656+
clones.erase(clones.begin()); // the sprite cannot touch itself
657+
658+
EXPECT_CALL(iface2, touchingClones(_)).WillOnce(WithArgs<0>(Invoke([&clones, &actualClones](const std::vector<Sprite *> &candidates) {
659+
actualClones = candidates;
660+
return false;
661+
})));
662+
ASSERT_FALSE(another.touchingSprite(&another));
663+
ASSERT_EQ(clones, actualClones);
664+
clones.insert(clones.begin(), &another);
665+
666+
SpriteHandlerMock iface3;
667+
EXPECT_CALL(iface3, init);
668+
clone2->setInterface(&iface3);
669+
clones.erase(clones.begin() + 2); // the clone cannot touch itself, but can touch the sprite (clone root)
670+
EXPECT_CALL(iface3, touchingClones(_)).WillOnce(WithArgs<0>(Invoke([&clones, &actualClones](const std::vector<Sprite *> &candidates) {
671+
actualClones = candidates;
672+
return false;
673+
})));
674+
ASSERT_FALSE(clone2->touchingSprite(clone2.get()));
675+
ASSERT_EQ(clones, actualClones);
652676
}
653677

654678
TEST(SpriteTest, TouchingPoint)

0 commit comments

Comments
 (0)