Skip to content

Commit cc81033

Browse files
committed
Implement motion_turnright block
1 parent d3a2c9c commit cc81033

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

src/blocks/motionblocks.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ void MotionBlocks::registerBlocks(IEngine *engine)
1919
{
2020
// Blocks
2121
engine->addCompileFunction(this, "motion_movesteps", &compileMoveSteps);
22+
engine->addCompileFunction(this, "motion_turnright", &compileTurnRight);
2223

2324
// Inputs
2425
engine->addInput(this, "STEPS", STEPS);
26+
engine->addInput(this, "DEGREES", DEGREES);
2527
}
2628

2729
void MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -30,6 +32,12 @@ void MotionBlocks::compileMoveSteps(Compiler *compiler)
3032
compiler->addFunctionCall(&moveSteps);
3133
}
3234

35+
void MotionBlocks::compileTurnRight(Compiler *compiler)
36+
{
37+
compiler->addInput(DEGREES);
38+
compiler->addFunctionCall(&turnRight);
39+
}
40+
3341
unsigned int MotionBlocks::moveSteps(VirtualMachine *vm)
3442
{
3543
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
@@ -43,3 +51,13 @@ unsigned int MotionBlocks::moveSteps(VirtualMachine *vm)
4351

4452
return 1;
4553
}
54+
55+
unsigned int MotionBlocks::turnRight(VirtualMachine *vm)
56+
{
57+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
58+
59+
if (sprite)
60+
sprite->setDirection(sprite->direction() + vm->getInput(0, 1)->toDouble());
61+
62+
return 1;
63+
}

src/blocks/motionblocks.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ class MotionBlocks : public IBlockSection
1313
public:
1414
enum Inputs
1515
{
16-
STEPS
16+
STEPS,
17+
DEGREES
1718
};
1819

1920
std::string name() const override;
2021

2122
void registerBlocks(IEngine *engine) override;
2223

2324
static void compileMoveSteps(Compiler *compiler);
25+
static void compileTurnRight(Compiler *compiler);
2426

2527
static unsigned int moveSteps(VirtualMachine *vm);
28+
static unsigned int turnRight(VirtualMachine *vm);
2629
};
2730

2831
} // namespace libscratchcpp

test/blocks/motion_blocks_test.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ TEST_F(MotionBlocksTest, RegisterBlocks)
5252
{
5353
// Blocks
5454
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_movesteps", &MotionBlocks::compileMoveSteps));
55+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_turnright", &MotionBlocks::compileTurnRight));
5556

5657
// Inputs
5758
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "STEPS", MotionBlocks::STEPS));
59+
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "DEGREES", MotionBlocks::DEGREES));
5860

5961
m_section->registerBlocks(&m_engineMock);
6062
}
@@ -100,3 +102,42 @@ TEST_F(MotionBlocksTest, MoveStepsImpl)
100102
ASSERT_EQ(std::round(sprite.x() * 100) / 100, -21.36);
101103
ASSERT_EQ(std::round(sprite.y() * 100) / 100, 14.22);
102104
}
105+
106+
TEST_F(MotionBlocksTest, TurnRight)
107+
{
108+
Compiler compiler(&m_engineMock);
109+
110+
// turn right (12.05) degrees
111+
auto block = std::make_shared<Block>("a", "motion_turnright");
112+
addValueInput(block, "DEGREES", MotionBlocks::DEGREES, 12.05);
113+
114+
EXPECT_CALL(m_engineMock, functionIndex(&MotionBlocks::turnRight)).WillOnce(Return(0));
115+
116+
compiler.init();
117+
compiler.setBlock(block);
118+
MotionBlocks::compileTurnRight(&compiler);
119+
compiler.end();
120+
121+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT }));
122+
ASSERT_EQ(compiler.constValues().size(), 1);
123+
ASSERT_EQ(compiler.constValues()[0].toDouble(), 12.05);
124+
}
125+
126+
TEST_F(MotionBlocksTest, TurnRightImpl)
127+
{
128+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
129+
static BlockFunc functions[] = { &MotionBlocks::turnRight };
130+
static Value constValues[] = { 12.05 };
131+
132+
Sprite sprite;
133+
sprite.setDirection(124.37);
134+
135+
VirtualMachine vm(&sprite, nullptr, nullptr);
136+
vm.setBytecode(bytecode);
137+
vm.setFunctions(functions);
138+
vm.setConstValues(constValues);
139+
vm.run();
140+
141+
ASSERT_EQ(vm.registerCount(), 0);
142+
ASSERT_EQ(std::round(sprite.direction() * 100) / 100, 136.42);
143+
}

0 commit comments

Comments
 (0)