Skip to content

Commit 94f4558

Browse files
committed
alien-cake-addict example: ensure the cake doesn't spawn on the player (#1713)
fixes #1707 In the game, it makes sense to never spawn the cake on the player.
1 parent 2dd2e5e commit 94f4558

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

examples/game/alien_cake_addict.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,15 @@ fn spawn_bonus(
307307
return;
308308
}
309309
}
310-
game.bonus.i = rand::thread_rng().gen_range(0..BOARD_SIZE_I);
311-
game.bonus.j = rand::thread_rng().gen_range(0..BOARD_SIZE_J);
310+
311+
// ensure bonus doesn't spawn on the player
312+
loop {
313+
game.bonus.i = rand::thread_rng().gen_range(0..BOARD_SIZE_I);
314+
game.bonus.j = rand::thread_rng().gen_range(0..BOARD_SIZE_J);
315+
if game.bonus.i != game.player.i || game.bonus.j != game.player.j {
316+
break;
317+
}
318+
}
312319
game.bonus.entity = commands
313320
.spawn((
314321
Transform {

0 commit comments

Comments
 (0)