fn focus_camera(
    time: Res<Time>,
    mut game: ResMut<Game>,
    mut transforms: QuerySet<(Query<(&mut Transform, &Camera)>, Query<&Transform>)>,
) {
    const SPEED: f32 = 2.0;
    // if there is both a player and a bonus, target the mid-point of them
    if let (Some(player_entity), Some(bonus_entity)) = (game.player.entity, game.bonus.entity) {
        if let (Ok(player_transform), Ok(bonus_transform)) = (
            transforms.q1().get(player_entity),
            transforms.q1().get(bonus_entity),
        ) {
            game.camera_should_focus = player_transform
                .translation
                .lerp(bonus_transform.translation, 0.5);
        }
    // otherwise, if there is only a player, target the player
    } else if let Some(player_entity) = game.player.entity {
        if let Ok(player_transform) = transforms.q1().get(player_entity) {
            game.camera_should_focus = player_transform.translation;
        }
    // otherwise, target the middle
    } else {
        game.camera_should_focus = Vec3::from(RESET_FOCUS);
    }
    // calculate the camera motion based on the difference between where the camera is looking
    // and where it should be looking; the greater the distance, the faster the motion;
    // smooth out the camera movement using the frame time
    let mut camera_motion = game.camera_should_focus - game.camera_is_focus;
    if camera_motion.length() > 0.2 {
        camera_motion *= SPEED * time.delta_seconds();
        // set the new camera's actual focus
        game.camera_is_focus += camera_motion;
    }
    // look at that new camera's actual focus
    for (mut transform, camera) in transforms.q0_mut().iter_mut() {
        if camera.name == Some(CAMERA_3D.to_string()) {
            *transform = transform.looking_at(game.camera_is_focus, Vec3::Y);
        }
    }
}
// change the focus of the camera
change the focus of the camera
change the focus of the camera
fn focus_camera(
    time: Res<Time>,
    mut game: ResMut<Game>,
    mut transforms: QuerySet<(Query<(&mut Transform, &Camera)>, Query<&Transform>)>,
) {
    const SPEED: f32 = 2.0;
    // if there is both a player and a bonus, target the mid-point of them
    if let (Some(player_entity), Some(bonus_entity)) = (game.player.entity, game.bonus.entity) {
        if let (Ok(player_transform), Ok(bonus_transform)) = (
            transforms.q1().get(player_entity),
            transforms.q1().get(bonus_entity),
        ) {
            game.camera_should_focus = player_transform
                .translation
                .lerp(bonus_transform.translation, 0.5);
        }
    // otherwise, if there is only a player, target the player
    } else if let Some(player_entity) = game.player.entity {
        if let Ok(player_transform) = transforms.q1().get(player_entity) {
            game.camera_should_focus = player_transform.translation;
        }
    // otherwise, target the middle
    } else {
        game.camera_should_focus = Vec3::from(RESET_FOCUS);
    }
    // calculate the camera motion based on the difference between where the camera is looking
    // and where it should be looking; the greater the distance, the faster the motion;
    // smooth out the camera movement using the frame time
    let mut camera_motion = game.camera_should_focus - game.camera_is_focus;
    if camera_motion.length() > 0.2 {
        camera_motion *= SPEED * time.delta_seconds();
        // set the new camera's actual focus
        game.camera_is_focus += camera_motion;
    }
    // look at that new camera's actual focus
    for (mut transform, camera) in transforms.q0_mut().iter_mut() {
        if camera.name == Some(CAMERA_3D.to_string()) {
            *transform = transform.looking_at(game.camera_is_focus, Vec3::Y);
        }
    }
}
fn focus_camera ( time : Res < Time > , mut game : ResMut < Game > , mut transforms : QuerySet < ( Query < ( & mut Transform , & Camera ) > , Query < & Transform > ) > , ) { const SPEED : f32 = 2.0 ; // if there is both a player and a bonus, target the mid-point of them if let ( Some ( player_entity ) , Some ( bonus_entity ) ) = ( game . player . entity , game . bonus . entity ) { if let ( Ok ( player_transform ) , Ok ( bonus_transform ) ) = ( transforms . q1 ( ) . get ( player_entity ) , transforms . q1 ( ) . get ( bonus_entity ) , ) { game . camera_should_focus = player_transform . translation . lerp ( bonus_transform . translation , 0.5 ) ; } // otherwise, if there is only a player, target the player } else if let Some ( player_entity ) = game . player . entity { if let Ok ( player_transform ) = transforms . q1 ( ) . get ( player_entity ) { game . camera_should_focus = player_transform . translation ; } // otherwise, target the middle } else { game . camera_should_focus = Vec3 :: from ( RESET_FOCUS ) ; } // calculate the camera motion based on the difference between where the camera is looking // and where it should be looking; the greater the distance, the faster the motion; // smooth out the camera movement using the frame time let mut camera_motion = game . camera_should_focus - game . camera_is_focus ; if camera_motion . length ( ) > 0.2 { camera_motion *= SPEED * time . delta_seconds ( ) ; // set the new camera's actual focus game . camera_is_focus += camera_motion ; } // look at that new camera's actual focus for ( mut transform , camera ) in transforms . q0_mut ( ) . iter_mut ( ) { if camera . name == Some ( CAMERA_3D . to_string ( ) ) { * transform = transform . looking_at ( game . camera_is_focus , Vec3 :: Y ) ; } } }
change the focus of the camera
change the focus of the camera
time
Res<Time>
game
ResMut<Game>
transforms
QuerySet<(Query<(&mut Transform, &Camera)>, Query<&Transform>)>
time
Res<Time>
game
ResMut<Game>
transforms
QuerySet<(Query<(&mut Transform, &Camera)>, Query<&Transform>)>