Merge from bevy-0.10 branch.

This commit is contained in:
Joe Ardent 2023-03-09 18:06:00 -08:00
commit a4c268f72d
11 changed files with 690 additions and 571 deletions

986
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,31 +5,33 @@ edition = "2021"
[dependencies] [dependencies]
rand = "0.8" rand = "0.8"
bevy_polyline = "0.4" # bevy_polyline = "0.4"
noise = { git = "https://github.com/Razaekel/noise-rs" } noise = { git = "https://github.com/Razaekel/noise-rs" }
hexasphere = "7" hexasphere = "7"
wgpu = "0.14" wgpu = "0.15"
bevy-inspector-egui = "0.17.0" bevy-inspector-egui = "0.18"
# wgpu = "0.12"
[features] [features]
inspector = [] inspector = []
[dependencies.bevy] [dependencies.bevy]
version = "0.9" version = "0.10"
default-features = false default-features = false
features = [ features = [
"bevy_gilrs", "bevy_gilrs",
"bevy_winit", "bevy_winit",
"render",
"png", "png",
"hdr", "hdr",
"x11", "x11",
"bevy_ui",
"bevy_text",
"bevy_gltf",
"bevy_sprite"
] ]
[dependencies.bevy_rapier3d] [dependencies.bevy_rapier3d]
features = ["debug-render-3d"] features = ["debug-render-3d"]
version = "0.20" version = "0.21"
# Maybe also enable only a small amount of optimization for our code: # Maybe also enable only a small amount of optimization for our code:
[profile.dev] [profile.dev]

View file

@ -1,6 +1,7 @@
use bevy::{ use bevy::{
diagnostic::FrameTimeDiagnosticsPlugin, diagnostic::FrameTimeDiagnosticsPlugin,
prelude::{App, IntoSystemDescriptor, Plugin, ReflectResource, Resource}, ecs::reflect::ReflectResource,
prelude::{App, IntoSystemConfigs, Plugin, Resource},
reflect::Reflect, reflect::Reflect,
}; };
use bevy_rapier3d::prelude::{NoUserData, RapierPhysicsPlugin}; use bevy_rapier3d::prelude::{NoUserData, RapierPhysicsPlugin};
@ -30,18 +31,17 @@ impl Plugin for CyberActionPlugin {
.add_plugin(RapierPhysicsPlugin::<NoUserData>::default()) .add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
.add_startup_system(timestep_setup) .add_startup_system(timestep_setup)
.add_plugin(FrameTimeDiagnosticsPlugin::default()) .add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_system(surface_fix.label("surface_fix")) .add_systems(
.add_system(gravity.label("gravity").before("cat")) (
.add_system(cyber_lean.before("cat").after("gravity")) gravity,
.add_system(falling_cat.label("cat")) cyber_lean,
.add_system(input_forces.label("iforces").after("cat")) falling_cat,
.add_system( input_forces,
tunnel_out drag,
.label("tunnel") tunnel_out,
.before("surface_fix") surface_fix,
.after("drag"), )
) .chain(),
.add_system(surface_fix.label("surface_fix").after("cat")) );
.add_system(drag.label("drag").after("iforces"));
} }
} }

View file

@ -2,7 +2,9 @@ mod body;
mod components; mod components;
mod wheels; mod wheels;
use bevy::prelude::{App, Assets, Mesh, Plugin, ResMut, StandardMaterial, StartupStage}; use bevy::prelude::{
App, Assets, IntoSystemConfig, Mesh, Plugin, ResMut, StandardMaterial, StartupSet,
};
use bevy_rapier3d::prelude::Group; use bevy_rapier3d::prelude::Group;
pub(crate) use self::components::*; pub(crate) use self::components::*;
@ -21,6 +23,6 @@ impl Plugin for CyberBikePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.insert_resource(WheelConfig::default()) app.insert_resource(WheelConfig::default())
.register_type::<WheelConfig>() .register_type::<WheelConfig>()
.add_startup_system_to_stage(StartupStage::PostStartup, spawn_cyberbike); .add_startup_system(spawn_cyberbike.in_base_set(StartupSet::PostStartup));
} }
} }

View file

@ -5,8 +5,9 @@ use crate::{bike::CyberBikeBody, input::InputState};
// 85 degrees in radians // 85 degrees in radians
const MAX_PITCH: f32 = 1.48353; const MAX_PITCH: f32 = 1.48353;
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Component)] #[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Component, States, Default)]
enum CyberCameras { enum CyberCameras {
#[default]
Hero, Hero,
Debug, Debug,
} }
@ -99,7 +100,7 @@ fn update_active_camera(
) { ) {
// find the camera with the current state, set it as the ActiveCamera // find the camera with the current state, set it as the ActiveCamera
query.iter_mut().for_each(|(mut cam, cyber)| { query.iter_mut().for_each(|(mut cam, cyber)| {
if cyber.eq(state.current()) { if cyber.eq(&state.0) {
cam.is_active = true; cam.is_active = true;
} else { } else {
cam.is_active = false; cam.is_active = false;
@ -107,11 +108,15 @@ fn update_active_camera(
}); });
} }
fn cycle_cam_state(mut state: ResMut<State<CyberCameras>>, mut keys: ResMut<Input<KeyCode>>) { fn cycle_cam_state(
state: Res<State<CyberCameras>>,
mut next: ResMut<NextState<CyberCameras>>,
mut keys: ResMut<Input<KeyCode>>,
) {
if keys.just_pressed(KeyCode::D) { if keys.just_pressed(KeyCode::D) {
let new_state = state.current().next(); let new_state = state.0.next();
info!("{:?}", new_state); info!("{:?}", new_state);
state.set(new_state).unwrap(); next.set(new_state);
keys.reset(KeyCode::D); keys.reset(KeyCode::D);
} }
} }
@ -127,7 +132,7 @@ impl Plugin for CyberCamPlugin {
fn common(app: &mut bevy::prelude::App) { fn common(app: &mut bevy::prelude::App) {
app.insert_resource(DebugCamOffset::default()) app.insert_resource(DebugCamOffset::default())
.add_startup_system(setup_cybercams) .add_startup_system(setup_cybercams)
.add_state(CyberCameras::Hero) .add_state::<CyberCameras>()
.add_system(cycle_cam_state) .add_system(cycle_cam_state)
.add_system(update_active_camera) .add_system(update_active_camera)
.add_system(follow_cyberbike); .add_system(follow_cyberbike);

View file

@ -2,60 +2,60 @@ use bevy::{
prelude::*, prelude::*,
render::mesh::{Indices, VertexAttributeValues}, render::mesh::{Indices, VertexAttributeValues},
}; };
use bevy_polyline::prelude::{Polyline, PolylineBundle, PolylineMaterial, PolylinePlugin}; // use bevy_polyline::prelude::{Polyline, PolylineBundle, PolylineMaterial, PolylinePlugin};
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use crate::{lights::AnimateCyberLightWireframe, planet::CyberPlanet}; use crate::{lights::AnimateCyberLightWireframe, planet::CyberPlanet};
pub const BISEXY_COLOR: Color = Color::hsla(292.0, 0.9, 0.60, 1.1); pub const BISEXY_COLOR: Color = Color::hsla(292.0, 0.9, 0.60, 1.1);
fn wireframe_planet( // fn wireframe_planet(
mut commands: Commands, // mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, // mut meshes: ResMut<Assets<Mesh>>,
mut polylines: ResMut<Assets<Polyline>>, // mut polylines: ResMut<Assets<Polyline>>,
mut polymats: ResMut<Assets<PolylineMaterial>>, // mut polymats: ResMut<Assets<PolylineMaterial>>,
query: Query<&Handle<Mesh>, With<CyberPlanet>>, // query: Query<&Handle<Mesh>, With<CyberPlanet>>,
) { // ) {
let handle = query.single(); // let handle = query.single();
let mesh = meshes.get_mut(handle).unwrap(); // let mesh = meshes.get_mut(handle).unwrap();
let vertices = mesh.attribute(Mesh::ATTRIBUTE_POSITION).unwrap(); // let vertices = mesh.attribute(Mesh::ATTRIBUTE_POSITION).unwrap();
let mut pts = Vec::with_capacity(vertices.len()); // let mut pts = Vec::with_capacity(vertices.len());
if let VertexAttributeValues::Float32x3(verts) = vertices { // if let VertexAttributeValues::Float32x3(verts) = vertices {
let indices = mesh.indices().unwrap(); // let indices = mesh.indices().unwrap();
if let Indices::U32(indices) = indices { // if let Indices::U32(indices) = indices {
for i in indices.iter() { // for i in indices.iter() {
let v = verts[*i as usize]; // let v = verts[*i as usize];
let v = Vec3::from_slice(&v); // let v = Vec3::from_slice(&v);
pts.push(v); // pts.push(v);
} // }
} // }
} // }
let mut verts = Vec::with_capacity((pts.len() as f32 * 1.4) as usize); // let mut verts = Vec::with_capacity((pts.len() as f32 * 1.4) as usize);
for pts in pts.chunks(3) { // for pts in pts.chunks(3) {
if pts.len() > 1 { // if pts.len() > 1 {
verts.extend_from_slice(pts); // verts.extend_from_slice(pts);
verts.push(Vec3::NAN); // verts.push(Vec3::NAN);
} // }
} // }
// don't need the indices anymore // // don't need the indices anymore
mesh.duplicate_vertices(); // mesh.duplicate_vertices();
mesh.compute_flat_normals(); // mesh.compute_flat_normals();
commands.spawn(PolylineBundle { // commands.spawn(PolylineBundle {
polyline: polylines.add(Polyline { vertices: verts }), // polyline: polylines.add(Polyline { vertices: verts }),
material: polymats.add(PolylineMaterial { // material: polymats.add(PolylineMaterial {
width: 101.0, // width: 101.0,
color: BISEXY_COLOR, // color: BISEXY_COLOR,
perspective: true, // perspective: true,
depth_bias: -0.001, // depth_bias: -0.001,
}), // }),
..Default::default() // ..Default::default()
}); // });
} // }
fn wireframify_lights(mut lights: Query<&mut AnimateCyberLightWireframe>) { fn wireframify_lights(mut lights: Query<&mut AnimateCyberLightWireframe>) {
let chance = 0.005; let chance = 0.005;
@ -98,8 +98,6 @@ impl Plugin for CyberGlamorPlugin {
app.add_plugin(rplugin); app.add_plugin(rplugin);
} }
app.add_startup_system_to_stage(StartupStage::PostStartup, wireframe_planet) app.add_system(wireframify_lights);
.add_system(wireframify_lights)
.add_plugin(PolylinePlugin);
} }
} }

View file

@ -1,4 +1,8 @@
use bevy::{prelude::*, utils::HashSet}; use bevy::{
input::gamepad::{GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadEvent},
prelude::*,
utils::HashSet,
};
use crate::camera::DebugCamOffset; use crate::camera::DebugCamOffset;
@ -46,39 +50,36 @@ fn update_debug_cam(mut offset: ResMut<DebugCamOffset>, mut keys: ResMut<Input<K
} }
fn update_input(mut events: EventReader<GamepadEvent>, mut istate: ResMut<InputState>) { fn update_input(mut events: EventReader<GamepadEvent>, mut istate: ResMut<InputState>) {
for GamepadEvent { for pad_event in events.iter() {
gamepad: _, match pad_event {
event_type: ev, GamepadEvent::Button(button_event) => {
} in events.iter() let GamepadButtonChangedEvent {
{ button_type, value, ..
match *ev { } = button_event;
GamepadEventType::ButtonChanged(GamepadButtonType::RightTrigger2, val) => { match button_type {
istate.throttle = val; GamepadButtonType::RightTrigger => istate.throttle = *value,
} GamepadButtonType::LeftTrigger => istate.throttle = -value,
GamepadEventType::ButtonChanged(GamepadButtonType::LeftTrigger2, val) => { GamepadButtonType::East => {
istate.throttle = -val; if value > &0.5 {
} istate.brake = true;
GamepadEventType::ButtonChanged(GamepadButtonType::East, val) => { } else {
if val > 0.5 { istate.brake = false;
istate.brake = true; }
} else { }
istate.brake = false; _ => info!("unhandled button press: {button_event:?}"),
} }
} }
GamepadEventType::AxisChanged(GamepadAxisType::LeftStickX, val) => { GamepadEvent::Axis(axis_event) => {
istate.yaw = val; let GamepadAxisChangedEvent {
} axis_type, value, ..
// ignore spurious vertical movement for now } = axis_event;
GamepadEventType::AxisChanged(GamepadAxisType::LeftStickY, val) => { match axis_type {
istate.pitch = val; _ => info!("unhandled axis event: {axis_event:?}"),
} }
_ => {
info!("unhandled gamepad event: {:?}", ev);
} }
GamepadEvent::Connection(_) => {}
} }
} }
//dbg!(&istate);
} }
pub struct CyberInputPlugin; pub struct CyberInputPlugin;

View file

@ -1,6 +1,7 @@
use bevy::{ use bevy::{
ecs::schedule::StageLabel, ecs::schedule::SystemSet,
prelude::{ResMut, SystemLabel, Vec3, Windows}, prelude::{Query, Vec3, Window, With},
window::PrimaryWindow,
}; };
pub mod action; pub mod action;
@ -12,7 +13,7 @@ pub mod lights;
pub mod planet; pub mod planet;
pub mod ui; pub mod ui;
#[derive(Clone, Debug, Hash, PartialEq, Eq, SystemLabel, StageLabel)] #[derive(Clone, Debug, Hash, PartialEq, Eq, SystemSet)]
pub enum Label { pub enum Label {
Geometry, Geometry,
Glamor, Glamor,
@ -20,10 +21,10 @@ pub enum Label {
Action, Action,
} }
pub fn disable_mouse_trap(mut windows: ResMut<Windows>) { pub fn disable_mouse_trap(mut window: Query<&mut Window, With<PrimaryWindow>>) {
let window = windows.get_primary_mut().unwrap(); let mut window = window.get_single_mut().unwrap();
window.set_cursor_grab_mode(bevy::window::CursorGrabMode::None); window.cursor.grab_mode = bevy::window::CursorGrabMode::None;
window.set_cursor_visibility(true); window.cursor.visible = true;
} }
pub fn random_unit_vec(r: &mut impl rand::prelude::Rng) -> Vec3 { pub fn random_unit_vec(r: &mut impl rand::prelude::Rng) -> Vec3 {

View file

@ -74,10 +74,13 @@ fn spawn_moving_lights(
builder builder
// now a simple mesh to show a wireframe. // now a simple mesh to show a wireframe.
.spawn(PbrBundle { .spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere { mesh: meshes.add(
radius, Mesh::try_from(shape::Icosphere {
subdivisions: 1, radius,
})), subdivisions: 1,
})
.unwrap(),
),
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: Color::hsla(272.0, 0.7, 0.56, 0.7), base_color: Color::hsla(272.0, 0.7, 0.56, 0.7),
emissive: color, emissive: color,
@ -128,10 +131,13 @@ fn spawn_static_lights(
}) })
.with_children(|builder| { .with_children(|builder| {
builder.spawn(PbrBundle { builder.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere { mesh: meshes.add(
radius: 10.0, Mesh::try_from(shape::Icosphere {
subdivisions: 2, radius: 10.0,
})), subdivisions: 2,
})
.unwrap(),
),
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: Color::BLUE, base_color: Color::BLUE,
emissive: Color::PINK, emissive: Color::PINK,
@ -149,10 +155,13 @@ fn spawn_static_lights(
}) })
.with_children(|builder| { .with_children(|builder| {
builder.spawn(PbrBundle { builder.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere { mesh: meshes.add(
radius: 10.0, Mesh::try_from(shape::Icosphere {
subdivisions: 2, radius: 10.0,
})), subdivisions: 2,
})
.unwrap(),
),
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: Color::PINK, base_color: Color::PINK,
emissive: Color::BLUE, emissive: Color::BLUE,

View file

@ -10,14 +10,13 @@ const CYBER_SKY: Color = Color::rgb(0.07, 0.001, 0.02);
fn main() { fn main() {
let mut app = App::new(); let mut app = App::new();
app.insert_resource(Msaa { samples: 4 }) app.insert_resource(Msaa::Sample4)
.insert_resource(ClearColor(CYBER_SKY)) .insert_resource(ClearColor(CYBER_SKY))
.add_plugins(DefaultPlugins.set(WindowPlugin { .add_plugins(DefaultPlugins.set(WindowPlugin {
window: WindowDescriptor { primary_window: Some(Window {
width: 2560.0, resolution: (2560.0, 1440.0).into(),
height: 1440.0,
..Default::default() ..Default::default()
}, }),
..Default::default() ..Default::default()
})) }))
.add_plugin(CyberPlanetPlugin) .add_plugin(CyberPlanetPlugin)

View file

@ -59,7 +59,7 @@ fn spawn_planet(
pub struct CyberPlanetPlugin; pub struct CyberPlanetPlugin;
impl Plugin for CyberPlanetPlugin { impl Plugin for CyberPlanetPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_startup_system(spawn_planet.label(Label::Geometry)); app.add_startup_system(spawn_planet);
} }
} }