Skip to content

Commit 1c5425e

Browse files
committed
Implement motion_changexby block
1 parent e789021 commit 1c5425e

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/blocks/motionblocks.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
3333
engine->addCompileFunction(this, "motion_goto", &compileGoTo);
3434
engine->addCompileFunction(this, "motion_glidesecstoxy", &compileGlideSecsToXY);
3535
engine->addCompileFunction(this, "motion_glideto", &compileGlideTo);
36+
engine->addCompileFunction(this, "motion_changexby", &compileChangeXBy);
3637

3738
// Inputs
3839
engine->addInput(this, "STEPS", STEPS);
@@ -43,6 +44,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
4344
engine->addInput(this, "Y", Y);
4445
engine->addInput(this, "TO", TO);
4546
engine->addInput(this, "SECS", SECS);
47+
engine->addInput(this, "DX", DX);
4648
}
4749

4850
void MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -158,6 +160,12 @@ void MotionBlocks::compileGlideTo(Compiler *compiler)
158160
compiler->addFunctionCall(&glideSecsTo);
159161
}
160162

163+
void MotionBlocks::compileChangeXBy(Compiler *compiler)
164+
{
165+
compiler->addInput(DX);
166+
compiler->addFunctionCall(&changeXBy);
167+
}
168+
161169
unsigned int MotionBlocks::moveSteps(VirtualMachine *vm)
162170
{
163171
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
@@ -519,3 +527,13 @@ unsigned int MotionBlocks::startGlideToRandomPosition(VirtualMachine *vm)
519527

520528
return 1;
521529
}
530+
531+
unsigned int MotionBlocks::changeXBy(VirtualMachine *vm)
532+
{
533+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
534+
535+
if (sprite)
536+
sprite->setX(sprite->x() + vm->getInput(0, 1)->toDouble());
537+
538+
return 1;
539+
}

src/blocks/motionblocks.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class MotionBlocks : public IBlockSection
2727
X,
2828
Y,
2929
TO,
30-
SECS
30+
SECS,
31+
DX
3132
};
3233

3334
enum Fields
@@ -51,6 +52,7 @@ class MotionBlocks : public IBlockSection
5152
static void compileGoTo(Compiler *compiler);
5253
static void compileGlideSecsToXY(Compiler *compiler);
5354
static void compileGlideTo(Compiler *compiler);
55+
static void compileChangeXBy(Compiler *compiler);
5456

5557
static unsigned int moveSteps(VirtualMachine *vm);
5658
static unsigned int turnRight(VirtualMachine *vm);
@@ -80,6 +82,8 @@ class MotionBlocks : public IBlockSection
8082
static unsigned int startGlideToMousePointer(VirtualMachine *vm);
8183
static unsigned int startGlideToRandomPosition(VirtualMachine *vm);
8284

85+
static unsigned int changeXBy(VirtualMachine *vm);
86+
8387
static IRandomGenerator *rng;
8488
static IClock *clock;
8589

test/blocks/motion_blocks_test.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ TEST_F(MotionBlocksTest, RegisterBlocks)
106106
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_goto", &MotionBlocks::compileGoTo));
107107
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_glidesecstoxy", &MotionBlocks::compileGlideSecsToXY));
108108
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_glideto", &MotionBlocks::compileGlideTo));
109+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_changexby", &MotionBlocks::compileChangeXBy));
109110

110111
// Inputs
111112
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "STEPS", MotionBlocks::STEPS));
@@ -116,6 +117,7 @@ TEST_F(MotionBlocksTest, RegisterBlocks)
116117
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "Y", MotionBlocks::Y));
117118
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "TO", MotionBlocks::TO));
118119
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "SECS", MotionBlocks::SECS));
120+
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "DX", MotionBlocks::DX));
119121

120122
m_section->registerBlocks(&m_engineMock);
121123
}
@@ -920,3 +922,42 @@ TEST_F(MotionBlocksTest, GlideToImpl)
920922
MotionBlocks::clock = Clock::instance().get();
921923
MotionBlocks::rng = RandomGenerator::instance().get();
922924
}
925+
926+
TEST_F(MotionBlocksTest, ChangeXBy)
927+
{
928+
Compiler compiler(&m_engineMock);
929+
930+
// change x by (56.54)
931+
auto block = std::make_shared<Block>("a", "motion_changexby");
932+
addValueInput(block, "DX", MotionBlocks::DX, 56.54);
933+
934+
EXPECT_CALL(m_engineMock, functionIndex(&MotionBlocks::changeXBy)).WillOnce(Return(0));
935+
936+
compiler.init();
937+
compiler.setBlock(block);
938+
MotionBlocks::compileChangeXBy(&compiler);
939+
compiler.end();
940+
941+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT }));
942+
ASSERT_EQ(compiler.constValues().size(), 1);
943+
ASSERT_EQ(compiler.constValues()[0].toDouble(), 56.54);
944+
}
945+
946+
TEST_F(MotionBlocksTest, ChangeXByImpl)
947+
{
948+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
949+
static BlockFunc functions[] = { &MotionBlocks::changeXBy };
950+
static Value constValues[] = { 56.54 };
951+
952+
Sprite sprite;
953+
sprite.setX(-23.4);
954+
955+
VirtualMachine vm(&sprite, nullptr, nullptr);
956+
vm.setBytecode(bytecode);
957+
vm.setFunctions(functions);
958+
vm.setConstValues(constValues);
959+
vm.run();
960+
961+
ASSERT_EQ(vm.registerCount(), 0);
962+
ASSERT_EQ(sprite.x(), 33.14);
963+
}

0 commit comments

Comments
 (0)