This chapter shows how to move a camera with a keyboard.
!!! note "Not ready for yet"
This section remains unfinished until feedback about the previous sections
has been received.
The idea is to change the camera's position, rotation and zoom upon a key press.
This chapter introduces:
- TODO
fn test_empty_app_has_no_cameras() {
let mut app = App::new();
assert_eq!(count_n_cameras(&mut app), 0);
}
fn test_create_app_has_a_moving_camera() {
let mut app = create_app();
app.update();
assert_eq!(count_n_cameras(&mut app), 1);
}
fn test_player_is_at_origin() {
let mut app = create_app();
app.update();
assert_eq!(get_player_position(&mut app), Vec2::new(0.0, 0.0));
}
fn test_player_has_a_custom_size() {
let mut app = create_app();
app.update();
assert_eq!(get_player_size(&mut app), Vec2::new(64.0, 32.0));
}
fn test_camera_is_at_origin() {
let mut app = create_app();
app.update();
assert_eq!(get_camera_position(&mut app), Vec2::new(0.0, 0.0));
}
fn test_camera_moves_when_pressed_up() {
let mut app = create_app();
app.update();
assert_eq!(get_camera_position(&mut app), Vec2::new(0.0, 0.0));
// Press the key
app.world_mut()
.resource_mut::<ButtonInput<KeyCode>>()
.press(KeyCode::ArrowUp);
app.update();
assert_ne!(get_camera_position(&mut app), Vec2::new(0.0, 0.0));
}
fn test_camera_is_not_rotated_at_start() {
let mut app = create_app();
app.update();
assert_eq!(get_camera_rotation(&mut app), 0.0);
}
fn test_camera_rotates_when_pressed_q() {
let mut app = create_app();
app.update();
assert_eq!(get_camera_rotation(&mut app), 0.0);
// Press the key
app.world_mut()
.resource_mut::<ButtonInput<KeyCode>>()
.press(KeyCode::KeyQ);
app.update();
assert_ne!(get_camera_rotation(&mut app), 0.0);
}
fn test_camera_is_not_zoomed_in_or_out_at_start() {
let mut app = create_app();
app.update();
assert_eq!(get_camera_zoom(&mut app), 1.0);
}
fn test_camera_zooms_in_when_pressed_w() {
let mut app = create_app();
app.update();
assert_eq!(get_camera_zoom(&mut app), 1.0);
// Press the key
app.world_mut()
.resource_mut::<ButtonInput<KeyCode>>()
.press(KeyCode::KeyW);
app.update();
assert!(get_camera_zoom(&mut app) < 1.0);
}
fn test_camera_zoom_out_when_pressed_s() {
let mut app = create_app();
app.update();
assert_eq!(get_camera_zoom(&mut app), 1.0);
// Press the key
app.world_mut()
.resource_mut::<ButtonInput<KeyCode>>()
.press(KeyCode::KeyS);
app.update();
assert!(get_camera_zoom(&mut app) > 1.0);
}
Running the application shows the camera movement in action.
We can now create an App
with a camera that responds to key presses.
We have tested everything that the App
does!
Full code can be found at https://github.com/richelbilderbeek/bevy_tdd_book_move_camera_with_keyboard.