Skip to content

Commit 6060064

Browse files
committed
Implement motion_changexby block
1 parent f0fa83f commit 6060064

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
@@ -47,6 +47,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
4747
engine->addCompileFunction(this, "motion_goto", &compileGoTo);
4848
engine->addCompileFunction(this, "motion_glidesecstoxy", &compileGlideSecsToXY);
4949
engine->addCompileFunction(this, "motion_glideto", &compileGlideTo);
50+
engine->addCompileFunction(this, "motion_changexby", &compileChangeXBy);
5051
}
5152

5253
CompilerValue *MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -246,6 +247,16 @@ CompilerValue *MotionBlocks::compileGlideTo(Compiler *compiler)
246247
return nullptr;
247248
}
248249

250+
CompilerValue *MotionBlocks::compileChangeXBy(Compiler *compiler)
251+
{
252+
if (!compiler->target()->isStage()) {
253+
CompilerValue *dx = compiler->addInput("DX");
254+
compiler->addTargetFunctionCall("motion_changexby", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { dx });
255+
}
256+
257+
return nullptr;
258+
}
259+
249260
extern "C" void motion_movesteps(Sprite *sprite, double steps)
250261
{
251262
double dir = sprite->direction();
@@ -510,6 +521,11 @@ extern "C" bool motion_is_target_valid(ExecutionContext *ctx, const StringPtr *n
510521
}
511522
}
512523

524+
extern "C" void motion_changexby(Sprite *sprite, double dx)
525+
{
526+
sprite->setX(sprite->x() + dx);
527+
}
528+
513529
extern "C" double motion_xposition(Sprite *sprite)
514530
{
515531
return sprite->x();

src/blocks/motionblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class MotionBlocks : public IExtension
2626
static CompilerValue *compileGoTo(Compiler *compiler);
2727
static CompilerValue *compileGlideSecsToXY(Compiler *compiler);
2828
static CompilerValue *compileGlideTo(Compiler *compiler);
29+
static CompilerValue *compileChangeXBy(Compiler *compiler);
2930
};
3031

3132
} // namespace libscratchcpp

test/blocks/motion_blocks_test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,3 +1598,36 @@ TEST_F(MotionBlocksTest, GlideToSprite)
15981598
ASSERT_TRUE(code->isFinished(ctx.get()));
15991599
}
16001600
}
1601+
1602+
TEST_F(MotionBlocksTest, ChangeXBy)
1603+
{
1604+
{
1605+
auto sprite = std::make_shared<Sprite>();
1606+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1607+
1608+
builder.addBlock("motion_changexby");
1609+
builder.addValueInput("DX", 30.25);
1610+
1611+
sprite->setX(5.2);
1612+
sprite->setY(-0.25);
1613+
sprite->setDirection(-61.42);
1614+
1615+
builder.build();
1616+
builder.run();
1617+
ASSERT_EQ(sprite->x(), 35.45);
1618+
ASSERT_EQ(sprite->y(), -0.25);
1619+
}
1620+
1621+
m_engine->clear();
1622+
1623+
{
1624+
auto stage = std::make_shared<Stage>();
1625+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
1626+
1627+
builder.addBlock("motion_changexby");
1628+
builder.addValueInput("DX", 30.25);
1629+
1630+
builder.build();
1631+
builder.run();
1632+
}
1633+
}

0 commit comments

Comments
 (0)