File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed
Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
5555 engine->addCompileFunction (this , " motion_ifonedgebounce" , &compileIfOnEdgeBounce);
5656 engine->addCompileFunction (this , " motion_setrotationstyle" , &compileSetRotationStyle);
5757 engine->addCompileFunction (this , " motion_xposition" , &compileXPosition);
58+ engine->addCompileFunction (this , " motion_yposition" , &compileYPosition);
5859}
5960
6061CompilerValue *MotionBlocks::compileMoveSteps (Compiler *compiler)
@@ -336,6 +337,14 @@ CompilerValue *MotionBlocks::compileXPosition(Compiler *compiler)
336337 return compiler->addTargetFunctionCall (" motion_xposition" , Compiler::StaticType::Number);
337338}
338339
340+ CompilerValue *MotionBlocks::compileYPosition (Compiler *compiler)
341+ {
342+ if (compiler->target ()->isStage ())
343+ return compiler->addConstValue (0 );
344+ else
345+ return compiler->addTargetFunctionCall (" motion_yposition" , Compiler::StaticType::Number);
346+ }
347+
339348extern " C" void motion_movesteps (Sprite *sprite, double steps)
340349{
341350 double dir = sprite->direction ();
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ class MotionBlocks : public IExtension
3333 static CompilerValue *compileIfOnEdgeBounce (Compiler *compiler);
3434 static CompilerValue *compileSetRotationStyle (Compiler *compiler);
3535 static CompilerValue *compileXPosition (Compiler *compiler);
36+ static CompilerValue *compileYPosition (Compiler *compiler);
3637};
3738
3839} // namespace libscratchcpp
Original file line number Diff line number Diff line change @@ -1941,3 +1941,41 @@ TEST_F(MotionBlocksTest, XPosition)
19411941 ASSERT_EQ (Value (list->data ()[0 ]).toDouble (), 0 );
19421942 }
19431943}
1944+
1945+ TEST_F (MotionBlocksTest, YPosition)
1946+ {
1947+ {
1948+ auto sprite = std::make_shared<Sprite>();
1949+ ScriptBuilder builder (m_extension.get (), m_engine, sprite);
1950+
1951+ builder.addBlock (" motion_yposition" );
1952+ builder.captureBlockReturnValue ();
1953+ builder.build ();
1954+
1955+ sprite->setX (5.2 );
1956+ sprite->setY (-0.25 );
1957+ sprite->setDirection (-61.42 );
1958+ builder.run ();
1959+
1960+ List *list = builder.capturedValues ();
1961+ ASSERT_EQ (list->size (), 1 );
1962+ ASSERT_EQ (Value (list->data ()[0 ]).toDouble (), -0.25 );
1963+ }
1964+
1965+ m_engine->clear ();
1966+
1967+ {
1968+ auto stage = std::make_shared<Stage>();
1969+ ScriptBuilder builder (m_extension.get (), m_engine, stage);
1970+
1971+ builder.addBlock (" motion_yposition" );
1972+ builder.captureBlockReturnValue ();
1973+
1974+ builder.build ();
1975+ builder.run ();
1976+
1977+ List *list = builder.capturedValues ();
1978+ ASSERT_EQ (list->size (), 1 );
1979+ ASSERT_EQ (Value (list->data ()[0 ]).toDouble (), 0 );
1980+ }
1981+ }
You can’t perform that action at this time.
0 commit comments