Skip to content

Commit e5bba37

Browse files
committed
Implement motion_sety block
1 parent 8dbd3f2 commit e5bba37

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/blocks/motionblocks.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
5050
engine->addCompileFunction(this, "motion_changexby", &compileChangeXBy);
5151
engine->addCompileFunction(this, "motion_setx", &compileSetX);
5252
engine->addCompileFunction(this, "motion_changeyby", &compileChangeYBy);
53+
engine->addCompileFunction(this, "motion_sety", &compileSetY);
5354
}
5455

5556
CompilerValue *MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -279,6 +280,16 @@ CompilerValue *MotionBlocks::compileChangeYBy(Compiler *compiler)
279280
return nullptr;
280281
}
281282

283+
CompilerValue *MotionBlocks::compileSetY(Compiler *compiler)
284+
{
285+
if (!compiler->target()->isStage()) {
286+
CompilerValue *y = compiler->addInput("Y");
287+
compiler->addTargetFunctionCall("motion_sety", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { y });
288+
}
289+
290+
return nullptr;
291+
}
292+
282293
extern "C" void motion_movesteps(Sprite *sprite, double steps)
283294
{
284295
double dir = sprite->direction();
@@ -558,6 +569,11 @@ extern "C" void motion_changeyby(Sprite *sprite, double dy)
558569
sprite->setY(sprite->y() + dy);
559570
}
560571

572+
extern "C" void motion_sety(Sprite *sprite, double y)
573+
{
574+
sprite->setY(y);
575+
}
576+
561577
extern "C" double motion_xposition(Sprite *sprite)
562578
{
563579
return sprite->x();

src/blocks/motionblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class MotionBlocks : public IExtension
2929
static CompilerValue *compileChangeXBy(Compiler *compiler);
3030
static CompilerValue *compileSetX(Compiler *compiler);
3131
static CompilerValue *compileChangeYBy(Compiler *compiler);
32+
static CompilerValue *compileSetY(Compiler *compiler);
3233
};
3334

3435
} // namespace libscratchcpp

test/blocks/motion_blocks_test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,3 +1697,36 @@ TEST_F(MotionBlocksTest, ChangeYBy)
16971697
builder.run();
16981698
}
16991699
}
1700+
1701+
TEST_F(MotionBlocksTest, SetY)
1702+
{
1703+
{
1704+
auto sprite = std::make_shared<Sprite>();
1705+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1706+
1707+
builder.addBlock("motion_sety");
1708+
builder.addValueInput("Y", 30.25);
1709+
1710+
sprite->setX(5.2);
1711+
sprite->setY(-0.25);
1712+
sprite->setDirection(-61.42);
1713+
1714+
builder.build();
1715+
builder.run();
1716+
ASSERT_EQ(sprite->x(), 5.2);
1717+
ASSERT_EQ(sprite->y(), 30.25);
1718+
}
1719+
1720+
m_engine->clear();
1721+
1722+
{
1723+
auto stage = std::make_shared<Stage>();
1724+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
1725+
1726+
builder.addBlock("motion_sety");
1727+
builder.addValueInput("Y", 30.25);
1728+
1729+
builder.build();
1730+
builder.run();
1731+
}
1732+
}

0 commit comments

Comments
 (0)