Skip to content

Commit 191d617

Browse files
committed
Implement motion_gotoxy block
1 parent 734a172 commit 191d617

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

src/blocks/motionblocks.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ void MotionBlocks::registerBlocks(IEngine *engine)
2727
engine->addCompileFunction(this, "motion_turnleft", &compileTurnLeft);
2828
engine->addCompileFunction(this, "motion_pointindirection", &compilePointInDirection);
2929
engine->addCompileFunction(this, "motion_pointtowards", &compilePointTowards);
30+
engine->addCompileFunction(this, "motion_gotoxy", &compileGoToXY);
3031

3132
// Inputs
3233
engine->addInput(this, "STEPS", STEPS);
3334
engine->addInput(this, "DEGREES", DEGREES);
3435
engine->addInput(this, "DIRECTION", DIRECTION);
3536
engine->addInput(this, "TOWARDS", TOWARDS);
37+
engine->addInput(this, "X", X);
38+
engine->addInput(this, "Y", Y);
3639
}
3740

3841
void MotionBlocks::compileMoveSteps(Compiler *compiler)
@@ -82,6 +85,13 @@ void MotionBlocks::compilePointTowards(Compiler *compiler)
8285
}
8386
}
8487

88+
void MotionBlocks::compileGoToXY(Compiler *compiler)
89+
{
90+
compiler->addInput(X);
91+
compiler->addInput(Y);
92+
compiler->addFunctionCall(&goToXY);
93+
}
94+
8595
unsigned int MotionBlocks::moveSteps(VirtualMachine *vm)
8696
{
8797
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
@@ -203,3 +213,15 @@ unsigned int MotionBlocks::pointTowardsRandomPosition(VirtualMachine *vm)
203213

204214
return 0;
205215
}
216+
217+
unsigned int MotionBlocks::goToXY(VirtualMachine *vm)
218+
{
219+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
220+
221+
if (sprite) {
222+
sprite->setX(vm->getInput(0, 2)->toDouble());
223+
sprite->setY(vm->getInput(1, 2)->toDouble());
224+
}
225+
226+
return 2;
227+
}

src/blocks/motionblocks.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ class MotionBlocks : public IBlockSection
1919
STEPS,
2020
DEGREES,
2121
DIRECTION,
22-
TOWARDS
22+
TOWARDS,
23+
X,
24+
Y
2325
};
2426

2527
enum Fields
@@ -39,6 +41,7 @@ class MotionBlocks : public IBlockSection
3941
static void compileTurnLeft(Compiler *compiler);
4042
static void compilePointInDirection(Compiler *compiler);
4143
static void compilePointTowards(Compiler *compiler);
44+
static void compileGoToXY(Compiler *compiler);
4245

4346
static unsigned int moveSteps(VirtualMachine *vm);
4447
static unsigned int turnRight(VirtualMachine *vm);
@@ -52,6 +55,8 @@ class MotionBlocks : public IBlockSection
5255
static unsigned int pointTowardsMousePointer(VirtualMachine *vm);
5356
static unsigned int pointTowardsRandomPosition(VirtualMachine *vm);
5457

58+
static unsigned int goToXY(VirtualMachine *vm);
59+
5560
static IRandomGenerator *rng;
5661
};
5762

test/blocks/motion_blocks_test.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ TEST_F(MotionBlocksTest, RegisterBlocks)
9999
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_turnleft", &MotionBlocks::compileTurnLeft));
100100
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_pointindirection", &MotionBlocks::compilePointInDirection));
101101
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_pointtowards", &MotionBlocks::compilePointTowards));
102+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "motion_gotoxy", &MotionBlocks::compileGoToXY));
102103

103104
// Inputs
104105
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "STEPS", MotionBlocks::STEPS));
105106
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "DEGREES", MotionBlocks::DEGREES));
106107
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "DIRECTION", MotionBlocks::DIRECTION));
107108
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "TOWARDS", MotionBlocks::TOWARDS));
109+
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "X", MotionBlocks::X));
110+
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "Y", MotionBlocks::Y));
108111

109112
m_section->registerBlocks(&m_engineMock);
110113
}
@@ -436,3 +439,42 @@ TEST_F(MotionBlocksTest, PointTowardsImpl)
436439
ASSERT_EQ(std::round(sprite.direction() * 100) / 100, intPosResults[i]);
437440
}
438441
}
442+
443+
TEST_F(MotionBlocksTest, GoToXY)
444+
{
445+
Compiler compiler(&m_engineMock);
446+
447+
// turn right (12.05) degrees
448+
auto block = std::make_shared<Block>("a", "motion_gotoxy");
449+
addValueInput(block, "X", MotionBlocks::X, 95.2);
450+
addValueInput(block, "Y", MotionBlocks::Y, -175.9);
451+
452+
EXPECT_CALL(m_engineMock, functionIndex(&MotionBlocks::goToXY)).WillOnce(Return(0));
453+
454+
compiler.init();
455+
compiler.setBlock(block);
456+
MotionBlocks::compileGoToXY(&compiler);
457+
compiler.end();
458+
459+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_CONST, 1, vm::OP_EXEC, 0, vm::OP_HALT }));
460+
ASSERT_EQ(compiler.constValues(), std::vector<Value>({ 95.2, -175.9 }));
461+
}
462+
463+
TEST_F(MotionBlocksTest, GoToXYImpl)
464+
{
465+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_CONST, 1, vm::OP_EXEC, 0, vm::OP_HALT };
466+
static BlockFunc functions[] = { &MotionBlocks::goToXY };
467+
static Value constValues[] = { 95.2, -175.9 };
468+
469+
Sprite sprite;
470+
471+
VirtualMachine vm(&sprite, nullptr, nullptr);
472+
vm.setBytecode(bytecode);
473+
vm.setFunctions(functions);
474+
vm.setConstValues(constValues);
475+
vm.run();
476+
477+
ASSERT_EQ(vm.registerCount(), 0);
478+
ASSERT_EQ(sprite.x(), 95.2);
479+
ASSERT_EQ(sprite.y(), -175.9);
480+
}

0 commit comments

Comments
 (0)