Skip to content

Commit 8dbd3f2

Browse files
committed
Implement motion_changeyby block
1 parent d7caa97 commit 8dbd3f2

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
@@ -49,6 +49,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
4949
engine->addCompileFunction(this, "motion_glideto", &compileGlideTo);
5050
engine->addCompileFunction(this, "motion_changexby", &compileChangeXBy);
5151
engine->addCompileFunction(this, "motion_setx", &compileSetX);
52+
engine->addCompileFunction(this, "motion_changeyby", &compileChangeYBy);
5253
}
5354

5455
CompilerValue *MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -268,6 +269,16 @@ CompilerValue *MotionBlocks::compileSetX(Compiler *compiler)
268269
return nullptr;
269270
}
270271

272+
CompilerValue *MotionBlocks::compileChangeYBy(Compiler *compiler)
273+
{
274+
if (!compiler->target()->isStage()) {
275+
CompilerValue *dy = compiler->addInput("DY");
276+
compiler->addTargetFunctionCall("motion_changeyby", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { dy });
277+
}
278+
279+
return nullptr;
280+
}
281+
271282
extern "C" void motion_movesteps(Sprite *sprite, double steps)
272283
{
273284
double dir = sprite->direction();
@@ -542,6 +553,11 @@ extern "C" void motion_setx(Sprite *sprite, double x)
542553
sprite->setX(x);
543554
}
544555

556+
extern "C" void motion_changeyby(Sprite *sprite, double dy)
557+
{
558+
sprite->setY(sprite->y() + dy);
559+
}
560+
545561
extern "C" double motion_xposition(Sprite *sprite)
546562
{
547563
return sprite->x();

src/blocks/motionblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class MotionBlocks : public IExtension
2828
static CompilerValue *compileGlideTo(Compiler *compiler);
2929
static CompilerValue *compileChangeXBy(Compiler *compiler);
3030
static CompilerValue *compileSetX(Compiler *compiler);
31+
static CompilerValue *compileChangeYBy(Compiler *compiler);
3132
};
3233

3334
} // namespace libscratchcpp

test/blocks/motion_blocks_test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,3 +1664,36 @@ TEST_F(MotionBlocksTest, SetX)
16641664
builder.run();
16651665
}
16661666
}
1667+
1668+
TEST_F(MotionBlocksTest, ChangeYBy)
1669+
{
1670+
{
1671+
auto sprite = std::make_shared<Sprite>();
1672+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1673+
1674+
builder.addBlock("motion_changeyby");
1675+
builder.addValueInput("DY", 30.25);
1676+
1677+
sprite->setX(5.2);
1678+
sprite->setY(-0.25);
1679+
sprite->setDirection(-61.42);
1680+
1681+
builder.build();
1682+
builder.run();
1683+
ASSERT_EQ(sprite->x(), 5.2);
1684+
ASSERT_EQ(sprite->y(), 30);
1685+
}
1686+
1687+
m_engine->clear();
1688+
1689+
{
1690+
auto stage = std::make_shared<Stage>();
1691+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
1692+
1693+
builder.addBlock("motion_changeyby");
1694+
builder.addValueInput("DY", 30.25);
1695+
1696+
builder.build();
1697+
builder.run();
1698+
}
1699+
}

0 commit comments

Comments
 (0)