fn spawn_bonus(
    mut state: ResMut<State<GameState>>,
    mut commands: Commands,
    mut game: ResMut<Game>,
) {
    if *state.current() != GameState::Playing {
        return;
    }
    if let Some(entity) = game.bonus.entity {
        game.score -= 3;
        commands.entity(entity).despawn_recursive();
        game.bonus.entity = None;
        if game.score <= -5 {
            state.set(GameState::GameOver).unwrap();
            return;
        }
    }

    // ensure bonus doesn't spawn on the player
    loop {
        game.bonus.i = rand::thread_rng().gen_range(0..BOARD_SIZE_I);
        game.bonus.j = rand::thread_rng().gen_range(0..BOARD_SIZE_J);
        if game.bonus.i != game.player.i || game.bonus.j != game.player.j {
            break;
        }
    }
    game.bonus.entity = Some(
        commands
            .spawn_bundle((
                Transform {
                    translation: Vec3::new(
                        game.bonus.i as f32,
                        game.board[game.bonus.j][game.bonus.i].height + 0.2,
                        game.bonus.j as f32,
                    ),
                    ..Default::default()
                },
                GlobalTransform::identity(),
            ))
            .with_children(|cell| {
                cell.spawn_scene(game.bonus.handle.clone());
            })
            .id(),
    );
}
// despawn the bonus if there is one, then spawn a new one at a random location
despawn the bonus if there is one, then spawn a new one at a random location
despawn the bonus if there is one then spawn a new one at a random location
fn spawn_bonus(
    mut state: ResMut<State<GameState>>,
    mut commands: Commands,
    mut game: ResMut<Game>,
) {
    if *state.current() != GameState::Playing {
        return;
    }
    if let Some(entity) = game.bonus.entity {
        game.score -= 3;
        commands.entity(entity).despawn_recursive();
        game.bonus.entity = None;
        if game.score <= -5 {
            state.set(GameState::GameOver).unwrap();
            return;
        }
    }
    // ensure bonus doesn't spawn on the player
    loop {
        game.bonus.i = rand::thread_rng().gen_range(0..BOARD_SIZE_I);
        game.bonus.j = rand::thread_rng().gen_range(0..BOARD_SIZE_J);
        if game.bonus.i != game.player.i || game.bonus.j != game.player.j {
            break;
        }
    }
    game.bonus.entity = Some(
        commands
            .spawn_bundle((
                Transform {
                    translation: Vec3::new(
                        game.bonus.i as f32,
                        game.board[game.bonus.j][game.bonus.i].height + 0.2,
                        game.bonus.j as f32,
                    ),
                    ..Default::default()
                },
                GlobalTransform::identity(),
            ))
            .with_children(|cell| {
                cell.spawn_scene(game.bonus.handle.clone());
            })
            .id(),
    );
}
fn spawn_bonus ( mut state : ResMut < State < GameState > > , mut commands : Commands , mut game : ResMut < Game > , ) { if * state . current ( ) != GameState :: Playing { return ; } if let Some ( entity ) = game . bonus . entity { game . score -= 3 ; commands . entity ( entity ) . despawn_recursive ( ) ; game . bonus . entity = None ; if game . score <= - 5 { state . set ( GameState :: GameOver ) . unwrap ( ) ; return ; } } // ensure bonus doesn't spawn on the player loop { game . bonus . i = rand :: thread_rng ( ) . gen_range ( 0 .. BOARD_SIZE_I ) ; game . bonus . j = rand :: thread_rng ( ) . gen_range ( 0 .. BOARD_SIZE_J ) ; if game . bonus . i != game . player . i || game . bonus . j != game . player . j { break ; } } game . bonus . entity = Some ( commands . spawn_bundle ( ( Transform { translation : Vec3 :: new ( game . bonus . i as f32 , game . board [ game . bonus . j ] [ game . bonus . i ] . height + 0.2 , game . bonus . j as f32 , ) , .. Default :: default ( ) } , GlobalTransform :: identity ( ) , ) ) . with_children ( | cell | { cell . spawn_scene ( game . bonus . handle . clone ( ) ) ; } ) . id ( ) , ) ; }
despawn the bonus if there is one, then spawn a new one at a random location
despawn the bonus if there is one then spawn a new one at a random location
state
ResMut<State<GameState>>
commands
Commands
game
ResMut<Game>
state
ResMut<State<GameState>>
commands
Commands
game
ResMut<Game>