From 3ea4fda9c5e5de5779b937bbdaae1ef1393bc143 Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Tue, 24 Dec 2024 09:45:10 +0100 Subject: [PATCH] cam move with mouse --- src/camera.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index f9521d5..8b692ca 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -4,7 +4,8 @@ pub struct Plugin; impl bevy::prelude::Plugin for Plugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup) - .add_systems(Update, move_cam); + .add_systems(Update, move_cam) + .init_resource::(); } } @@ -16,20 +17,24 @@ fn setup(mut cmds: Commands, window: Query<&Window>) { )); } +#[derive(Resource, Default)] +struct MousePos(Vec2); + fn move_cam( mut cam: Query<&mut Transform, With>, mouse_buttons: Res>, - mut mouse_events: EventReader, - window: Query<&Window, With> + window: Query<&Window, With>, + mut mouse_pos: ResMut ) { let window = window.single(); - if mouse_buttons.pressed(MouseButton::Left) && !mouse_buttons.just_pressed(MouseButton::Left) { - for ev in mouse_events.read() { - if let Some(delta) = ev.delta { - let mut cam = cam.single_mut(); - cam.translation.x -= delta.x/window.width(); - cam.translation.y += delta.y/window.height(); - } + if let Some(cursor_pos) = window.cursor_position() { + if mouse_buttons.pressed(MouseButton::Left) { + let delta = cursor_pos - mouse_pos.0; + dbg!(delta); + let mut cam = cam.single_mut(); + cam.translation.x -= delta.x*cam.scale.x; + cam.translation.y += delta.y*cam.scale.y; } + mouse_pos.0 = cursor_pos; } } \ No newline at end of file