Skip to content

Commit 4967268

Browse files
committed
Implement motion_gotoxy block
1 parent 727bf6a commit 4967268

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/blocks/motionblocks.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
4242
engine->addCompileFunction(this, "motion_turnleft", &compileTurnLeft);
4343
engine->addCompileFunction(this, "motion_pointindirection", &compilePointInDirection);
4444
engine->addCompileFunction(this, "motion_pointtowards", &compilePointTowards);
45+
engine->addCompileFunction(this, "motion_gotoxy", &compileGoToXY);
4546
}
4647

4748
CompilerValue *MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -113,6 +114,17 @@ CompilerValue *MotionBlocks::compilePointTowards(Compiler *compiler)
113114
return nullptr;
114115
}
115116

117+
CompilerValue *MotionBlocks::compileGoToXY(Compiler *compiler)
118+
{
119+
if (!compiler->target()->isStage()) {
120+
CompilerValue *x = compiler->addInput("X");
121+
CompilerValue *y = compiler->addInput("Y");
122+
compiler->addTargetFunctionCall("motion_gotoxy", Compiler::StaticType::Void, { Compiler::StaticType::Number, Compiler::StaticType::Number }, { x, y });
123+
}
124+
125+
return nullptr;
126+
}
127+
116128
extern "C" void motion_movesteps(Sprite *sprite, double steps)
117129
{
118130
double dir = sprite->direction();
@@ -196,3 +208,8 @@ extern "C" void motion_pointtowards(ExecutionContext *ctx, const StringPtr *towa
196208
}
197209
}
198210
}
211+
212+
extern "C" void motion_gotoxy(Sprite *sprite, double x, double y)
213+
{
214+
sprite->setPosition(x, y);
215+
}

src/blocks/motionblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class MotionBlocks : public IExtension
2222
static CompilerValue *compileTurnLeft(Compiler *compiler);
2323
static CompilerValue *compilePointInDirection(Compiler *compiler);
2424
static CompilerValue *compilePointTowards(Compiler *compiler);
25+
static CompilerValue *compileGoToXY(Compiler *compiler);
2526
};
2627

2728
} // namespace libscratchcpp

test/blocks/motion_blocks_test.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,37 @@ TEST_F(MotionBlocksTest, PointTowardsSprite)
486486
builder.run();
487487
}
488488
}
489+
490+
TEST_F(MotionBlocksTest, GoToXY)
491+
{
492+
{
493+
auto sprite = std::make_shared<Sprite>();
494+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
495+
496+
builder.addBlock("motion_gotoxy");
497+
builder.addValueInput("X", -55.2);
498+
builder.addValueInput("Y", 23.254);
499+
500+
sprite->setX(51.28);
501+
sprite->setY(-0.5);
502+
503+
builder.build();
504+
builder.run();
505+
ASSERT_EQ(sprite->x(), -55.2);
506+
ASSERT_EQ(sprite->y(), 23.254);
507+
}
508+
509+
m_engine->clear();
510+
511+
{
512+
auto stage = std::make_shared<Stage>();
513+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
514+
515+
builder.addBlock("motion_gotoxy");
516+
builder.addValueInput("X", -55.2);
517+
builder.addValueInput("Y", 23.254);
518+
519+
builder.build();
520+
builder.run();
521+
}
522+
}

0 commit comments

Comments
 (0)