Skip to content

Commit 5c12d10

Browse files
committed
Implement motion_sety block
1 parent 9e4e6fd commit 5c12d10

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
@@ -36,6 +36,7 @@ void MotionBlocks::registerBlocks(IEngine *engine)
3636
engine->addCompileFunction(this, "motion_changexby", &compileChangeXBy);
3737
engine->addCompileFunction(this, "motion_setx", &compileSetX);
3838
engine->addCompileFunction(this, "motion_changeyby", &compileChangeYBy);
39+
engine->addCompileFunction(this, "motion_sety", &compileSetY);
3940

4041
// Inputs
4142
engine->addInput(this, "STEPS", STEPS);
@@ -181,6 +182,12 @@ void MotionBlocks::compileChangeYBy(Compiler *compiler)
181182
compiler->addFunctionCall(&changeYBy);
182183
}
183184

185+
void MotionBlocks::compileSetY(Compiler *compiler)
186+
{
187+
compiler->addInput(Y);
188+
compiler->addFunctionCall(&setY);
189+
}
190+
184191
unsigned int MotionBlocks::moveSteps(VirtualMachine *vm)
185192
{
186193
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
@@ -572,3 +579,13 @@ unsigned int MotionBlocks::changeYBy(VirtualMachine *vm)
572579

573580
return 1;
574581
}
582+
583+
unsigned int MotionBlocks::setY(VirtualMachine *vm)
584+
{
585+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
586+
587+
if (sprite)
588+
sprite->setY(vm->getInput(0, 1)->toDouble());
589+
590+
return 1;
591+
}

src/blocks/motionblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class MotionBlocks : public IBlockSection
5656
static void compileChangeXBy(Compiler *compiler);
5757
static void compileSetX(Compiler *compiler);
5858
static void compileChangeYBy(Compiler *compiler);
59+
static void compileSetY(Compiler *compiler);
5960

6061
static unsigned int moveSteps(VirtualMachine *vm);
6162
static unsigned int turnRight(VirtualMachine *vm);
@@ -88,6 +89,7 @@ class MotionBlocks : public IBlockSection
8889
static unsigned int changeXBy(VirtualMachine *vm);
8990
static unsigned int setX(VirtualMachine *vm);
9091
static unsigned int changeYBy(VirtualMachine *vm);
92+
static unsigned int setY(VirtualMachine *vm);
9193

9294
static IRandomGenerator *rng;
9395
static IClock *clock;

test/blocks/motion_blocks_test.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ TEST_F(MotionBlocksTest, RegisterBlocks)
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));
111111
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_changeyby", &MotionBlocks::compileChangeYBy));
112+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_sety", &MotionBlocks::compileSetY));
112113

113114
// Inputs
114115
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "STEPS", MotionBlocks::STEPS));
@@ -1042,3 +1043,42 @@ TEST_F(MotionBlocksTest, ChangeYByImpl)
10421043
ASSERT_EQ(vm.registerCount(), 0);
10431044
ASSERT_EQ(std::round(sprite.y() * 1000) / 1000, 335.857);
10441045
}
1046+
1047+
TEST_F(MotionBlocksTest, SetY)
1048+
{
1049+
Compiler compiler(&m_engineMock);
1050+
1051+
// set y to (189.42)
1052+
auto block = std::make_shared<Block>("a", "motion_sety");
1053+
addValueInput(block, "Y", MotionBlocks::Y, 189.42);
1054+
1055+
EXPECT_CALL(m_engineMock, functionIndex(&MotionBlocks::setY)).WillOnce(Return(0));
1056+
1057+
compiler.init();
1058+
compiler.setBlock(block);
1059+
MotionBlocks::compileSetY(&compiler);
1060+
compiler.end();
1061+
1062+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT }));
1063+
ASSERT_EQ(compiler.constValues().size(), 1);
1064+
ASSERT_EQ(compiler.constValues()[0].toDouble(), 189.42);
1065+
}
1066+
1067+
TEST_F(MotionBlocksTest, SetYImpl)
1068+
{
1069+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
1070+
static BlockFunc functions[] = { &MotionBlocks::setY };
1071+
static Value constValues[] = { 189.42 };
1072+
1073+
Sprite sprite;
1074+
sprite.setY(-15.056);
1075+
1076+
VirtualMachine vm(&sprite, nullptr, nullptr);
1077+
vm.setBytecode(bytecode);
1078+
vm.setFunctions(functions);
1079+
vm.setConstValues(constValues);
1080+
vm.run();
1081+
1082+
ASSERT_EQ(vm.registerCount(), 0);
1083+
ASSERT_EQ(sprite.y(), 189.42);
1084+
}

0 commit comments

Comments
 (0)