Skip to content

Commit 7ccafe3

Browse files
committed
Implement motion_changeyby block
1 parent 2cb41f4 commit 7ccafe3

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
@@ -35,6 +35,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
3535
engine->addCompileFunction(this, "motion_glideto", &compileGlideTo);
3636
engine->addCompileFunction(this, "motion_changexby", &compileChangeXBy);
3737
engine->addCompileFunction(this, "motion_setx", &compileSetX);
38+
engine->addCompileFunction(this, "motion_changeyby", &compileChangeYBy);
3839

3940
// Inputs
4041
engine->addInput(this, "STEPS", STEPS);
@@ -46,6 +47,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
4647
engine->addInput(this, "TO", TO);
4748
engine->addInput(this, "SECS", SECS);
4849
engine->addInput(this, "DX", DX);
50+
engine->addInput(this, "DY", DY);
4951
}
5052

5153
void MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -173,6 +175,12 @@ void MotionBlocks::compileSetX(Compiler *compiler)
173175
compiler->addFunctionCall(&setX);
174176
}
175177

178+
void MotionBlocks::compileChangeYBy(Compiler *compiler)
179+
{
180+
compiler->addInput(DY);
181+
compiler->addFunctionCall(&changeYBy);
182+
}
183+
176184
unsigned int MotionBlocks::moveSteps(VirtualMachine *vm)
177185
{
178186
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
@@ -554,3 +562,13 @@ unsigned int MotionBlocks::setX(VirtualMachine *vm)
554562

555563
return 1;
556564
}
565+
566+
unsigned int MotionBlocks::changeYBy(VirtualMachine *vm)
567+
{
568+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
569+
570+
if (sprite)
571+
sprite->setY(sprite->y() + vm->getInput(0, 1)->toDouble());
572+
573+
return 1;
574+
}

src/blocks/motionblocks.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class MotionBlocks : public IBlockSection
2828
Y,
2929
TO,
3030
SECS,
31-
DX
31+
DX,
32+
DY
3233
};
3334

3435
enum Fields
@@ -54,6 +55,7 @@ class MotionBlocks : public IBlockSection
5455
static void compileGlideTo(Compiler *compiler);
5556
static void compileChangeXBy(Compiler *compiler);
5657
static void compileSetX(Compiler *compiler);
58+
static void compileChangeYBy(Compiler *compiler);
5759

5860
static unsigned int moveSteps(VirtualMachine *vm);
5961
static unsigned int turnRight(VirtualMachine *vm);
@@ -85,6 +87,7 @@ class MotionBlocks : public IBlockSection
8587

8688
static unsigned int changeXBy(VirtualMachine *vm);
8789
static unsigned int setX(VirtualMachine *vm);
90+
static unsigned int changeYBy(VirtualMachine *vm);
8891

8992
static IRandomGenerator *rng;
9093
static IClock *clock;

test/blocks/motion_blocks_test.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ TEST_F(MotionBlocksTest, RegisterBlocks)
108108
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_glideto", &MotionBlocks::compileGlideTo));
109109
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_changexby", &MotionBlocks::compileChangeXBy));
110110
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_setx", &MotionBlocks::compileSetX));
111+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_changeyby", &MotionBlocks::compileChangeYBy));
111112

112113
// Inputs
113114
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "STEPS", MotionBlocks::STEPS));
@@ -119,6 +120,7 @@ TEST_F(MotionBlocksTest, RegisterBlocks)
119120
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "TO", MotionBlocks::TO));
120121
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "SECS", MotionBlocks::SECS));
121122
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "DX", MotionBlocks::DX));
123+
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "DY", MotionBlocks::DY));
122124

123125
m_section->registerBlocks(&m_engineMock);
124126
}
@@ -1001,3 +1003,42 @@ TEST_F(MotionBlocksTest, SetXImpl)
10011003
ASSERT_EQ(vm.registerCount(), 0);
10021004
ASSERT_EQ(sprite.x(), -63.4);
10031005
}
1006+
1007+
TEST_F(MotionBlocksTest, ChangeYBy)
1008+
{
1009+
Compiler compiler(&m_engineMock);
1010+
1011+
// change y by (135.2)
1012+
auto block = std::make_shared<Block>("a", "motion_changeyby");
1013+
addValueInput(block, "DY", MotionBlocks::DY, 135.2);
1014+
1015+
EXPECT_CALL(m_engineMock, functionIndex(&MotionBlocks::changeYBy)).WillOnce(Return(0));
1016+
1017+
compiler.init();
1018+
compiler.setBlock(block);
1019+
MotionBlocks::compileChangeYBy(&compiler);
1020+
compiler.end();
1021+
1022+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT }));
1023+
ASSERT_EQ(compiler.constValues().size(), 1);
1024+
ASSERT_EQ(compiler.constValues()[0].toDouble(), 135.2);
1025+
}
1026+
1027+
TEST_F(MotionBlocksTest, ChangeYByImpl)
1028+
{
1029+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
1030+
static BlockFunc functions[] = { &MotionBlocks::changeYBy };
1031+
static Value constValues[] = { 135.2 };
1032+
1033+
Sprite sprite;
1034+
sprite.setY(200.657);
1035+
1036+
VirtualMachine vm(&sprite, nullptr, nullptr);
1037+
vm.setBytecode(bytecode);
1038+
vm.setFunctions(functions);
1039+
vm.setConstValues(constValues);
1040+
vm.run();
1041+
1042+
ASSERT_EQ(vm.registerCount(), 0);
1043+
ASSERT_EQ(std::round(sprite.y() * 1000) / 1000, 335.857);
1044+
}

0 commit comments

Comments
 (0)