45 lines
1.2 KiB
Rust
45 lines
1.2 KiB
Rust
use bevy::prelude::*;
|
|
use bevy::input::keyboard::KeyCode;
|
|
|
|
#[derive(Component)]
|
|
pub struct PanningCamera {
|
|
pub pan_speed: f32,
|
|
}
|
|
|
|
pub fn camera_movement(
|
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
|
mut query: Query<(&PanningCamera, &mut Transform), With<Camera>>,
|
|
) {
|
|
for (camera, mut transform) in query.iter_mut() {
|
|
let mut direction = Vec2::ZERO;
|
|
|
|
if keyboard_input.pressed(KeyCode::ArrowUp) {
|
|
direction.y += 2.0;
|
|
}
|
|
if keyboard_input.pressed(KeyCode::ArrowDown) {
|
|
direction.y -= 2.0;
|
|
}
|
|
if keyboard_input.pressed(KeyCode::ArrowLeft) {
|
|
direction.x -= 2.0;
|
|
}
|
|
if keyboard_input.pressed(KeyCode::ArrowRight) {
|
|
direction.x += 2.0;
|
|
}
|
|
|
|
if direction.length() > 0.0 {
|
|
direction = direction.normalize();
|
|
|
|
let movement = direction * camera.pan_speed;
|
|
transform.translation.x += movement.x;
|
|
transform.translation.y += movement.y;
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn spawn_panning_camera(mut commands: Commands) {
|
|
commands.spawn((
|
|
Camera2d::default(),
|
|
Transform::from_xyz(0.,0.,10.),
|
|
PanningCamera { pan_speed: 5.0 }
|
|
));
|
|
} |