Skip to content

Commit

Permalink
Works
Browse files Browse the repository at this point in the history
  • Loading branch information
richelbilderbeek committed Jul 10, 2024
1 parent 9b0f3ca commit d087b54
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ fn get_camera_rotation(app: &mut App) -> f32 {
transform.rotation.z
}

#[cfg(test)]
fn get_camera_zoom(app: &mut App) -> f32 {
let mut query = app.world_mut().query::<(&OrthographicProjection, &Camera)>();
let (projection, _) = query.single(app.world());
projection.scale
}


#[cfg(test)]
fn get_player_position(app: &mut App) -> Vec2 {
let mut query = app.world_mut().query::<(&Transform, &Player)>();
Expand All @@ -73,10 +81,10 @@ fn count_n_cameras(app: &mut App) -> usize {
}

fn respond_to_keyboard(
mut query: Query<(&mut Transform, &Camera)>,
mut query: Query<(&mut Transform, &mut OrthographicProjection, &Camera)>,
input: Res<ButtonInput<KeyCode>>,
) {
let (mut transform, _) = query.single_mut();
let (mut transform, mut projection, _) = query.single_mut();
use bevy::input::keyboard::KeyCode;
if input.pressed(KeyCode::ArrowRight) {
transform.translation.x += 1.0;
Expand All @@ -96,6 +104,12 @@ fn respond_to_keyboard(
if input.pressed(KeyCode::KeyE) {
transform.rotate_z(0.1);
}
if input.pressed(KeyCode::KeyW) {
projection.scale /= 1.1
}
if input.pressed(KeyCode::KeyS) {
projection.scale *= 1.1
}
}


Expand Down Expand Up @@ -149,6 +163,13 @@ mod tests {
assert_eq!(get_camera_rotation(&mut app), 0.0);
}

#[test]
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);
}

#[test]
fn test_camera_moves_when_pressed_up() {
let mut app = create_app();
Expand Down Expand Up @@ -235,4 +256,33 @@ mod tests {
assert_ne!(get_camera_rotation(&mut app), 0.0);
}

#[test]
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);
}
#[test]
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);
}

}

0 comments on commit d087b54

Please sign in to comment.