Skip to content

Commit 16fed38

Browse files
committed
Implement motion_turnleft block
1 parent cc81033 commit 16fed38

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/blocks/motionblocks.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
2020
// Blocks
2121
engine->addCompileFunction(this, "motion_movesteps", &compileMoveSteps);
2222
engine->addCompileFunction(this, "motion_turnright", &compileTurnRight);
23+
engine->addCompileFunction(this, "motion_turnleft", &compileTurnLeft);
2324

2425
// Inputs
2526
engine->addInput(this, "STEPS", STEPS);
@@ -38,6 +39,12 @@ void MotionBlocks::compileTurnRight(Compiler *compiler)
3839
compiler->addFunctionCall(&turnRight);
3940
}
4041

42+
void MotionBlocks::compileTurnLeft(Compiler *compiler)
43+
{
44+
compiler->addInput(DEGREES);
45+
compiler->addFunctionCall(&turnLeft);
46+
}
47+
4148
unsigned int MotionBlocks::moveSteps(VirtualMachine *vm)
4249
{
4350
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
@@ -61,3 +68,13 @@ unsigned int MotionBlocks::turnRight(VirtualMachine *vm)
6168

6269
return 1;
6370
}
71+
72+
unsigned int MotionBlocks::turnLeft(VirtualMachine *vm)
73+
{
74+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
75+
76+
if (sprite)
77+
sprite->setDirection(sprite->direction() - vm->getInput(0, 1)->toDouble());
78+
79+
return 1;
80+
}

src/blocks/motionblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ class MotionBlocks : public IBlockSection
2323

2424
static void compileMoveSteps(Compiler *compiler);
2525
static void compileTurnRight(Compiler *compiler);
26+
static void compileTurnLeft(Compiler *compiler);
2627

2728
static unsigned int moveSteps(VirtualMachine *vm);
2829
static unsigned int turnRight(VirtualMachine *vm);
30+
static unsigned int turnLeft(VirtualMachine *vm);
2931
};
3032

3133
} // namespace libscratchcpp

test/blocks/motion_blocks_test.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ TEST_F(MotionBlocksTest, RegisterBlocks)
5353
// Blocks
5454
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_movesteps", &MotionBlocks::compileMoveSteps));
5555
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_turnright", &MotionBlocks::compileTurnRight));
56+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_turnleft", &MotionBlocks::compileTurnLeft));
5657

5758
// Inputs
5859
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "STEPS", MotionBlocks::STEPS));
@@ -141,3 +142,42 @@ TEST_F(MotionBlocksTest, TurnRightImpl)
141142
ASSERT_EQ(vm.registerCount(), 0);
142143
ASSERT_EQ(std::round(sprite.direction() * 100) / 100, 136.42);
143144
}
145+
146+
TEST_F(MotionBlocksTest, TurnLeft)
147+
{
148+
Compiler compiler(&m_engineMock);
149+
150+
// turn left (12.05) degrees
151+
auto block = std::make_shared<Block>("a", "motion_turnleft");
152+
addValueInput(block, "DEGREES", MotionBlocks::DEGREES, 12.05);
153+
154+
EXPECT_CALL(m_engineMock, functionIndex(&MotionBlocks::turnLeft)).WillOnce(Return(0));
155+
156+
compiler.init();
157+
compiler.setBlock(block);
158+
MotionBlocks::compileTurnLeft(&compiler);
159+
compiler.end();
160+
161+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT }));
162+
ASSERT_EQ(compiler.constValues().size(), 1);
163+
ASSERT_EQ(compiler.constValues()[0].toDouble(), 12.05);
164+
}
165+
166+
TEST_F(MotionBlocksTest, TurnLeftImpl)
167+
{
168+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
169+
static BlockFunc functions[] = { &MotionBlocks::turnLeft };
170+
static Value constValues[] = { 12.05 };
171+
172+
Sprite sprite;
173+
sprite.setDirection(124.37);
174+
175+
VirtualMachine vm(&sprite, nullptr, nullptr);
176+
vm.setBytecode(bytecode);
177+
vm.setFunctions(functions);
178+
vm.setConstValues(constValues);
179+
vm.run();
180+
181+
ASSERT_EQ(vm.registerCount(), 0);
182+
ASSERT_EQ(std::round(sprite.direction() * 100) / 100, 112.32);
183+
}

0 commit comments

Comments
 (0)