Skip to content

Commit 522dbb7

Browse files
committed
Implement motion_turnleft block
1 parent f40be06 commit 522dbb7

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
@@ -30,6 +30,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
3030
{
3131
engine->addCompileFunction(this, "motion_movesteps", &compileMoveSteps);
3232
engine->addCompileFunction(this, "motion_turnright", &compileTurnRight);
33+
engine->addCompileFunction(this, "motion_turnleft", &compileTurnLeft);
3334
}
3435

3536
CompilerValue *MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -46,6 +47,13 @@ CompilerValue *MotionBlocks::compileTurnRight(Compiler *compiler)
4647
return nullptr;
4748
}
4849

50+
CompilerValue *MotionBlocks::compileTurnLeft(Compiler *compiler)
51+
{
52+
CompilerValue *degrees = compiler->addInput("DEGREES");
53+
compiler->addTargetFunctionCall("motion_turnleft", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { degrees });
54+
return nullptr;
55+
}
56+
4957
extern "C" void motion_movesteps(Target *target, double steps)
5058
{
5159
if (!target->isStage()) {
@@ -62,3 +70,11 @@ extern "C" void motion_turnright(Target *target, double degrees)
6270
sprite->setDirection(sprite->direction() + degrees);
6371
}
6472
}
73+
74+
extern "C" void motion_turnleft(Target *target, double degrees)
75+
{
76+
if (!target->isStage()) {
77+
Sprite *sprite = static_cast<Sprite *>(target);
78+
sprite->setDirection(sprite->direction() - degrees);
79+
}
80+
}

src/blocks/motionblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class MotionBlocks : public IExtension
1919
private:
2020
static CompilerValue *compileMoveSteps(Compiler *compiler);
2121
static CompilerValue *compileTurnRight(Compiler *compiler);
22+
static CompilerValue *compileTurnLeft(Compiler *compiler);
2223
};
2324

2425
} // namespace libscratchcpp

test/blocks/motion_blocks_test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,33 @@ TEST_F(MotionBlocksTest, TurnRight)
8888
builder.run();
8989
}
9090
}
91+
92+
TEST_F(MotionBlocksTest, TurnLeft)
93+
{
94+
{
95+
auto sprite = std::make_shared<Sprite>();
96+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
97+
98+
builder.addBlock("motion_turnleft");
99+
builder.addValueInput("DEGREES", 12.05);
100+
101+
sprite->setDirection(124.37);
102+
103+
builder.build();
104+
builder.run();
105+
ASSERT_EQ(std::round(sprite->direction() * 100) / 100, 112.32);
106+
}
107+
108+
m_engine->clear();
109+
110+
{
111+
auto stage = std::make_shared<Stage>();
112+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
113+
114+
builder.addBlock("motion_turnleft");
115+
builder.addValueInput("DEGREES", 12.05);
116+
117+
builder.build();
118+
builder.run();
119+
}
120+
}

0 commit comments

Comments
 (0)