Skip to content

Commit 57855e8

Browse files
committed
Implement motion_direction block
1 parent edd51ca commit 57855e8

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/blocks/motionblocks.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
5656
engine->addCompileFunction(this, "motion_setrotationstyle", &compileSetRotationStyle);
5757
engine->addCompileFunction(this, "motion_xposition", &compileXPosition);
5858
engine->addCompileFunction(this, "motion_yposition", &compileYPosition);
59+
engine->addCompileFunction(this, "motion_direction", &compileDirection);
5960
}
6061

6162
CompilerValue *MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -345,6 +346,14 @@ CompilerValue *MotionBlocks::compileYPosition(Compiler *compiler)
345346
return compiler->addTargetFunctionCall("motion_yposition", Compiler::StaticType::Number);
346347
}
347348

349+
CompilerValue *MotionBlocks::compileDirection(Compiler *compiler)
350+
{
351+
if (compiler->target()->isStage())
352+
return compiler->addConstValue(90);
353+
else
354+
return compiler->addTargetFunctionCall("motion_direction", Compiler::StaticType::Number);
355+
}
356+
348357
extern "C" void motion_movesteps(Sprite *sprite, double steps)
349358
{
350359
double dir = sprite->direction();
@@ -737,3 +746,8 @@ extern "C" double motion_yposition(Sprite *sprite)
737746
{
738747
return sprite->y();
739748
}
749+
750+
extern "C" double motion_direction(Sprite *sprite)
751+
{
752+
return sprite->direction();
753+
}

src/blocks/motionblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class MotionBlocks : public IExtension
3434
static CompilerValue *compileSetRotationStyle(Compiler *compiler);
3535
static CompilerValue *compileXPosition(Compiler *compiler);
3636
static CompilerValue *compileYPosition(Compiler *compiler);
37+
static CompilerValue *compileDirection(Compiler *compiler);
3738
};
3839

3940
} // namespace libscratchcpp

test/blocks/motion_blocks_test.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,3 +1979,41 @@ TEST_F(MotionBlocksTest, YPosition)
19791979
ASSERT_EQ(Value(list->data()[0]).toDouble(), 0);
19801980
}
19811981
}
1982+
1983+
TEST_F(MotionBlocksTest, Direction)
1984+
{
1985+
{
1986+
auto sprite = std::make_shared<Sprite>();
1987+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1988+
1989+
builder.addBlock("motion_direction");
1990+
builder.captureBlockReturnValue();
1991+
builder.build();
1992+
1993+
sprite->setX(5.2);
1994+
sprite->setY(-0.25);
1995+
sprite->setDirection(-61.42);
1996+
builder.run();
1997+
1998+
List *list = builder.capturedValues();
1999+
ASSERT_EQ(list->size(), 1);
2000+
ASSERT_EQ(Value(list->data()[0]).toDouble(), -61.42);
2001+
}
2002+
2003+
m_engine->clear();
2004+
2005+
{
2006+
auto stage = std::make_shared<Stage>();
2007+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
2008+
2009+
builder.addBlock("motion_direction");
2010+
builder.captureBlockReturnValue();
2011+
2012+
builder.build();
2013+
builder.run();
2014+
2015+
List *list = builder.capturedValues();
2016+
ASSERT_EQ(list->size(), 1);
2017+
ASSERT_EQ(Value(list->data()[0]).toDouble(), 90);
2018+
}
2019+
}

0 commit comments

Comments
 (0)