ok, works

This commit is contained in:
Joe Ardent 2023-03-09 17:50:14 -08:00
parent aa9a89ae94
commit 63d3bb87d4
11 changed files with 371 additions and 1254 deletions

1391
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,12 +5,11 @@ 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.15" wgpu = "0.15"
bevy-inspector-egui = "0.18" bevy-inspector-egui = "0.18"
# wgpu = "0.12"
[features] [features]
inspector = [] inspector = []
@ -25,7 +24,9 @@ features = [
"hdr", "hdr",
"x11", "x11",
"bevy_ui", "bevy_ui",
"bevy_text" "bevy_text",
"bevy_gltf",
"bevy_sprite"
] ]
[dependencies.bevy_rapier3d] [dependencies.bevy_rapier3d]

View file

@ -1,7 +1,7 @@
use bevy::{ use bevy::{
diagnostic::FrameTimeDiagnosticsPlugin, diagnostic::FrameTimeDiagnosticsPlugin,
ecs::reflect::ReflectResource, ecs::reflect::ReflectResource,
prelude::{App, Plugin, Resource}, prelude::{App, IntoSystemConfigs, Plugin, Resource},
reflect::Reflect, reflect::Reflect,
}; };
use bevy_rapier3d::prelude::{NoUserData, RapierPhysicsPlugin}; use bevy_rapier3d::prelude::{NoUserData, RapierPhysicsPlugin};
@ -42,6 +42,6 @@ impl Plugin for CyberActionPlugin {
surface_fix, surface_fix,
) )
.chain(), .chain(),
) );
} }
} }

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, StartupSet}; 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(StartupSet::PostStartup, spawn_cyberbike); .add_startup_system(spawn_cyberbike.in_base_set(StartupSet::PostStartup));
} }
} }

View file

@ -5,18 +5,13 @@ 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, States)] #[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Component, States, Default)]
enum CyberCameras { enum CyberCameras {
#[default]
Hero, Hero,
Debug, Debug,
} }
impl Default for CyberCameras {
fn default() -> Self {
Self::Hero
}
}
#[derive(Debug, Resource)] #[derive(Debug, Resource)]
pub struct DebugCamOffset { pub struct DebugCamOffset {
pub rot: f32, pub rot: f32,
@ -105,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;
@ -113,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);
} }
} }
@ -133,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(StartupSet::PostStartup, wireframe_planet) app.add_system(wireframify_lights);
.add_system(wireframify_lights)
.add_plugin(PolylinePlugin);
} }
} }

View file

@ -1,4 +1,8 @@
use bevy::{input::gamepad::GamepadEvent, prelude::*, utils::HashSet}; use bevy::{
input::gamepad::{GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadEvent},
prelude::*,
utils::HashSet,
};
use crate::camera::DebugCamOffset; use crate::camera::DebugCamOffset;
@ -48,29 +52,32 @@ 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 pad_event in events.iter() { for pad_event in events.iter() {
match pad_event { match pad_event {
GamepadEvent::Button(GamepadButtonType::RightTrigger2, val) => { GamepadEvent::Button(button_event) => {
istate.throttle = val; let GamepadButtonChangedEvent {
} button_type, value, ..
GamepadEvent::Button(GamepadButtonType::LeftTrigger2, val) => { } = button_event;
istate.throttle = -val; match button_type {
} GamepadButtonType::RightTrigger => istate.throttle = *value,
GamepadEvent::Button(GamepadButtonType::East, val) => { GamepadButtonType::LeftTrigger => istate.throttle = -value,
if val > 0.5 { GamepadButtonType::East => {
istate.brake = true; if value > &0.5 {
} else { istate.brake = true;
istate.brake = false; } else {
istate.brake = false;
}
}
_ => info!("unhandled button press: {button_event:?}"),
} }
} }
GamepadEvent::Axis(GamepadAxisType::LeftStickX, val) => { GamepadEvent::Axis(axis_event) => {
istate.yaw = val; let GamepadAxisChangedEvent {
} axis_type, value, ..
// ignore spurious vertical movement for now } = axis_event;
GamepadEvent::Axis(GamepadAxisType::LeftStickY, val) => { match axis_type {
istate.pitch = val; _ => info!("unhandled axis event: {axis_event:?}"),
} }
_ => {
info!("unhandled gamepad event: {:?}", pad_event);
} }
GamepadEvent::Connection(_) => {}
} }
} }
} }

View file

@ -1,6 +1,7 @@
use bevy::{ use bevy::{
ecs::schedule::SystemSet, ecs::schedule::SystemSet,
prelude::{Query, Vec3, Window}, prelude::{Query, Vec3, Window, With},
window::PrimaryWindow,
}; };
pub mod action; pub mod action;
@ -20,10 +21,10 @@ pub enum Label {
Action, Action,
} }
pub fn disable_mouse_trap(mut windows: Query<&Window>) { 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);
} }
} }