Skip to content

Commit d4fc993

Browse files
committed
Implement motion_pointindirection block
1 parent 9ceee87 commit d4fc993

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)
@@ -63,6 +64,16 @@ CompilerValue *MotionBlocks::compileTurnLeft(Compiler *compiler)
6364
return nullptr;
6465
}
6566

67+
CompilerValue *MotionBlocks::compilePointInDirection(Compiler *compiler)
68+
{
69+
if (!compiler->target()->isStage()) {
70+
CompilerValue *direction = compiler->addInput("DIRECTION");
71+
compiler->addTargetFunctionCall("motion_pointindirection", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { direction });
72+
}
73+
74+
return nullptr;
75+
}
76+
6677
extern "C" void motion_movesteps(Sprite *sprite, double steps)
6778
{
6879
double dir = sprite->direction();
@@ -78,3 +89,8 @@ extern "C" void motion_turnleft(Sprite *sprite, double degrees)
7889
{
7990
sprite->setDirection(sprite->direction() - degrees);
8091
}
92+
93+
extern "C" void motion_pointindirection(Sprite *sprite, double direction)
94+
{
95+
sprite->setDirection(direction);
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)