Skip to content

Commit

Permalink
fixed camera panning
Browse files Browse the repository at this point in the history
  • Loading branch information
Byteron committed Jun 28, 2022
1 parent 046278a commit 568eff2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use bevy::{
core::Time,
input::Input,
math::Vec2,
prelude::{
Camera, EventWriter, KeyCode, OrthographicProjection, Query, Res, ResMut, Transform, With,
Without,
Camera, Component, EventWriter, KeyCode, OrthographicProjection, Query, Res, ResMut,
Transform, With, Without,
},
render::camera::CameraProjection,
window::Windows,
Expand All @@ -12,31 +13,32 @@ use bevy_parallax::ParallaxMoveEvent;

use crate::{consts, Player};

#[derive(Component)]
pub struct Panning {
pub offset: Vec2,
}

pub fn helper_camera_controller(
mut query: Query<(&mut Camera, &mut OrthographicProjection, &mut Transform)>,
mut query: Query<(&mut Camera, &mut OrthographicProjection, &mut Panning)>,
keys: Res<Input<KeyCode>>,
time: Res<Time>,
mut windows: ResMut<Windows>,
) {
let (mut camera, mut projection, mut transform) = query.single_mut();
let (mut camera, mut projection, mut panning) = query.single_mut();

if keys.pressed(KeyCode::Up) {
transform.translation.y += 150.0 * time.delta_seconds();
panning.offset.y += 150.0 * time.delta_seconds();
}
if keys.pressed(KeyCode::Left) {
transform.translation.x -= 150.0 * time.delta_seconds();
panning.offset.x -= 150.0 * time.delta_seconds();
}
if keys.pressed(KeyCode::Down) {
transform.translation.y -= 150.0 * time.delta_seconds();
panning.offset.y -= 150.0 * time.delta_seconds();
}
if keys.pressed(KeyCode::Right) {
transform.translation.x += 150.0 * time.delta_seconds();
panning.offset.x += 150.0 * time.delta_seconds();
}

let scale = projection.scale;

let w = windows.primary_mut();

if keys.pressed(KeyCode::Z) {
projection.scale = f32::clamp(
projection.scale - 150. * time.delta_seconds(),
Expand All @@ -48,23 +50,30 @@ pub fn helper_camera_controller(
projection.scale += 150. * time.delta_seconds();
}

let scale = projection.scale;
let window = windows.primary_mut();

if (projection.scale - scale).abs() > f32::EPSILON {
projection.update(w.width(), w.height());
projection.update(window.width(), window.height());
camera.projection_matrix = projection.get_projection_matrix();
camera.depth_calculation = projection.depth_calculation();
}
}

pub fn camera_follow_player(
player_query: Query<&Transform, With<Player>>,
mut camera_query: Query<&mut Transform, (With<Camera>, Without<Player>)>,
mut camera_query: Query<(&mut Transform, &Panning), (With<Camera>, Without<Player>)>,
mut move_event_writer: EventWriter<ParallaxMoveEvent>,
) {
let player = player_query.single().translation;
let camera = camera_query.single_mut();
let (mut camera, panning) = camera_query.single_mut();

let diff = player.x - (camera.translation.x - panning.offset.x);

camera.translation.x = player.x + panning.offset.x;
camera.translation.y = player.y + panning.offset.y;

move_event_writer.send(ParallaxMoveEvent {
camera_move_speed: (player.x - camera.translation.x) * consts::CAMERA_SPEED,
camera_move_speed: diff * consts::CAMERA_SPEED,
});
// camera.translation.y += (player.y - camera.translation.y) * time.delta_seconds() * 5.;
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ fn setup(
camera_bundle.orthographic_projection.scale = 16. * 14.;
commands
.spawn_bundle(camera_bundle)
.insert(Panning { offset: Vec2::ZERO })
.insert(ParallaxCameraComponent);

let texture_handle = asset_server.load("PlayerFishy(96x80).png");
Expand Down

0 comments on commit 568eff2

Please sign in to comment.