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

New scroll example and reduced compile time for lib using dev-dependencies #5

Merged
merged 6 commits into from
Apr 11, 2021
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
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ categories = ["game-engines", "game-development"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = { version = "0.5", default-features = false, features = ["render", "bevy_winit", "bevy_wgpu", "x11", "wayland" ] }
bevy = { version = "0.5", default-features = false, features = ["render"] }

[dev-dependencies]
bevy = { version = "0.5", default-features = false, features = ["render", "bevy_winit", "bevy_wgpu" ] }

[target.'cfg(target_os = "linux")'.dev-dependencies]
bevy = { version = "0.5", default-features = false, features = [ "x11", "wayland" ] }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn main() {
}
```

Alternativly you can see the example `basic.rs` located in the examples folder.
Alternatively you can see the example `basic.rs` or `scroll.rs` located in the examples folder.
You can run the example by cloning this repository and run the command: `cargo run --release --example basic`

## Customization
Expand Down
4 changes: 3 additions & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use bevy::prelude::*;
// Remove the line below if you are copying this to your own project
extern crate bevy_flycam;

use bevy::prelude::*;
use bevy_flycam::MovementSettings;
use bevy_flycam::PlayerPlugin;

Expand Down
94 changes: 94 additions & 0 deletions examples/scroll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Remove the line below if you are copying this to your own project
extern crate bevy_flycam;

use bevy::{input::mouse::MouseWheel, prelude::*, render::camera::PerspectiveProjection};

use bevy_flycam::FlyCam;
use bevy_flycam::MovementSettings;
use bevy_flycam::PlayerPlugin;

// From bevy examples:
// https://github.com/bevyengine/bevy/blob/latest/examples/3d/3d_scene.rs

#[derive(Clone, Eq, PartialEq, Debug, Hash)]
enum ScrollType {
MovementSpeed,
Zoom,
}

fn main() {
App::build()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_plugin(PlayerPlugin)
.insert_resource(MovementSettings {
..Default::default()
})
// Setting initial state
.add_state(ScrollType::MovementSpeed)
.add_startup_system(setup.system())
.add_system(switch_scroll_type.system())
.add_system(scroll.system())
.run();
}

/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn_bundle(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 {
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(LightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
});
}

// Listens for Z key being pressed and toggles between the two scroll-type states
fn switch_scroll_type(
mut scroll_type: ResMut<State<ScrollType>>,
keyboard_input: Res<Input<KeyCode>>,
) {
if keyboard_input.just_pressed(KeyCode::Z) {
match scroll_type.current() {
ScrollType::MovementSpeed => scroll_type.set(ScrollType::Zoom),
ScrollType::Zoom => scroll_type.set(ScrollType::MovementSpeed),
}
.unwrap();
println!("{:?}", scroll_type.current());
}
}

// Depending on the state, the mousescroll changes either the movement speed or the field-of-view of the camera
fn scroll(
mut settings: ResMut<MovementSettings>,
scroll_type: Res<State<ScrollType>>,
mut mouse_wheel_events: EventReader<MouseWheel>,
mut query: Query<(&FlyCam, &mut PerspectiveProjection)>,
) {
for event in mouse_wheel_events.iter() {
if *scroll_type.current() == ScrollType::MovementSpeed {
settings.speed = (settings.speed + event.y * 0.1).abs();
println!("Speed: {:?}", settings.speed);
} else {
for (_camera, mut project) in query.iter_mut() {
project.fov = (project.fov + event.y * 0.01).abs();
println!("{:?}", project);
}
}
}
}