Skip to content

Commit d7caa97

Browse files
committed
Implement motion_setx block
1 parent 6060064 commit d7caa97

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
@@ -48,6 +48,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
4848
engine->addCompileFunction(this, "motion_glidesecstoxy", &compileGlideSecsToXY);
4949
engine->addCompileFunction(this, "motion_glideto", &compileGlideTo);
5050
engine->addCompileFunction(this, "motion_changexby", &compileChangeXBy);
51+
engine->addCompileFunction(this, "motion_setx", &compileSetX);
5152
}
5253

5354
CompilerValue *MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -257,6 +258,16 @@ CompilerValue *MotionBlocks::compileChangeXBy(Compiler *compiler)
257258
return nullptr;
258259
}
259260

261+
CompilerValue *MotionBlocks::compileSetX(Compiler *compiler)
262+
{
263+
if (!compiler->target()->isStage()) {
264+
CompilerValue *x = compiler->addInput("X");
265+
compiler->addTargetFunctionCall("motion_setx", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { x });
266+
}
267+
268+
return nullptr;
269+
}
270+
260271
extern "C" void motion_movesteps(Sprite *sprite, double steps)
261272
{
262273
double dir = sprite->direction();
@@ -526,6 +537,11 @@ extern "C" void motion_changexby(Sprite *sprite, double dx)
526537
sprite->setX(sprite->x() + dx);
527538
}
528539

540+
extern "C" void motion_setx(Sprite *sprite, double x)
541+
{
542+
sprite->setX(x);
543+
}
544+
529545
extern "C" double motion_xposition(Sprite *sprite)
530546
{
531547
return sprite->x();

src/blocks/motionblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class MotionBlocks : public IExtension
2727
static CompilerValue *compileGlideSecsToXY(Compiler *compiler);
2828
static CompilerValue *compileGlideTo(Compiler *compiler);
2929
static CompilerValue *compileChangeXBy(Compiler *compiler);
30+
static CompilerValue *compileSetX(Compiler *compiler);
3031
};
3132

3233
} // namespace libscratchcpp

test/blocks/motion_blocks_test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,3 +1631,36 @@ TEST_F(MotionBlocksTest, ChangeXBy)
16311631
builder.run();
16321632
}
16331633
}
1634+
1635+
TEST_F(MotionBlocksTest, SetX)
1636+
{
1637+
{
1638+
auto sprite = std::make_shared<Sprite>();
1639+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1640+
1641+
builder.addBlock("motion_setx");
1642+
builder.addValueInput("X", 30.25);
1643+
1644+
sprite->setX(5.2);
1645+
sprite->setY(-0.25);
1646+
sprite->setDirection(-61.42);
1647+
1648+
builder.build();
1649+
builder.run();
1650+
ASSERT_EQ(sprite->x(), 30.25);
1651+
ASSERT_EQ(sprite->y(), -0.25);
1652+
}
1653+
1654+
m_engine->clear();
1655+
1656+
{
1657+
auto stage = std::make_shared<Stage>();
1658+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
1659+
1660+
builder.addBlock("motion_setx");
1661+
builder.addValueInput("X", 30.25);
1662+
1663+
builder.build();
1664+
builder.run();
1665+
}
1666+
}

0 commit comments

Comments
 (0)