Skip to content

Commit dfdcab9

Browse files
committed
Implement motion_turnright block
1 parent ff81f7a commit dfdcab9

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/blocks/motionblocks.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Rgb MotionBlocks::color() const
2929
void MotionBlocks::registerBlocks(IEngine *engine)
3030
{
3131
engine->addCompileFunction(this, "motion_movesteps", &compileMoveSteps);
32+
engine->addCompileFunction(this, "motion_turnright", &compileTurnRight);
3233
}
3334

3435
CompilerValue *MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -41,8 +42,23 @@ CompilerValue *MotionBlocks::compileMoveSteps(Compiler *compiler)
4142
return nullptr;
4243
}
4344

45+
CompilerValue *MotionBlocks::compileTurnRight(Compiler *compiler)
46+
{
47+
if (!compiler->target()->isStage()) {
48+
CompilerValue *degrees = compiler->addInput("DEGREES");
49+
compiler->addTargetFunctionCall("motion_turnright", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { degrees });
50+
}
51+
52+
return nullptr;
53+
}
54+
4455
extern "C" void motion_movesteps(Sprite *sprite, double steps)
4556
{
4657
double dir = sprite->direction();
4758
sprite->setPosition(sprite->x() + std::sin(dir * pi / 180) * steps, sprite->y() + std::cos(dir * pi / 180) * steps);
4859
}
60+
61+
extern "C" void motion_turnright(Sprite *sprite, double degrees)
62+
{
63+
sprite->setDirection(sprite->direction() + degrees);
64+
}

src/blocks/motionblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class MotionBlocks : public IExtension
1818

1919
private:
2020
static CompilerValue *compileMoveSteps(Compiler *compiler);
21+
static CompilerValue *compileTurnRight(Compiler *compiler);
2122
};
2223

2324
} // namespace libscratchcpp

test/blocks/motion_blocks_test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,33 @@ TEST_F(MotionBlocksTest, MoveSteps)
5858
builder.run();
5959
}
6060
}
61+
62+
TEST_F(MotionBlocksTest, TurnRight)
63+
{
64+
{
65+
auto sprite = std::make_shared<Sprite>();
66+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
67+
68+
builder.addBlock("motion_turnright");
69+
builder.addValueInput("DEGREES", 12.05);
70+
71+
sprite->setDirection(124.37);
72+
73+
builder.build();
74+
builder.run();
75+
ASSERT_EQ(std::round(sprite->direction() * 100) / 100, 136.42);
76+
}
77+
78+
m_engine->clear();
79+
80+
{
81+
auto stage = std::make_shared<Stage>();
82+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
83+
84+
builder.addBlock("motion_turnright");
85+
builder.addValueInput("DEGREES", 12.05);
86+
87+
builder.build();
88+
builder.run();
89+
}
90+
}

0 commit comments

Comments
 (0)