Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to bevy 0.9 #22

Merged
merged 3 commits into from Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "bevy_flycam"
version = "0.8.1"
version = "0.9.0"
authors = ["Spencer Burris <sburris@posteo.net>"]
edition = "2021"
license = "ISC"
Expand All @@ -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"]}
bevy = {version = "0.9", default-features = false, features = ["x11", "wayland", "bevy_pbr", "bevy_core_pipeline", "bevy_asset"]}
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -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
Expand All @@ -25,15 +25,15 @@ 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 = "*"
```

or

```toml
[dependencies]
bevy = "0.8"
bevy = "0.9"
bevy_flycam = { git = "https://github.com/sburris0/bevy_flycam" }
```

Expand Down
6 changes: 3 additions & 3 deletions examples/basic.rs
Expand Up @@ -25,20 +25,20 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// 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()
});
Expand Down
8 changes: 4 additions & 4 deletions examples/scroll.rs
Expand Up @@ -34,20 +34,20 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// 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()
});
Expand All @@ -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");
Expand Down
50 changes: 33 additions & 17 deletions src/lib.rs
@@ -1,16 +1,19 @@
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<MouseMotion>,
pitch: f32,
yaw: f32,
}

/// Mouse sensitivity and movement speed

#[derive(Resource)]
pub struct MovementSettings {
pub sensitivity: f32,
pub speed: f32,
Expand All @@ -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
Expand All @@ -46,12 +57,13 @@ fn initial_grab_cursor(mut windows: ResMut<Windows>) {

/// 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
Expand All @@ -70,16 +82,17 @@ 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,
KeyCode::D => velocity += right,
KeyCode::Space => velocity += Vec3::Y,
KeyCode::LShift => velocity -= Vec3::Y,
_ => (),
}
},
}
}

Expand All @@ -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);
Expand Down