Skip to content

Commit c9fd5ef

Browse files
committed
Set random direction in point towards random block
1 parent 57855e8 commit c9fd5ef

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

src/blocks/motionblocks.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ CompilerValue *MotionBlocks::compilePointTowards(Compiler *compiler)
112112
if (value == "_mouse_")
113113
compiler->addTargetFunctionCall("motion_point_towards_mouse");
114114
else if (value == "_random_")
115-
compiler->addFunctionCallWithCtx("motion_point_towards_random_pos");
115+
compiler->addFunctionCallWithCtx("motion_point_towards_random_direction");
116116
else {
117117
int index = compiler->engine()->findTarget(value);
118118
Target *anotherTarget = compiler->engine()->targetAt(index);
@@ -398,14 +398,11 @@ extern "C" void motion_point_towards_mouse(Sprite *sprite)
398398
motion_point_towards_pos(sprite, engine->mouseX(), engine->mouseY());
399399
}
400400

401-
extern "C" void motion_point_towards_random_pos(ExecutionContext *ctx)
401+
extern "C" void motion_point_towards_random_direction(ExecutionContext *ctx)
402402
{
403403
Sprite *sprite = static_cast<Sprite *>(ctx->thread()->target());
404-
IEngine *engine = ctx->engine();
405-
const int stageWidth = engine->stageWidth();
406-
const int stageHeight = engine->stageHeight();
407404
IRandomGenerator *rng = ctx->rng();
408-
motion_point_towards_pos(sprite, rng->randintDouble(-stageWidth / 2.0, stageWidth / 2.0), rng->randintDouble(-stageHeight / 2.0, stageHeight / 2.0));
405+
sprite->setDirection(rng->randint(-180, 179));
409406
}
410407

411408
extern "C" void motion_point_towards_target_by_index(Sprite *sprite, double index)
@@ -424,7 +421,7 @@ extern "C" void motion_pointtowards(ExecutionContext *ctx, const StringPtr *towa
424421
if (string_compare_case_sensitive(towards, &MOUSE_STR) == 0)
425422
motion_point_towards_mouse(sprite);
426423
else if (string_compare_case_sensitive(towards, &RANDOM_STR) == 0)
427-
motion_point_towards_random_pos(ctx);
424+
motion_point_towards_random_direction(ctx);
428425
else {
429426
// TODO: Use UTF-16 in engine
430427
std::string u8name = utf8::utf16to8(std::u16string(towards->data));

test/blocks/motion_blocks_test.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ TEST_F(MotionBlocksTest, PointTowardsMouse)
224224
}
225225
}
226226

227-
TEST_F(MotionBlocksTest, PointTowardsRandomPosition)
227+
TEST_F(MotionBlocksTest, PointTowardsRandomDirection)
228228
{
229229
{
230230
auto sprite = std::make_shared<Sprite>();
@@ -247,12 +247,9 @@ TEST_F(MotionBlocksTest, PointTowardsRandomPosition)
247247
RandomGeneratorMock rng;
248248
ctx->setRng(&rng);
249249

250-
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
251-
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
252-
EXPECT_CALL(rng, randintDouble(-320, 320)).WillOnce(Return(95.2));
253-
EXPECT_CALL(rng, randintDouble(-250, 250)).WillOnce(Return(-100.025));
250+
EXPECT_CALL(rng, randint(-180, 179)).WillOnce(Return(95));
254251
code->run(ctx.get());
255-
ASSERT_EQ(std::round(sprite->direction() * 100) / 100, 90);
252+
ASSERT_EQ(sprite->direction(), 95);
256253
}
257254

258255
{
@@ -276,12 +273,9 @@ TEST_F(MotionBlocksTest, PointTowardsRandomPosition)
276273
RandomGeneratorMock rng;
277274
ctx->setRng(&rng);
278275

279-
EXPECT_CALL(m_engineMock, stageWidth()).WillOnce(Return(640));
280-
EXPECT_CALL(m_engineMock, stageHeight()).WillOnce(Return(500));
281-
EXPECT_CALL(rng, randintDouble(-320, 320)).WillOnce(Return(-21.28));
282-
EXPECT_CALL(rng, randintDouble(-250, 250)).WillOnce(Return(-100.025));
276+
EXPECT_CALL(rng, randint(-180, 179)).WillOnce(Return(-42));
283277
code->run(ctx.get());
284-
ASSERT_EQ(std::round(sprite->direction() * 100) / 100, -90);
278+
ASSERT_EQ(sprite->direction(), -42);
285279
}
286280

287281
{

0 commit comments

Comments
 (0)