Skip to content

Commit

Permalink
Merge pull request #50 from purplg/bevy_0.15
Browse files Browse the repository at this point in the history
Update to Bevy 0.15
  • Loading branch information
sburris0 authored Dec 8, 2024
2 parents 4fc9071 + bb27950 commit 79b688c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 80 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ 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.14", default-features = false, features = [
bevy = { version = "0.15", default-features = false, features = [
"bevy_asset",
"bevy_core_pipeline",
"bevy_render",
"bevy_window",
] }

[dev-dependencies]
bevy = { version = "0.14", default-features = false, features = [
bevy = { version = "0.15", default-features = false, features = [
"bevy_asset",
"bevy_core_pipeline",
"bevy_pbr",
"bevy_state",
"bevy_window",
"ktx2",
"tonemapping_luts",
"wayland",
Expand Down
26 changes: 10 additions & 16 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use bevy_flycam::prelude::*;

fn main() {
App::new()
.insert_resource(Msaa::Sample4)
.add_plugins(DefaultPlugins)
.add_plugins(PlayerPlugin)
.insert_resource(MovementSettings {
Expand All @@ -24,25 +23,20 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn((PbrBundle {
mesh: meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(2.5))),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..Default::default()
},));
commands.spawn((
Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(2.5)))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
));

// cube
commands.spawn(PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(Color::srgb(0.8, 0.7, 0.6)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
});
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
MeshMaterial3d(materials.add(Color::srgb(0.8, 0.7, 0.6))),
Transform::from_xyz(0.0, 0.5, 0.0),
));

// light
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
});
commands.spawn((PointLight::default(), Transform::from_xyz(4.0, 8.0, 4.0)));

info!("Move camera around by using WASD for lateral movement");
info!("Use Left Shift and Spacebar for vertical movement");
Expand Down
26 changes: 10 additions & 16 deletions examples/key_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use bevy_flycam::prelude::*;

fn main() {
App::new()
.insert_resource(Msaa::Sample4)
.add_plugins(DefaultPlugins)
.add_plugins(PlayerPlugin)
.insert_resource(MovementSettings {
Expand All @@ -30,23 +29,18 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn((PbrBundle {
mesh: meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(2.5))),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..Default::default()
},));
commands.spawn((
Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(2.5)))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
));

// cube
commands.spawn(PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(Color::srgb(0.8, 0.7, 0.6)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
});
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
MeshMaterial3d(materials.add(Color::srgb(0.8, 0.7, 0.6))),
Transform::from_xyz(0.0, 0.5, 0.0),
));

// light
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
});
commands.spawn((PointLight::default(), Transform::from_xyz(4.0, 8.0, 4.0)));
}
33 changes: 13 additions & 20 deletions examples/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ enum ScrollType {

fn main() {
App::new()
.insert_resource(Msaa::Sample4)
.add_plugins(DefaultPlugins)
//NoCameraPlayerPlugin as we provide the camera
.add_plugins(NoCameraPlayerPlugin)
Expand All @@ -35,32 +34,26 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn((PbrBundle {
mesh: meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(2.5))),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..Default::default()
},));
commands.spawn((
Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(2.5)))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
));

// cube
commands.spawn(PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(Color::srgb(0.8, 0.7, 0.6)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
});
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
MeshMaterial3d(materials.add(Color::srgb(0.8, 0.7, 0.6))),
Transform::from_xyz(0.0, 0.5, 0.0),
));

// light
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
});
// light
commands.spawn((PointLight::default(), Transform::from_xyz(4.0, 8.0, 4.0)));

// add camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(-2.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
},
Camera3d::default(),
Transform::from_xyz(-2.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
FlyCam,
));

Expand Down
40 changes: 14 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bevy::ecs::event::{Events, ManualEventReader};
use bevy::input::mouse::MouseMotion;
use bevy::prelude::*;
use bevy::window::{CursorGrabMode, PrimaryWindow};
Expand All @@ -7,12 +6,6 @@ pub mod prelude {
pub use crate::*;
}

/// Keeps track of mouse motion events, pitch, and yaw
#[derive(Resource, Default)]
struct InputState {
reader_motion: ManualEventReader<MouseMotion>,
}

/// Mouse sensitivity and movement speed
#[derive(Resource)]
pub struct MovementSettings {
Expand Down Expand Up @@ -62,14 +55,14 @@ pub struct FlyCam;

/// Grabs/ungrabs mouse cursor
fn toggle_grab_cursor(window: &mut Window) {
match window.cursor.grab_mode {
match window.cursor_options.grab_mode {
CursorGrabMode::None => {
window.cursor.grab_mode = CursorGrabMode::Confined;
window.cursor.visible = false;
window.cursor_options.grab_mode = CursorGrabMode::Confined;
window.cursor_options.visible = false;
}
_ => {
window.cursor.grab_mode = CursorGrabMode::None;
window.cursor.visible = true;
window.cursor_options.grab_mode = CursorGrabMode::None;
window.cursor_options.visible = true;
}
}
}
Expand All @@ -86,11 +79,9 @@ fn initial_grab_cursor(mut primary_window: Query<&mut Window, With<PrimaryWindow
/// Spawns the `Camera3dBundle` to be controlled
fn setup_player(mut commands: Commands) {
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(-2.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
},
Camera3d::default(),
FlyCam,
Transform::from_xyz(-2.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
));
}

Expand All @@ -111,7 +102,7 @@ fn player_move(
let right = Vec3::new(local_z.z, 0., -local_z.x);

for key in keys.get_pressed() {
match window.cursor.grab_mode {
match window.cursor_options.grab_mode {
CursorGrabMode::None => (),
_ => {
let key = *key;
Expand All @@ -134,7 +125,7 @@ fn player_move(

velocity = velocity.normalize_or_zero();

transform.translation += velocity * time.delta_seconds() * settings.speed
transform.translation += velocity * time.delta_secs() * settings.speed
}
} else {
warn!("Primary window not found for `player_move`!");
Expand All @@ -145,15 +136,14 @@ fn player_move(
fn player_look(
settings: Res<MovementSettings>,
primary_window: Query<&Window, With<PrimaryWindow>>,
mut state: ResMut<InputState>,
motion: Res<Events<MouseMotion>>,
mut state: EventReader<MouseMotion>,
mut query: Query<&mut Transform, With<FlyCam>>,
) {
if let Ok(window) = primary_window.get_single() {
for mut transform in query.iter_mut() {
for ev in state.reader_motion.read(&motion) {
for ev in state.read() {
let (mut yaw, mut pitch, _) = transform.rotation.to_euler(EulerRot::YXZ);
match window.cursor.grab_mode {
match window.cursor_options.grab_mode {
CursorGrabMode::None => (),
_ => {
// Using smallest of height or width ensures equal vertical and horizontal sensitivity
Expand Down Expand Up @@ -209,8 +199,7 @@ fn initial_grab_on_flycam_spawn(
pub struct PlayerPlugin;
impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<InputState>()
.init_resource::<MovementSettings>()
app.init_resource::<MovementSettings>()
.init_resource::<KeyBindings>()
.add_systems(Startup, setup_player)
.add_systems(Startup, initial_grab_cursor)
Expand All @@ -224,8 +213,7 @@ impl Plugin for PlayerPlugin {
pub struct NoCameraPlayerPlugin;
impl Plugin for NoCameraPlayerPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<InputState>()
.init_resource::<MovementSettings>()
app.init_resource::<MovementSettings>()
.init_resource::<KeyBindings>()
.add_systems(Startup, initial_grab_cursor)
.add_systems(Startup, initial_grab_on_flycam_spawn)
Expand Down

0 comments on commit 79b688c

Please sign in to comment.