Move a camera with a keyboard¶
This chapter shows how to move a camera with a keyboard.
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
First test: an empty App
has no cameras¶
fn test_empty_app_has_no_cameras() {
let mut app = App::new();
assert_eq!(count_n_cameras(&mut app), 0);
}
Second test: our App
has one camera¶
fn test_create_app_has_a_moving_camera() {
let mut app = create_app();
app.update();
assert_eq!(count_n_cameras(&mut app), 1);
}
Third test: our player is at the origin¶
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));
}
Fourth test: our player has a proper size¶
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));
}
Fifth test: our camera starts at the origin¶
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));
}
Sixth test: our camera moves when pressing the arrow up key¶
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));
}
Seventh test: our camera is not rotated at the start¶
fn test_camera_is_not_rotated_at_start() {
let mut app = create_app();
app.update();
assert_eq!(get_camera_rotation(&mut app), 0.0);
}
Eighth test: our camera rotates when pressing the Q key¶
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);
}
Ninth test: our camera is not zoomed in or out at the start¶
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);
}
Tenth test: our camera zooms in when pressing the W key¶
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);
}
Eleventh test: our camera zooms out when pressing the S key¶
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);
}
main.rs
¶
Running the application shows the camera movement in action.
Conclusion¶
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.