From 1b7df5c2f5f89ecdfc5f810e14ca8a2e063e5bed Mon Sep 17 00:00:00 2001 From: Mikkel Rasmussen Date: Sun, 13 Nov 2022 01:58:51 +0100 Subject: [PATCH 1/3] Updated to bevy 0.9 --- Cargo.toml | 4 ++-- README.md | 4 ++-- examples/basic.rs | 6 +++--- examples/scroll.rs | 8 ++++---- src/lib.rs | 50 ++++++++++++++++++++++++++++++---------------- 5 files changed, 44 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7dca0cf..0eb4718 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ resolver = "2" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bevy = {version = "0.8", default-features = false, features = ["bevy_render", "bevy_core_pipeline", "bevy_asset"]} +bevy = {version = "0.9", default-features = false, features = ["bevy_render", "bevy_core_pipeline", "bevy_asset"]} [dev-dependencies] -bevy = {version = "0.8", default-features = false, features = ["x11", "wayland", "bevy_pbr", "bevy_core_pipeline", "bevy_asset"]} \ No newline at end of file +bevy = {version = "0.9", default-features = false, features = ["x11", "wayland", "bevy_pbr", "bevy_core_pipeline", "bevy_asset"]} \ No newline at end of file diff --git a/README.md b/README.md index 834d42e..0b88c1f 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ There are a few notable differences from [bevy_fly_camera](https://github.com/mc 1. Add to `Cargo.toml` or copy `lib.rs` to your own file ```toml [dependencies] -bevy = "0.8" +bevy = "0.9" bevy_flycam = "*" ``` @@ -33,7 +33,7 @@ or ```toml [dependencies] -bevy = "0.8" +bevy = "0.9" bevy_flycam = { git = "https://github.com/sburris0/bevy_flycam" } ``` diff --git a/examples/basic.rs b/examples/basic.rs index db82411..fda0a89 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -25,20 +25,20 @@ fn setup( mut materials: ResMut>, ) { // plane - commands.spawn_bundle(PbrBundle { + commands.spawn(PbrBundle { mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })), material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), ..Default::default() }); // cube - commands.spawn_bundle(PbrBundle { + commands.spawn(PbrBundle { mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), transform: Transform::from_xyz(0.0, 0.5, 0.0), ..Default::default() }); // light - commands.spawn_bundle(PointLightBundle { + commands.spawn(PointLightBundle { transform: Transform::from_xyz(4.0, 8.0, 4.0), ..Default::default() }); diff --git a/examples/scroll.rs b/examples/scroll.rs index c6e28af..524621c 100644 --- a/examples/scroll.rs +++ b/examples/scroll.rs @@ -34,20 +34,20 @@ fn setup( mut materials: ResMut>, ) { // plane - commands.spawn_bundle(PbrBundle { + commands.spawn(PbrBundle { mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })), material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), ..Default::default() }); // cube - commands.spawn_bundle(PbrBundle { + commands.spawn(PbrBundle { mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), transform: Transform::from_xyz(0.0, 0.5, 0.0), ..Default::default() }); // light - commands.spawn_bundle(PointLightBundle { + commands.spawn(PointLightBundle { transform: Transform::from_xyz(4.0, 8.0, 4.0), ..Default::default() }); @@ -59,7 +59,7 @@ fn setup( }; // add plugin - commands.spawn_bundle(camera).insert(FlyCam); + commands.spawn(camera).insert(FlyCam); info!("Press 'Z' to switch between Movement Speed and Zoom"); info!("Changing the selected value by scrolling the mousewheel"); diff --git a/src/lib.rs b/src/lib.rs index 160a2ed..2a630ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,10 @@ use bevy::ecs::event::{Events, ManualEventReader}; use bevy::input::mouse::MouseMotion; use bevy::prelude::*; +use bevy::window::CursorGrabMode; /// Keeps track of mouse motion events, pitch, and yaw -#[derive(Default)] +#[derive(Resource, Default)] struct InputState { reader_motion: ManualEventReader, pitch: f32, @@ -11,6 +12,8 @@ struct InputState { } /// Mouse sensitivity and movement speed + +#[derive(Resource)] pub struct MovementSettings { pub sensitivity: f32, pub speed: f32, @@ -31,8 +34,16 @@ pub struct FlyCam; /// Grabs/ungrabs mouse cursor fn toggle_grab_cursor(window: &mut Window) { - window.set_cursor_lock_mode(!window.cursor_locked()); - window.set_cursor_visibility(!window.cursor_visible()); + match window.cursor_grab_mode() { + CursorGrabMode::None => { + window.set_cursor_grab_mode(CursorGrabMode::Confined); + window.set_cursor_visibility(false); + } + _ => { + window.set_cursor_grab_mode(CursorGrabMode::None); + window.set_cursor_visibility(true); + }, + } } /// Grabs the cursor when game first starts @@ -46,12 +57,13 @@ fn initial_grab_cursor(mut windows: ResMut) { /// Spawns the `Camera3dBundle` to be controlled fn setup_player(mut commands: Commands) { - commands - .spawn_bundle(Camera3dBundle { + commands.spawn(( + Camera3dBundle { transform: Transform::from_xyz(-2.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), ..Default::default() - }) - .insert(FlyCam); + }, + FlyCam, + )); } /// Handles keyboard input and movement @@ -70,8 +82,9 @@ fn player_move( let right = Vec3::new(local_z.z, 0., -local_z.x); for key in keys.get_pressed() { - if window.cursor_locked() { - match key { + match window.cursor_grab_mode() { + CursorGrabMode::None => (), + _ => match key { KeyCode::W => velocity += forward, KeyCode::S => velocity -= forward, KeyCode::A => velocity -= right, @@ -79,7 +92,7 @@ fn player_move( KeyCode::Space => velocity += Vec3::Y, KeyCode::LShift => velocity -= Vec3::Y, _ => (), - } + }, } } @@ -104,13 +117,16 @@ fn player_look( let mut delta_state = state.as_mut(); for mut transform in query.iter_mut() { for ev in delta_state.reader_motion.iter(&motion) { - if window.cursor_locked() { - // Using smallest of height or width ensures equal vertical and horizontal sensitivity - let window_scale = window.height().min(window.width()); - delta_state.pitch -= - (settings.sensitivity * ev.delta.y * window_scale).to_radians(); - delta_state.yaw -= - (settings.sensitivity * ev.delta.x * window_scale).to_radians(); + match window.cursor_grab_mode() { + CursorGrabMode::None => (), + _ => { + // Using smallest of height or width ensures equal vertical and horizontal sensitivity + let window_scale = window.height().min(window.width()); + delta_state.pitch -= + (settings.sensitivity * ev.delta.y * window_scale).to_radians(); + delta_state.yaw -= + (settings.sensitivity * ev.delta.x * window_scale).to_radians(); + } } delta_state.pitch = delta_state.pitch.clamp(-1.54, 1.54); From 1766014187445745767b0c25785885d134d4999c Mon Sep 17 00:00:00 2001 From: Mikkel Rasmussen Date: Sun, 13 Nov 2022 02:00:40 +0100 Subject: [PATCH 2/3] fmt --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2a630ba..8a9db3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,7 @@ fn toggle_grab_cursor(window: &mut Window) { _ => { window.set_cursor_grab_mode(CursorGrabMode::None); window.set_cursor_visibility(true); - }, + } } } From 741297829e3ea7c7a5455937946fa3999487e9e2 Mon Sep 17 00:00:00 2001 From: Mikkel Rasmussen Date: Sun, 13 Nov 2022 02:04:06 +0100 Subject: [PATCH 3/3] Updated version of readme and cargo --- Cargo.toml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0eb4718..2956730 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bevy_flycam" -version = "0.8.1" +version = "0.9.0" authors = ["Spencer Burris "] edition = "2021" license = "ISC" diff --git a/README.md b/README.md index 0b88c1f..72f1872 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ![docs.rs](https://img.shields.io/docsrs/bevy_flycam) -A basic first-person fly camera for Bevy 0.8 +A basic first-person fly camera for Bevy 0.9 ## Controls * WASD to move horizontally