Skip to content

Commit ea41472

Browse files
committed
Implement motion_gotoxy block
1 parent a4f2057 commit ea41472

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
@@ -436,3 +436,37 @@ TEST_F(MotionBlocksTest, PointTowardsSprite)
436436
builder.run();
437437
}
438438
}
439+
440+
TEST_F(MotionBlocksTest, GoToXY)
441+
{
442+
{
443+
auto sprite = std::make_shared<Sprite>();
444+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
445+
446+
builder.addBlock("motion_gotoxy");
447+
builder.addValueInput("X", -55.2);
448+
builder.addValueInput("Y", 23.254);
449+
450+
sprite->setX(51.28);
451+
sprite->setY(-0.5);
452+
453+
builder.build();
454+
builder.run();
455+
ASSERT_EQ(sprite->x(), -55.2);
456+
ASSERT_EQ(sprite->y(), 23.254);
457+
}
458+
459+
m_engine->clear();
460+
461+
{
462+
auto stage = std::make_shared<Stage>();
463+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
464+
465+
builder.addBlock("motion_gotoxy");
466+
builder.addValueInput("X", -55.2);
467+
builder.addValueInput("Y", 23.254);
468+
469+
builder.build();
470+
builder.run();
471+
}
472+
}

0 commit comments

Comments
 (0)