Skip to content

Commit 6d2ff6d

Browse files
committed
Read stage size from engine in motion blocks
1 parent 3c20340 commit 6d2ff6d

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

src/blocks/motionblocks.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,8 @@ unsigned int MotionBlocks::pointTowards(VirtualMachine *vm)
304304
if (value == "_mouse_")
305305
pointTowardsPos(dynamic_cast<Sprite *>(vm->target()), vm->engine()->mouseX(), vm->engine()->mouseY());
306306
else if (value == "_random_") {
307-
// TODO: Read stage size from engine (#224)
308-
static const unsigned int stageWidth = 480;
309-
static const unsigned int stageHeight = 360;
307+
const unsigned int stageWidth = vm->engine()->stageWidth();
308+
const unsigned int stageHeight = vm->engine()->stageHeight();
310309

311310
if (!rng)
312311
rng = RandomGenerator::instance().get();
@@ -342,9 +341,8 @@ unsigned int MotionBlocks::pointTowardsMousePointer(VirtualMachine *vm)
342341

343342
unsigned int MotionBlocks::pointTowardsRandomPosition(VirtualMachine *vm)
344343
{
345-
// TODO: Read stage size from engine (#224)
346-
static const unsigned int stageWidth = 480;
347-
static const unsigned int stageHeight = 360;
344+
const unsigned int stageWidth = vm->engine()->stageWidth();
345+
const unsigned int stageHeight = vm->engine()->stageHeight();
348346

349347
if (!rng)
350348
rng = RandomGenerator::instance().get();
@@ -379,9 +377,8 @@ unsigned int MotionBlocks::goTo(VirtualMachine *vm)
379377
sprite->setX(vm->engine()->mouseX());
380378
sprite->setY(vm->engine()->mouseY());
381379
} else if (value == "_random_") {
382-
// TODO: Read stage size from engine (#224)
383-
static const unsigned int stageWidth = 480;
384-
static const unsigned int stageHeight = 360;
380+
const unsigned int stageWidth = vm->engine()->stageWidth();
381+
const unsigned int stageHeight = vm->engine()->stageHeight();
385382

386383
if (!rng)
387384
rng = RandomGenerator::instance().get();
@@ -432,9 +429,8 @@ unsigned int MotionBlocks::goToRandomPosition(VirtualMachine *vm)
432429
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
433430

434431
if (sprite) {
435-
// TODO: Read stage size from engine (#224)
436-
static const unsigned int stageWidth = 480;
437-
static const unsigned int stageHeight = 360;
432+
const unsigned int stageWidth = vm->engine()->stageWidth();
433+
const unsigned int stageHeight = vm->engine()->stageHeight();
438434

439435
if (!rng)
440436
rng = RandomGenerator::instance().get();
@@ -536,9 +532,8 @@ unsigned int MotionBlocks::startGlideTo(VirtualMachine *vm)
536532
if (value == "_mouse_")
537533
startGlidingToPos(vm, vm->engine()->mouseX(), vm->engine()->mouseY(), vm->getInput(0, 2)->toDouble());
538534
else if (value == "_random_") {
539-
// TODO: Read stage size from engine (#224)
540-
static const unsigned int stageWidth = 480;
541-
static const unsigned int stageHeight = 360;
535+
const unsigned int stageWidth = vm->engine()->stageWidth();
536+
const unsigned int stageHeight = vm->engine()->stageHeight();
542537

543538
if (!rng)
544539
rng = RandomGenerator::instance().get();
@@ -582,9 +577,8 @@ unsigned int MotionBlocks::startGlideToRandomPosition(VirtualMachine *vm)
582577
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
583578

584579
if (sprite) {
585-
// TODO: Read stage size from engine (#224)
586-
static const unsigned int stageWidth = 480;
587-
static const unsigned int stageHeight = 360;
580+
const unsigned int stageWidth = vm->engine()->stageWidth();
581+
const unsigned int stageHeight = vm->engine()->stageHeight();
588582

589583
if (!rng)
590584
rng = RandomGenerator::instance().get();

test/blocks/motion_blocks_test.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,10 @@ TEST_F(MotionBlocksTest, PointTowardsImpl)
386386
sprite.setY(std::round(sprite.y()));
387387

388388
for (int i = 0; i < positions.size(); i++) {
389-
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(std::round(positions[i].first)));
390-
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(std::round(positions[i].second)));
389+
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
390+
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
391+
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(std::round(positions[i].first)));
392+
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(std::round(positions[i].second)));
391393

392394
// TODO: Move setBytecode() out of the loop and use reset() after task #215 is completed
393395
vm.setBytecode(bytecode2);
@@ -449,8 +451,10 @@ TEST_F(MotionBlocksTest, PointTowardsImpl)
449451
sprite.setY(std::round(sprite.y()));
450452

451453
for (int i = 0; i < positions.size(); i++) {
452-
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(std::round(positions[i].first)));
453-
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(std::round(positions[i].second)));
454+
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
455+
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
456+
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(std::round(positions[i].first)));
457+
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(std::round(positions[i].second)));
454458

455459
// TODO: Move setBytecode() out of the loop and use reset() after task #215 is completed
456460
vm.setBytecode(bytecode6);
@@ -590,8 +594,10 @@ TEST_F(MotionBlocksTest, GoToImpl)
590594
ASSERT_EQ(sprite.y(), 45.2);
591595

592596
// go to (join "_random_" "")
593-
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(-158));
594-
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(65));
597+
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
598+
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
599+
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(-158));
600+
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(65));
595601

596602
vm.setBytecode(bytecode2);
597603
vm.run();
@@ -633,8 +639,10 @@ TEST_F(MotionBlocksTest, GoToImpl)
633639
ASSERT_EQ(sprite.y(), -170.6);
634640

635641
// go to (random position)
636-
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(220));
637-
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(-16));
642+
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
643+
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
644+
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(220));
645+
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(-16));
638646

639647
vm.setBytecode(bytecode6);
640648
vm.run();
@@ -869,8 +877,10 @@ TEST_F(MotionBlocksTest, GlideToImpl)
869877
EXPECT_CALL(m_engineMock, mouseY()).WillOnce(Return(endY));
870878
break;
871879
case 3:
872-
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(std::round(endX)));
873-
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(std::round(endY)));
880+
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
881+
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
882+
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(std::round(endX)));
883+
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(std::round(endY)));
874884
default:
875885
break;
876886
}

0 commit comments

Comments
 (0)