Skip to content

Commit

Permalink
Respond to keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
richelbilderbeek committed Jul 10, 2024
1 parent f597b09 commit 7d224af
Showing 1 changed file with 91 additions and 35 deletions.
126 changes: 91 additions & 35 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,26 @@ use bevy::prelude::*;
#[derive(Component)]
pub struct Player;

#[derive(Component)]
pub struct MovingCamera {
velocity: Vec2,
}

pub fn create_app(velocity: Vec2) -> App {
pub fn create_app() -> App {
let mut app = App::new();

// Only add this plugin in testing.
// The main app will assume it to be absent
if cfg!(test) {
app.add_plugins(bevy::input::InputPlugin);
}
let add_camera_fun = move |commands: Commands| add_moving_camera(commands, velocity);
app.add_systems(Startup, (add_camera_fun, add_player));
app.add_systems(Update, move_camera);
app.add_systems(Startup, (add_camera, add_player));
app.add_systems(Update, respond_to_keyboard);

// Do not do update, as this will disallow to do more steps
// app.update(); //Don't!
app
}

fn add_moving_camera(mut commands: Commands, velocity: Vec2) {
commands.spawn((
Camera2dBundle::default(),
MovingCamera { velocity: velocity },
));
fn add_camera(mut commands: Commands) {
commands.spawn(
Camera2dBundle::default()
);
}

fn add_player(mut commands: Commands) {
Expand All @@ -47,7 +40,7 @@ fn add_player(mut commands: Commands) {

#[cfg(test)]
fn get_camera_position(app: &mut App) -> Vec2 {
let mut query = app.world_mut().query::<(&Transform, &MovingCamera)>();
let mut query = app.world_mut().query::<(&Transform, &Camera)>();
let (transform, _) = query.single(app.world());
transform.translation.xy()
}
Expand All @@ -67,70 +60,133 @@ fn get_player_scale(app: &mut App) -> Vec2 {
}

#[cfg(test)]
fn count_n_moving_cameras(app: &mut App) -> usize {
let mut query = app.world_mut().query::<&MovingCamera>();
fn count_n_cameras(app: &mut App) -> usize {
let mut query = app.world_mut().query::<&Camera>();
query.iter(app.world()).len()
}

fn move_camera(mut query: Query<(&mut Transform, &MovingCamera)>) {
let (mut transform, moving_camera) = query.single_mut();
transform.translation.x += moving_camera.velocity.x;
transform.translation.y += moving_camera.velocity.y;
fn respond_to_keyboard(
mut query: Query<(&mut Transform, &Camera)>,
maybe_input: Option<Res<ButtonInput<KeyCode>>>,
) {
if maybe_input.is_none() {
return;
}
let input = maybe_input.unwrap();
let (mut transform, _) = query.single_mut();
use bevy::input::keyboard::KeyCode;
if input.pressed(KeyCode::ArrowRight) {
transform.translation.x += 1.0;
}
if input.pressed(KeyCode::ArrowLeft) {
transform.translation.x -= 1.0;
}
if input.pressed(KeyCode::ArrowUp) {
transform.translation.y += 1.0;
}
if input.pressed(KeyCode::ArrowDown) {
transform.translation.y -= 1.0;
}
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_can_create_app() {
let velocity = Vec2::new(0.2, 0.1);
create_app(velocity);
create_app();
}

#[test]
fn test_empty_app_has_no_moving_cameras() {
let mut app = App::new();
assert_eq!(count_n_moving_cameras(&mut app), 0);
assert_eq!(count_n_cameras(&mut app), 0);
}

#[test]
fn test_create_app_has_a_moving_camera() {
let velocity = Vec2::new(0.0, 0.0);
let mut app = create_app(velocity);
let mut app = create_app();
app.update();
assert_eq!(count_n_moving_cameras(&mut app), 1);
assert_eq!(count_n_cameras(&mut app), 1);
}

#[test]
fn test_player_is_at_origin() {
let velocity = Vec2::new(0.0, 0.0);
let mut app = create_app(velocity);
let mut app = create_app();
app.update();
assert_eq!(get_player_position(&mut app), Vec2::new(0.0, 0.0));
}

#[test]
fn test_player_has_a_custom_scale() {
let velocity = Vec2::new(0.0, 0.0);
let mut app = create_app(velocity);
let mut app = create_app();
app.update();
assert_eq!(get_player_scale(&mut app), Vec2::new(64.0, 32.0));
}

#[test]
fn test_camera_is_at_origin() {
let velocity = Vec2::new(0.0, 0.0);
let mut app = create_app(velocity);
let mut app = create_app();
app.update();
assert_eq!(get_camera_position(&mut app), Vec2::new(0.0, 0.0));
}

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

}
#[test]
fn test_camera_moves_when_pressed_right() {
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::ArrowRight);
app.update();
assert_ne!(get_camera_position(&mut app), Vec2::new(0.0, 0.0));

}
#[test]
fn test_moving_camera_moves() {
let velocity = Vec2::new(1.2, 3.4);
let mut app = create_app(velocity);
fn test_camera_moves_when_pressed_down() {
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::ArrowDown);
app.update();
assert_ne!(get_camera_position(&mut app), Vec2::new(0.0, 0.0));

}
#[test]
fn test_camera_moves_when_pressed_left() {
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::ArrowLeft);
app.update();assert_ne!(get_camera_position(&mut app), Vec2::new(0.0, 0.0));

}
}

0 comments on commit 7d224af

Please sign in to comment.