Skip to content

Commit

Permalink
Read stage size from engine in motion blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
adazem009 committed Sep 30, 2023
1 parent 3c20340 commit 6d2ff6d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
30 changes: 12 additions & 18 deletions src/blocks/motionblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,8 @@ unsigned int MotionBlocks::pointTowards(VirtualMachine *vm)
if (value == "_mouse_")
pointTowardsPos(dynamic_cast<Sprite *>(vm->target()), vm->engine()->mouseX(), vm->engine()->mouseY());
else if (value == "_random_") {
// TODO: Read stage size from engine (#224)
static const unsigned int stageWidth = 480;
static const unsigned int stageHeight = 360;
const unsigned int stageWidth = vm->engine()->stageWidth();
const unsigned int stageHeight = vm->engine()->stageHeight();

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

unsigned int MotionBlocks::pointTowardsRandomPosition(VirtualMachine *vm)
{
// TODO: Read stage size from engine (#224)
static const unsigned int stageWidth = 480;
static const unsigned int stageHeight = 360;
const unsigned int stageWidth = vm->engine()->stageWidth();
const unsigned int stageHeight = vm->engine()->stageHeight();

if (!rng)
rng = RandomGenerator::instance().get();
Expand Down Expand Up @@ -379,9 +377,8 @@ unsigned int MotionBlocks::goTo(VirtualMachine *vm)
sprite->setX(vm->engine()->mouseX());
sprite->setY(vm->engine()->mouseY());
} else if (value == "_random_") {
// TODO: Read stage size from engine (#224)
static const unsigned int stageWidth = 480;
static const unsigned int stageHeight = 360;
const unsigned int stageWidth = vm->engine()->stageWidth();
const unsigned int stageHeight = vm->engine()->stageHeight();

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

if (sprite) {
// TODO: Read stage size from engine (#224)
static const unsigned int stageWidth = 480;
static const unsigned int stageHeight = 360;
const unsigned int stageWidth = vm->engine()->stageWidth();
const unsigned int stageHeight = vm->engine()->stageHeight();

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

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

if (sprite) {
// TODO: Read stage size from engine (#224)
static const unsigned int stageWidth = 480;
static const unsigned int stageHeight = 360;
const unsigned int stageWidth = vm->engine()->stageWidth();
const unsigned int stageHeight = vm->engine()->stageHeight();

if (!rng)
rng = RandomGenerator::instance().get();
Expand Down
30 changes: 20 additions & 10 deletions test/blocks/motion_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,10 @@ TEST_F(MotionBlocksTest, PointTowardsImpl)
sprite.setY(std::round(sprite.y()));

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

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

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

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

// go to (join "_random_" "")
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(-158));
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(65));
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(-158));
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(65));

vm.setBytecode(bytecode2);
vm.run();
Expand Down Expand Up @@ -633,8 +639,10 @@ TEST_F(MotionBlocksTest, GoToImpl)
ASSERT_EQ(sprite.y(), -170.6);

// go to (random position)
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(220));
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(-16));
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(220));
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(-16));

vm.setBytecode(bytecode6);
vm.run();
Expand Down Expand Up @@ -869,8 +877,10 @@ TEST_F(MotionBlocksTest, GlideToImpl)
EXPECT_CALL(m_engineMock, mouseY()).WillOnce(Return(endY));
break;
case 3:
EXPECT_CALL(rng, randint(-240, 240)).WillOnce(Return(std::round(endX)));
EXPECT_CALL(rng, randint(-180, 180)).WillOnce(Return(std::round(endY)));
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
EXPECT_CALL(rng, randint(-320, 320)).WillOnce(Return(std::round(endX)));
EXPECT_CALL(rng, randint(-250, 250)).WillOnce(Return(std::round(endY)));
default:
break;
}
Expand Down

0 comments on commit 6d2ff6d

Please sign in to comment.