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 0219643 commit 7fcdf3e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
54 changes: 53 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ fn get_camera_position(app: &mut App) -> Vec2 {
transform.translation.xy()
}

#[cfg(test)]
fn get_camera_rotation(app: &mut App) -> f32 {
let mut query = app.world_mut().query::<(&Transform, &Camera)>();
let (transform, _) = query.single(app.world());
transform.rotation.z
}

#[cfg(test)]
fn get_player_position(app: &mut App) -> Vec2 {
let mut query = app.world_mut().query::<(&Transform, &Player)>();
Expand Down Expand Up @@ -83,6 +90,12 @@ fn respond_to_keyboard(
if input.pressed(KeyCode::ArrowDown) {
transform.translation.y -= 1.0;
}
if input.pressed(KeyCode::KeyQ) {
transform.rotate_z(-0.1);
}
if input.pressed(KeyCode::KeyE) {
transform.rotate_z(0.1);
}
}


Expand Down Expand Up @@ -117,7 +130,6 @@ mod tests {

#[test]
fn test_player_has_a_custom_scale() {
let velocity = Vec2::new(0.0, 0.0);
let mut app = create_app();
app.update();
assert_eq!(get_player_scale(&mut app), Vec2::new(64.0, 32.0));
Expand All @@ -130,6 +142,13 @@ mod tests {
assert_eq!(get_camera_position(&mut app), Vec2::new(0.0, 0.0));
}

#[test]
fn test_camera_is_not_rotated_at_start() {
let mut app = create_app();
app.update();
assert_eq!(get_camera_rotation(&mut app), 0.0);
}

#[test]
fn test_camera_moves_when_pressed_up() {
let mut app = create_app();
Expand Down Expand Up @@ -185,4 +204,37 @@ mod tests {
app.update();assert_ne!(get_camera_position(&mut app), Vec2::new(0.0, 0.0));

}

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

println!("{:?}", get_camera_rotation(&mut app));

assert_ne!(get_camera_rotation(&mut app), 0.0);
}

#[test]
fn test_camera_rotates_when_pressed_e() {
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::KeyE);
app.update();

assert_ne!(get_camera_rotation(&mut app), 0.0);
}

}
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use bevy::prelude::*;
mod app;

fn main() {
let velocity = Vec2::new(0.2, 0.1);
let mut app = create_app(velocity);
let mut app = create_app();
app.add_plugins(DefaultPlugins);
app.run();
}

0 comments on commit 7fcdf3e

Please sign in to comment.