Skip to content

Commit c109fc7

Browse files
committed
Implement motion_xposition block
1 parent 0c39def commit c109fc7

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/blocks/motionblocks.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
5454
engine->addCompileFunction(this, "motion_sety", &compileSetY);
5555
engine->addCompileFunction(this, "motion_ifonedgebounce", &compileIfOnEdgeBounce);
5656
engine->addCompileFunction(this, "motion_setrotationstyle", &compileSetRotationStyle);
57+
engine->addCompileFunction(this, "motion_xposition", &compileXPosition);
5758
}
5859

5960
CompilerValue *MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -327,6 +328,14 @@ CompilerValue *MotionBlocks::compileSetRotationStyle(Compiler *compiler)
327328
return nullptr;
328329
}
329330

331+
CompilerValue *MotionBlocks::compileXPosition(Compiler *compiler)
332+
{
333+
if (compiler->target()->isStage())
334+
return compiler->addConstValue(0);
335+
else
336+
return compiler->addTargetFunctionCall("motion_xposition", Compiler::StaticType::Number);
337+
}
338+
330339
extern "C" void motion_movesteps(Sprite *sprite, double steps)
331340
{
332341
double dir = sprite->direction();

src/blocks/motionblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class MotionBlocks : public IExtension
3232
static CompilerValue *compileSetY(Compiler *compiler);
3333
static CompilerValue *compileIfOnEdgeBounce(Compiler *compiler);
3434
static CompilerValue *compileSetRotationStyle(Compiler *compiler);
35+
static CompilerValue *compileXPosition(Compiler *compiler);
3536
};
3637

3738
} // namespace libscratchcpp

test/blocks/motion_blocks_test.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <scratchcpp/thread.h>
88
#include <scratchcpp/executablecode.h>
99
#include <scratchcpp/executioncontext.h>
10+
#include <scratchcpp/list.h>
1011
#include <enginemock.h>
1112
#include <randomgeneratormock.h>
1213
#include <stacktimermock.h>
@@ -1902,3 +1903,41 @@ TEST_F(MotionBlocksTest, SetRotationStyle)
19021903
builder.run();
19031904
}
19041905
}
1906+
1907+
TEST_F(MotionBlocksTest, XPosition)
1908+
{
1909+
{
1910+
auto sprite = std::make_shared<Sprite>();
1911+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1912+
1913+
builder.addBlock("motion_xposition");
1914+
builder.captureBlockReturnValue();
1915+
builder.build();
1916+
1917+
sprite->setX(5.2);
1918+
sprite->setY(-0.25);
1919+
sprite->setDirection(-61.42);
1920+
builder.run();
1921+
1922+
List *list = builder.capturedValues();
1923+
ASSERT_EQ(list->size(), 1);
1924+
ASSERT_EQ(Value(list->data()[0]).toDouble(), 5.2);
1925+
}
1926+
1927+
m_engine->clear();
1928+
1929+
{
1930+
auto stage = std::make_shared<Stage>();
1931+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
1932+
1933+
builder.addBlock("motion_xposition");
1934+
builder.captureBlockReturnValue();
1935+
1936+
builder.build();
1937+
builder.run();
1938+
1939+
List *list = builder.capturedValues();
1940+
ASSERT_EQ(list->size(), 1);
1941+
ASSERT_EQ(Value(list->data()[0]).toDouble(), 0);
1942+
}
1943+
}

0 commit comments

Comments
 (0)