switch to bevy 0.5 for egui-inspector compat; get spinny lights

This commit is contained in:
Joe Ardent 2021-11-07 23:58:43 -08:00
parent fff3e98eca
commit 0a9659f364
5 changed files with 861 additions and 478 deletions

4
.rustfmt.toml Normal file
View file

@ -0,0 +1,4 @@
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
wrap_comments = true
edition = "2021"

1200
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bevy = { path = "../bevy" } bevy = "0.5"
bevy-inspector-egui = "0.6.1"

View file

@ -1,8 +1,12 @@
use bevy::app::{Events, ManualEventReader}; use bevy::{
use bevy::input::mouse::MouseMotion; app::{Events, ManualEventReader},
use bevy::prelude::*; input::{mouse::MouseMotion, Input},
prelude::*,
};
// stolen with neither shame nor pity from git@github.com:sburris0/bevy_flycam.git, b90f6fc, which is copyright 2020 Spencer Burris // stolen with neither shame nor pity from
// git@github.com:sburris0/bevy_flycam.git, b90f6fc, which is copyright 2020
// Spencer Burris
/// Keeps track of mouse motion events, pitch, and yaw /// Keeps track of mouse motion events, pitch, and yaw
#[derive(Default)] #[derive(Default)]
@ -28,7 +32,7 @@ impl Default for MovementSettings {
} }
/// Used in queries when you want flycams and not other cameras /// Used in queries when you want flycams and not other cameras
#[derive(Debug, Component)] #[derive(Debug)]
pub struct FlyCam; pub struct FlyCam;
/// Grabs/ungrabs mouse cursor /// Grabs/ungrabs mouse cursor
@ -46,7 +50,7 @@ fn initial_grab_cursor(mut windows: ResMut<Windows>) {
fn setup_player(mut commands: Commands) { fn setup_player(mut commands: Commands) {
commands commands
.spawn_bundle(PerspectiveCameraBundle { .spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), transform: Transform::from_xyz(0.0, 0.0, -1200.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default() ..Default::default()
}) })
.insert(FlyCam); .insert(FlyCam);
@ -101,7 +105,8 @@ fn player_look(
for (_camera, mut transform) in query.iter_mut() { for (_camera, mut transform) in query.iter_mut() {
for ev in state.reader_motion.iter(&motion) { for ev in state.reader_motion.iter(&motion) {
if window.cursor_locked() { if window.cursor_locked() {
// Using smallest of height or width ensures equal vertical and horizontal sensitivity // Using smallest of height or width ensures equal vertical and horizontal
// sensitivity
let window_scale = window.height().min(window.width()); let window_scale = window.height().min(window.width());
state.pitch -= (settings.sensitivity * ev.delta.y * window_scale).to_radians(); state.pitch -= (settings.sensitivity * ev.delta.y * window_scale).to_radians();
@ -124,10 +129,11 @@ fn cursor_grab(keys: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
} }
} }
/// Contains everything needed to add first-person fly camera behavior to your game /// Contains everything needed to add first-person fly camera behavior to your
/// game
pub struct PlayerPlugin; pub struct PlayerPlugin;
impl Plugin for PlayerPlugin { impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut bevy::prelude::AppBuilder) {
app.init_resource::<InputState>() app.init_resource::<InputState>()
.init_resource::<MovementSettings>() .init_resource::<MovementSettings>()
.add_startup_system(setup_player.system()) .add_startup_system(setup_player.system())
@ -141,7 +147,7 @@ impl Plugin for PlayerPlugin {
/// Same as `PlayerPlugin` but does not spawn a camera /// Same as `PlayerPlugin` but does not spawn a camera
pub struct NoCameraPlayerPlugin; pub struct NoCameraPlayerPlugin;
impl Plugin for NoCameraPlayerPlugin { impl Plugin for NoCameraPlayerPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut bevy::prelude::AppBuilder) {
app.init_resource::<InputState>() app.init_resource::<InputState>()
.init_resource::<MovementSettings>() .init_resource::<MovementSettings>()
.add_startup_system(initial_grab_cursor.system()) .add_startup_system(initial_grab_cursor.system())

View file

@ -1,19 +1,23 @@
use bevy::prelude::*; use bevy::prelude::*;
use cyber_rider::flycam::MovementSettings; use bevy_inspector_egui::WorldInspectorPlugin;
use cyber_rider::flycam::PlayerPlugin; use cyber_rider::flycam::{MovementSettings, PlayerPlugin};
// stolen with neither shame nor pity from git@github.com:sburris0/bevy_flycam.git, b90f6fc, which is copyright 2020 Spencer Burris // stolen with neither shame nor pity from
// git@github.com:sburris0/bevy_flycam.git, b90f6fc, which is copyright 2020
// Spencer Burris
fn main() { fn main() {
App::new() App::build()
.insert_resource(Msaa { samples: 4 }) .insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugin(PlayerPlugin) .add_plugin(PlayerPlugin)
.add_plugin(WorldInspectorPlugin::new())
.insert_resource(MovementSettings { .insert_resource(MovementSettings {
sensitivity: 0.00015, // default: 0.00012 sensitivity: 0.00015, // default: 0.00012
speed: 12.0, // default: 12.0 speed: 12.0, // default: 12.0
}) })
.add_startup_system(setup.system()) .add_startup_system(setup.system())
.add_system(animate_lights.system())
.run(); .run();
} }
@ -23,22 +27,94 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
// plane let red_light = Light {
intensity: 50_000.0,
range: 1800.0,
color: Color::RED,
..Default::default()
};
let blue_light = Light {
intensity: 50_000.0,
range: 1800.0,
color: Color::BLUE,
..Default::default()
};
let purple_light = Light {
intensity: 50_000.0,
range: 1800.0,
color: Color::PURPLE,
..Default::default()
};
// world
commands.spawn_bundle(PbrBundle { commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })), mesh: meshes.add(Mesh::from(shape::Icosphere {
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), radius: 300.0,
subdivisions: 5,
})),
material: materials.add(StandardMaterial {
base_color: Color::GRAY,
metallic: 0.7,
roughness: 0.2,
..Default::default()
}),
..Default::default() ..Default::default()
}); });
// cube // east light
commands.spawn_bundle(PbrBundle { commands.spawn_bundle(LightBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), transform: Transform::from_xyz(750.0, 0.0, 0.0),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), light: purple_light.clone(),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default() ..Default::default()
}); });
// light // west light
commands.spawn_bundle(PointLightBundle { commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0), transform: Transform::from_xyz(-750.0, 0.0, 0.0),
light: purple_light.clone(),
..Default::default()
});
// north light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(0.0, 0.0, 750.0),
light: purple_light.clone(),
..Default::default()
});
// south light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(0.0, 0.0, -750.0),
light: purple_light,
..Default::default()
});
// up light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(0.0, 750.0, 0.0),
light: red_light,
..Default::default()
});
// down light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(0.0, 750.0, 0.0),
light: blue_light,
..Default::default() ..Default::default()
}); });
} }
fn animate_lights(mut query: Query<&mut Transform, With<Light>>) {
for mut transform in query.iter_mut() {
let translation = &transform.translation;
let x = translation.x;
let y = if translation.y.abs() < 0.1 {
translation.y + 0.15
} else {
translation.y
};
let theta = y.atan2(x) + 0.174533; // 10 degrees == 0.174533 radians
let x_new = 750.0 * theta.cos();
let y_new = 750.0 * theta.sin();
*transform = Transform::from_xyz(x_new, y_new, 0.0);
}
}