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]
rand = "0.8"
bevy_polyline = "0.4"
# bevy_polyline = "0.4"
noise = { git = "https://github.com/Razaekel/noise-rs" }
hexasphere = "7"
wgpu = "0.15"
bevy-inspector-egui = "0.18"
# wgpu = "0.12"
[features]
inspector = []
@ -25,7 +24,9 @@ features = [
"hdr",
"x11",
"bevy_ui",
"bevy_text"
"bevy_text",
"bevy_gltf",
"bevy_sprite"
]
[dependencies.bevy_rapier3d]

View File

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

View File

@ -2,7 +2,9 @@ mod body;
mod components;
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;
pub(crate) use self::components::*;
@ -21,6 +23,6 @@ impl Plugin for CyberBikePlugin {
fn build(&self, app: &mut App) {
app.insert_resource(WheelConfig::default())
.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
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 {
#[default]
Hero,
Debug,
}
impl Default for CyberCameras {
fn default() -> Self {
Self::Hero
}
}
#[derive(Debug, Resource)]
pub struct DebugCamOffset {
pub rot: f32,
@ -105,7 +100,7 @@ fn update_active_camera(
) {
// find the camera with the current state, set it as the ActiveCamera
query.iter_mut().for_each(|(mut cam, cyber)| {
if cyber.eq(state.current()) {
if cyber.eq(&state.0) {
cam.is_active = true;
} else {
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) {
let new_state = state.current().next();
let new_state = state.0.next();
info!("{:?}", new_state);
state.set(new_state).unwrap();
next.set(new_state);
keys.reset(KeyCode::D);
}
}
@ -133,7 +132,7 @@ impl Plugin for CyberCamPlugin {
fn common(app: &mut bevy::prelude::App) {
app.insert_resource(DebugCamOffset::default())
.add_startup_system(setup_cybercams)
.add_state(CyberCameras::Hero)
.add_state::<CyberCameras>()
.add_system(cycle_cam_state)
.add_system(update_active_camera)
.add_system(follow_cyberbike);

View File

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

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;
@ -48,30 +52,33 @@ fn update_debug_cam(mut offset: ResMut<DebugCamOffset>, mut keys: ResMut<Input<K
fn update_input(mut events: EventReader<GamepadEvent>, mut istate: ResMut<InputState>) {
for pad_event in events.iter() {
match pad_event {
GamepadEvent::Button(GamepadButtonType::RightTrigger2, val) => {
istate.throttle = val;
}
GamepadEvent::Button(GamepadButtonType::LeftTrigger2, val) => {
istate.throttle = -val;
}
GamepadEvent::Button(GamepadButtonType::East, val) => {
if val > 0.5 {
GamepadEvent::Button(button_event) => {
let GamepadButtonChangedEvent {
button_type, value, ..
} = button_event;
match button_type {
GamepadButtonType::RightTrigger => istate.throttle = *value,
GamepadButtonType::LeftTrigger => istate.throttle = -value,
GamepadButtonType::East => {
if value > &0.5 {
istate.brake = true;
} else {
istate.brake = false;
}
}
GamepadEvent::Axis(GamepadAxisType::LeftStickX, val) => {
istate.yaw = val;
_ => info!("unhandled button press: {button_event:?}"),
}
// ignore spurious vertical movement for now
GamepadEvent::Axis(GamepadAxisType::LeftStickY, val) => {
istate.pitch = val;
}
_ => {
info!("unhandled gamepad event: {:?}", pad_event);
GamepadEvent::Axis(axis_event) => {
let GamepadAxisChangedEvent {
axis_type, value, ..
} = axis_event;
match axis_type {
_ => info!("unhandled axis event: {axis_event:?}"),
}
}
GamepadEvent::Connection(_) => {}
}
}
}

View File

@ -1,6 +1,7 @@
use bevy::{
ecs::schedule::SystemSet,
prelude::{Query, Vec3, Window},
prelude::{Query, Vec3, Window, With},
window::PrimaryWindow,
};
pub mod action;
@ -20,10 +21,10 @@ pub enum Label {
Action,
}
pub fn disable_mouse_trap(mut windows: Query<&Window>) {
let window = windows.get_primary_mut().unwrap();
window.set_cursor_grab_mode(bevy::window::CursorGrabMode::None);
window.set_cursor_visibility(true);
pub fn disable_mouse_trap(mut window: Query<&mut Window, With<PrimaryWindow>>) {
let mut window = window.get_single_mut().unwrap();
window.cursor.grab_mode = bevy::window::CursorGrabMode::None;
window.cursor.visible = true;
}
pub fn random_unit_vec(r: &mut impl rand::prelude::Rng) -> Vec3 {

View File

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

View File

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

View File

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