Skip to content

Commit 2f55044

Browse files
committed
Implement motion_pointindirection block
1 parent 522dbb7 commit 2f55044

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
@@ -31,6 +31,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
3131
engine->addCompileFunction(this, "motion_movesteps", &compileMoveSteps);
3232
engine->addCompileFunction(this, "motion_turnright", &compileTurnRight);
3333
engine->addCompileFunction(this, "motion_turnleft", &compileTurnLeft);
34+
engine->addCompileFunction(this, "motion_pointindirection", &compilePointInDirection);
3435
}
3536

3637
CompilerValue *MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -54,6 +55,13 @@ CompilerValue *MotionBlocks::compileTurnLeft(Compiler *compiler)
5455
return nullptr;
5556
}
5657

58+
CompilerValue *MotionBlocks::compilePointInDirection(Compiler *compiler)
59+
{
60+
CompilerValue *direction = compiler->addInput("DIRECTION");
61+
compiler->addTargetFunctionCall("motion_pointindirection", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { direction });
62+
return nullptr;
63+
}
64+
5765
extern "C" void motion_movesteps(Target *target, double steps)
5866
{
5967
if (!target->isStage()) {
@@ -78,3 +86,11 @@ extern "C" void motion_turnleft(Target *target, double degrees)
7886
sprite->setDirection(sprite->direction() - degrees);
7987
}
8088
}
89+
90+
extern "C" void motion_pointindirection(Target *target, double direction)
91+
{
92+
if (!target->isStage()) {
93+
Sprite *sprite = static_cast<Sprite *>(target);
94+
sprite->setDirection(direction);
95+
}
96+
}

src/blocks/motionblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class MotionBlocks : public IExtension
2020
static CompilerValue *compileMoveSteps(Compiler *compiler);
2121
static CompilerValue *compileTurnRight(Compiler *compiler);
2222
static CompilerValue *compileTurnLeft(Compiler *compiler);
23+
static CompilerValue *compilePointInDirection(Compiler *compiler);
2324
};
2425

2526
} // namespace libscratchcpp

test/blocks/motion_blocks_test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,33 @@ TEST_F(MotionBlocksTest, TurnLeft)
118118
builder.run();
119119
}
120120
}
121+
122+
TEST_F(MotionBlocksTest, PointInDirection)
123+
{
124+
{
125+
auto sprite = std::make_shared<Sprite>();
126+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
127+
128+
builder.addBlock("motion_pointindirection");
129+
builder.addValueInput("DIRECTION", -60.5);
130+
131+
sprite->setDirection(50.02);
132+
133+
builder.build();
134+
builder.run();
135+
ASSERT_EQ(sprite->direction(), -60.5);
136+
}
137+
138+
m_engine->clear();
139+
140+
{
141+
auto stage = std::make_shared<Stage>();
142+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
143+
144+
builder.addBlock("motion_pointindirection");
145+
builder.addValueInput("DIRECTION", -60.5);
146+
147+
builder.build();
148+
builder.run();
149+
}
150+
}

0 commit comments

Comments
 (0)