Clean up Queries, add more Cyber.
This commit is contained in:
parent
6a79816968
commit
7de11bb8a9
7 changed files with 98 additions and 97 deletions
30
Cargo.lock
generated
30
Cargo.lock
generated
|
@ -1181,9 +1181,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.3"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
|
||||
checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"js-sys",
|
||||
|
@ -1322,7 +1322,7 @@ checksum = "a538f217be4d405ff4719a283ca68323cc2384003eca5baaa87501e821c81dda"
|
|||
dependencies = [
|
||||
"bitflags",
|
||||
"gpu-descriptor-types",
|
||||
"hashbrown",
|
||||
"hashbrown 0.11.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1344,6 +1344,12 @@ dependencies = [
|
|||
"svg_fmt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.11.2"
|
||||
|
@ -1458,12 +1464,12 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "1.8.0"
|
||||
version = "1.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223"
|
||||
checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"hashbrown",
|
||||
"hashbrown 0.9.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1691,9 +1697,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "naga"
|
||||
version = "0.8.1"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4419062f8aa39fb25938169486341945758679e260ddbc1f94bfd1f33924dc2"
|
||||
checksum = "1dfa3912b150e6bfb38a7e94d3f53b950a456a905bb8858590af02006e2e78be"
|
||||
dependencies = [
|
||||
"bit-set",
|
||||
"bitflags",
|
||||
|
@ -2779,9 +2785,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "wgpu-core"
|
||||
version = "0.12.1"
|
||||
version = "0.12.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cfe9cb9c9d728c0f7ea0c309f1b3d5e9d5c7d379890d0a4e3df3103323ff7a84"
|
||||
checksum = "c4688c000eb841ca55f7b35db659b78d6e1cd77d7caf8fb929f4e181f754047d"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bitflags",
|
||||
|
@ -2802,9 +2808,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "wgpu-hal"
|
||||
version = "0.12.1"
|
||||
version = "0.12.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "742ec904d0577e1a88b82dba6cf0347c12197cd664b859b05f868bceb8cfff4d"
|
||||
checksum = "92e33cb9c380dd1166f316dfc511ad9646f72cf2deb47e90bd714db3617a6998"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"ash",
|
||||
|
|
|
@ -23,15 +23,15 @@ impl Default for MovementSettings {
|
|||
}
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub(crate) struct PlayerState {
|
||||
pub(crate) struct CyberBikeState {
|
||||
pub velocity: Vec3,
|
||||
pub colliding: bool,
|
||||
}
|
||||
|
||||
fn falling_cat(time: Res<Time>, mut bike_query: Query<(&mut Transform, &CyberBike)>) {
|
||||
fn falling_cat(time: Res<Time>, mut bike_query: Query<&mut Transform, With<CyberBike>>) {
|
||||
let dt = time.delta_seconds();
|
||||
|
||||
let (mut bike_xform, _) = bike_query.single_mut();
|
||||
let mut bike_xform = bike_query.single_mut();
|
||||
let up = bike_xform.translation.normalize();
|
||||
let cam_up = bike_xform.up();
|
||||
let cos = up.dot(cam_up);
|
||||
|
@ -52,38 +52,35 @@ fn falling_cat(time: Res<Time>, mut bike_query: Query<(&mut Transform, &CyberBik
|
|||
|
||||
let rot = Quat::from_axis_angle(cam_up.cross(up).normalize(), angle);
|
||||
|
||||
if rot.is_finite() && theta.abs() > 0.0085 {
|
||||
if rot.is_finite() && theta.abs() > 1.0f32.to_radians() {
|
||||
bike_xform.rotate(rot);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_velocity(
|
||||
time: Res<Time>,
|
||||
mut bike_query: Query<(&mut Transform, &PlayerState, &CyberBike)>,
|
||||
) {
|
||||
fn apply_velocity(time: Res<Time>, mut bike_query: Query<(&mut Transform, &CyberBikeState)>) {
|
||||
let dt = time.delta_seconds();
|
||||
|
||||
let (mut bike_xform, player_state, _) = bike_query.single_mut();
|
||||
let (mut bike_xform, state) = bike_query.single_mut();
|
||||
|
||||
if player_state.velocity.is_finite() {
|
||||
bike_xform.translation += player_state.velocity * dt;
|
||||
if state.velocity.is_finite() {
|
||||
bike_xform.translation += state.velocity * dt;
|
||||
}
|
||||
}
|
||||
|
||||
fn update_player_vel(
|
||||
fn update_velocity(
|
||||
time: Res<Time>,
|
||||
settings: Res<MovementSettings>,
|
||||
input: Res<InputState>,
|
||||
mut query: Query<(&Transform, &mut PlayerState, &CyberBike)>,
|
||||
mut query: Query<(&Transform, &mut CyberBikeState)>,
|
||||
) {
|
||||
let dt = time.delta_seconds();
|
||||
let (xform, mut pstate, _) = query.single_mut();
|
||||
let (xform, mut state) = query.single_mut();
|
||||
|
||||
// first gravity
|
||||
let down = -xform.translation.normalize();
|
||||
let dvel = down * settings.gravity * dt;
|
||||
let mut vel = if pstate.velocity.is_finite() {
|
||||
pstate.velocity + dvel
|
||||
let mut vel = if state.velocity.is_finite() {
|
||||
state.velocity + dvel
|
||||
} else {
|
||||
dvel
|
||||
};
|
||||
|
@ -91,7 +88,6 @@ fn update_player_vel(
|
|||
// thrust or brake
|
||||
vel += xform.forward() * input.throttle * dt * settings.accel;
|
||||
|
||||
|
||||
// brake
|
||||
if input.brake {
|
||||
let s = vel.length_squared();
|
||||
|
@ -107,51 +103,45 @@ fn update_player_vel(
|
|||
let drag = vel * settings.drag * v2 * dt;
|
||||
vel -= drag;
|
||||
|
||||
if !vel.is_finite() {
|
||||
if vel.length_squared() < 0.0001 {
|
||||
vel = Vec3::ZERO;
|
||||
}
|
||||
|
||||
pstate.velocity = vel;
|
||||
state.velocity = vel;
|
||||
}
|
||||
|
||||
fn collision_detection(
|
||||
fn collisions(
|
||||
mut events: EventReader<CollisionEvent>,
|
||||
mut query: Query<&mut PlayerState>,
|
||||
mut query: Query<(&Transform, &mut CyberBikeState)>,
|
||||
) {
|
||||
let mut pstate = query.single_mut();
|
||||
let (xform, mut state) = query.single_mut();
|
||||
|
||||
for event in events.iter() {
|
||||
if let CollisionEvent::Started(_, _) = event {
|
||||
pstate.colliding = true;
|
||||
state.colliding = true;
|
||||
} else {
|
||||
pstate.colliding = false;
|
||||
state.colliding = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn collision_reaction(mut query: Query<(&mut PlayerState, &Transform)>) {
|
||||
let (mut pstate, xform) = query.single_mut();
|
||||
|
||||
let down = -xform.translation.normalize();
|
||||
|
||||
// now see if we're currently colliding
|
||||
if pstate.colliding {
|
||||
let vel = pstate.velocity;
|
||||
let dvel = down * vel.dot(down) * 1.02;
|
||||
pstate.velocity -= dvel;
|
||||
if state.colliding {
|
||||
let down = -xform.translation.normalize();
|
||||
let vel = state.velocity;
|
||||
let dvel = down * vel.dot(down) * 1.0001;
|
||||
state.velocity -= dvel;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CyberPhysicsPlugin;
|
||||
impl Plugin for CyberPhysicsPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_resource::<PlayerState>()
|
||||
app.init_resource::<CyberBikeState>()
|
||||
.init_resource::<MovementSettings>()
|
||||
.add_plugin(PhysicsPlugin::default())
|
||||
.add_system(collision_detection)
|
||||
.add_system(collision_reaction)
|
||||
.add_system(collisions)
|
||||
.add_system(falling_cat)
|
||||
.add_system(update_player_vel)
|
||||
.add_system(update_velocity)
|
||||
.add_system(apply_velocity);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,17 +1,20 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::{geometry::CyberBike, input::InputState, action::MovementSettings};
|
||||
use crate::{
|
||||
action::MovementSettings,
|
||||
geometry::{CyberBike, SPAWN_ALTITUDE},
|
||||
input::InputState,
|
||||
};
|
||||
|
||||
pub(crate) const CAM_DIST: f32 = 50.0;
|
||||
pub(crate) const CAM_DIST: f32 = 15.0;
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct CyberCam;
|
||||
|
||||
fn setup_cybercam(mut commands: Commands) {
|
||||
use crate::geometry::PLAYER_DIST;
|
||||
commands
|
||||
.spawn_bundle(PerspectiveCameraBundle {
|
||||
transform: Transform::from_xyz(PLAYER_DIST + CAM_DIST, 0.0, 0.0)
|
||||
transform: Transform::from_xyz(SPAWN_ALTITUDE + CAM_DIST, 0.0, 0.0)
|
||||
.looking_at(Vec3::ZERO, Vec3::Y),
|
||||
..Default::default()
|
||||
})
|
||||
|
@ -19,16 +22,16 @@ fn setup_cybercam(mut commands: Commands) {
|
|||
}
|
||||
|
||||
fn follow_player(
|
||||
bike_query: Query<(&Transform, &CyberBike), Without<CyberCam>>,
|
||||
mut cam_query: Query<(&mut Transform, &CyberCam), Without<CyberBike>>,
|
||||
bike_query: Query<&Transform, (Without<CyberCam>, With<CyberBike>)>,
|
||||
mut cam_query: Query<&mut Transform, (With<CyberCam>, Without<CyberBike>)>,
|
||||
) {
|
||||
let (bike_xform, _) = bike_query.single();
|
||||
let bike_xform = bike_query.single();
|
||||
let up = bike_xform.translation.normalize();
|
||||
|
||||
let look_at = bike_xform.translation;
|
||||
let cam_pos = bike_xform.translation + (bike_xform.back() * CAM_DIST * 1.3) + (up * CAM_DIST);
|
||||
let look_at = bike_xform.translation + (bike_xform.forward() * CAM_DIST);
|
||||
let cam_pos = bike_xform.translation + (bike_xform.back() * CAM_DIST * 1.5) + (up * CAM_DIST);
|
||||
|
||||
let (mut cam_xform, _) = cam_query.single_mut();
|
||||
let mut cam_xform = cam_query.single_mut();
|
||||
cam_xform.translation = cam_pos;
|
||||
cam_xform.look_at(look_at, up);
|
||||
}
|
||||
|
@ -38,17 +41,17 @@ fn player_look(
|
|||
windows: Res<Windows>,
|
||||
time: Res<Time>,
|
||||
istate: Res<InputState>,
|
||||
mut query: Query<(&mut Transform, &CyberBike)>,
|
||||
mut query: Query<&mut Transform, With<CyberBike>>,
|
||||
) {
|
||||
let window = windows.get_primary().unwrap();
|
||||
let window_scale = window.height().min(window.width());
|
||||
let dt = time.delta_seconds();
|
||||
let (mut transform, _) = query.single_mut();
|
||||
let mut transform = query.single_mut();
|
||||
|
||||
let d_alt = (settings.sensitivity * dt * window_scale * istate.pitch).to_radians();
|
||||
let d_az = (settings.sensitivity * dt * window_scale * istate.yaw).to_radians();
|
||||
let rotation = Quat::from_axis_angle(transform.local_y(), d_az)
|
||||
* Quat::from_axis_angle(transform.local_x(), d_alt);
|
||||
let d_pitch = (settings.sensitivity * dt * window_scale * istate.pitch).to_radians();
|
||||
let d_yaw = (settings.sensitivity * dt * window_scale * istate.yaw).to_radians();
|
||||
let rotation = Quat::from_axis_angle(transform.local_y(), d_yaw)
|
||||
* Quat::from_axis_angle(transform.local_x(), d_pitch);
|
||||
|
||||
transform.rotate(rotation);
|
||||
}
|
||||
|
|
|
@ -2,26 +2,19 @@ use bevy::prelude::*;
|
|||
use heron::prelude::{CollisionShape, RigidBody};
|
||||
|
||||
pub const PLANET_RADIUS: f32 = 150.0;
|
||||
pub(crate) const PLAYER_DIST: f32 = PLANET_RADIUS + 100.0;
|
||||
|
||||
pub struct CyberGeomPlugin;
|
||||
|
||||
impl Plugin for CyberGeomPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_startup_system(setup_giant_sphere)
|
||||
.add_startup_system(setup_player);
|
||||
}
|
||||
}
|
||||
pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS + 100.0;
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct CyberBike;
|
||||
|
||||
fn setup_giant_sphere(
|
||||
#[derive(Component)]
|
||||
pub struct CyberSphere;
|
||||
|
||||
fn spawn_giant_sphere(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
) {
|
||||
// world
|
||||
commands
|
||||
.spawn_bundle(PbrBundle {
|
||||
mesh: meshes.add(Mesh::from(shape::Icosphere {
|
||||
|
@ -37,18 +30,19 @@ fn setup_giant_sphere(
|
|||
|
||||
..Default::default()
|
||||
})
|
||||
.insert(CyberSphere)
|
||||
.insert(RigidBody::Static)
|
||||
.insert(CollisionShape::Sphere {
|
||||
radius: PLANET_RADIUS,
|
||||
});
|
||||
}
|
||||
|
||||
fn setup_player(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
use crate::action::PlayerState;
|
||||
fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
use crate::action::CyberBikeState;
|
||||
commands
|
||||
.spawn_bundle((
|
||||
Transform {
|
||||
translation: Vec3::new(PLAYER_DIST, 0.0, 0.0),
|
||||
translation: Vec3::new(SPAWN_ALTITUDE, 0.0, 0.0),
|
||||
..Default::default()
|
||||
}
|
||||
.looking_at(Vec3::ZERO, Vec3::Y),
|
||||
|
@ -63,5 +57,13 @@ fn setup_player(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
half_height: 2.0,
|
||||
radius: 0.8,
|
||||
})
|
||||
.insert(PlayerState::default());
|
||||
.insert(CyberBikeState::default());
|
||||
}
|
||||
|
||||
pub struct CyberGeomPlugin;
|
||||
impl Plugin for CyberGeomPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_startup_system(spawn_giant_sphere)
|
||||
.add_startup_system(spawn_cyberbike);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use bevy::{
|
||||
app::{Events, ManualEventReader},
|
||||
prelude::*,
|
||||
prelude::{info, GamepadEvent, GamepadEventType, Res, ResMut},
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
|
|
|
@ -5,18 +5,10 @@ use crate::geometry::PLANET_RADIUS;
|
|||
pub const LIGHT_RANGE: f32 = PLANET_RADIUS * 0.6;
|
||||
pub const LIGHT_DIST: f32 = PLANET_RADIUS * 1.2;
|
||||
|
||||
pub struct CyberSpaceLightsPlugin;
|
||||
|
||||
impl Plugin for CyberSpaceLightsPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_startup_system(setup).add_system(animate_lights);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Animate;
|
||||
|
||||
fn setup(
|
||||
fn spawn_lights(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
|
@ -207,3 +199,11 @@ fn animate_lights(
|
|||
*transform = Transform::from_xyz(x_new, y, z_new);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CyberSpaceLightsPlugin;
|
||||
impl Plugin for CyberSpaceLightsPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_startup_system(spawn_lights)
|
||||
.add_system(animate_lights);
|
||||
}
|
||||
}
|
||||
|
|
10
src/ui.rs
10
src/ui.rs
|
@ -1,6 +1,6 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::{geometry::PLANET_RADIUS, action::PlayerState};
|
||||
use crate::{action::CyberBikeState, geometry::PLANET_RADIUS};
|
||||
|
||||
#[derive(Component)]
|
||||
struct UpText;
|
||||
|
@ -33,15 +33,15 @@ fn setup_ui(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
}
|
||||
|
||||
fn update_ui(
|
||||
pstate_query: Query<(&PlayerState, &Transform)>,
|
||||
state_query: Query<(&CyberBikeState, &Transform)>,
|
||||
mut text_query: Query<&mut Text, With<UpText>>,
|
||||
) {
|
||||
let mut text = text_query.single_mut();
|
||||
let pstate = pstate_query.single();
|
||||
let state = state_query.single();
|
||||
text.sections[0].value = format!(
|
||||
"spd: {:.2}\nalt: {:.2}",
|
||||
pstate.0.velocity.length(),
|
||||
pstate.1.translation.length() - PLANET_RADIUS
|
||||
state.0.velocity.length(),
|
||||
state.1.translation.length() - PLANET_RADIUS
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue