Skip to content

Commit 2cb41f4

Browse files
committed
Implement motion_setx block
1 parent 1c5425e commit 2cb41f4

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
@@ -34,6 +34,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
3434
engine->addCompileFunction(this, "motion_glidesecstoxy", &compileGlideSecsToXY);
3535
engine->addCompileFunction(this, "motion_glideto", &compileGlideTo);
3636
engine->addCompileFunction(this, "motion_changexby", &compileChangeXBy);
37+
engine->addCompileFunction(this, "motion_setx", &compileSetX);
3738

3839
// Inputs
3940
engine->addInput(this, "STEPS", STEPS);
@@ -166,6 +167,12 @@ void MotionBlocks::compileChangeXBy(Compiler *compiler)
166167
compiler->addFunctionCall(&changeXBy);
167168
}
168169

170+
void MotionBlocks::compileSetX(Compiler *compiler)
171+
{
172+
compiler->addInput(X);
173+
compiler->addFunctionCall(&setX);
174+
}
175+
169176
unsigned int MotionBlocks::moveSteps(VirtualMachine *vm)
170177
{
171178
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
@@ -537,3 +544,13 @@ unsigned int MotionBlocks::changeXBy(VirtualMachine *vm)
537544

538545
return 1;
539546
}
547+
548+
unsigned int MotionBlocks::setX(VirtualMachine *vm)
549+
{
550+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
551+
552+
if (sprite)
553+
sprite->setX(vm->getInput(0, 1)->toDouble());
554+
555+
return 1;
556+
}

src/blocks/motionblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class MotionBlocks : public IBlockSection
5353
static void compileGlideSecsToXY(Compiler *compiler);
5454
static void compileGlideTo(Compiler *compiler);
5555
static void compileChangeXBy(Compiler *compiler);
56+
static void compileSetX(Compiler *compiler);
5657

5758
static unsigned int moveSteps(VirtualMachine *vm);
5859
static unsigned int turnRight(VirtualMachine *vm);
@@ -83,6 +84,7 @@ class MotionBlocks : public IBlockSection
8384
static unsigned int startGlideToRandomPosition(VirtualMachine *vm);
8485

8586
static unsigned int changeXBy(VirtualMachine *vm);
87+
static unsigned int setX(VirtualMachine *vm);
8688

8789
static IRandomGenerator *rng;
8890
static IClock *clock;

test/blocks/motion_blocks_test.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ TEST_F(MotionBlocksTest, RegisterBlocks)
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));
109109
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_changexby", &MotionBlocks::compileChangeXBy));
110+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_setx", &MotionBlocks::compileSetX));
110111

111112
// Inputs
112113
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "STEPS", MotionBlocks::STEPS));
@@ -961,3 +962,42 @@ TEST_F(MotionBlocksTest, ChangeXByImpl)
961962
ASSERT_EQ(vm.registerCount(), 0);
962963
ASSERT_EQ(sprite.x(), 33.14);
963964
}
965+
966+
TEST_F(MotionBlocksTest, SetX)
967+
{
968+
Compiler compiler(&m_engineMock);
969+
970+
// set x to (-63.4)
971+
auto block = std::make_shared<Block>("a", "motion_setx");
972+
addValueInput(block, "X", MotionBlocks::X, -63.4);
973+
974+
EXPECT_CALL(m_engineMock, functionIndex(&MotionBlocks::setX)).WillOnce(Return(0));
975+
976+
compiler.init();
977+
compiler.setBlock(block);
978+
MotionBlocks::compileSetX(&compiler);
979+
compiler.end();
980+
981+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT }));
982+
ASSERT_EQ(compiler.constValues().size(), 1);
983+
ASSERT_EQ(compiler.constValues()[0].toDouble(), -63.4);
984+
}
985+
986+
TEST_F(MotionBlocksTest, SetXImpl)
987+
{
988+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
989+
static BlockFunc functions[] = { &MotionBlocks::setX };
990+
static Value constValues[] = { -63.4 };
991+
992+
Sprite sprite;
993+
sprite.setX(239.98);
994+
995+
VirtualMachine vm(&sprite, nullptr, nullptr);
996+
vm.setBytecode(bytecode);
997+
vm.setFunctions(functions);
998+
vm.setConstValues(constValues);
999+
vm.run();
1000+
1001+
ASSERT_EQ(vm.registerCount(), 0);
1002+
ASSERT_EQ(sprite.x(), -63.4);
1003+
}

0 commit comments

Comments
 (0)