Replace PlayerState resource with Component.

This commit is contained in:
Joe Ardent 2022-01-06 22:29:25 -08:00
parent 9bd0ca6d88
commit 2885be2003

View file

@ -7,12 +7,17 @@ use bevy::{
prelude::*, prelude::*,
utils::tracing::info, utils::tracing::info,
}; };
use heron::{
prelude::{CollisionShape, RigidBody},
CollisionEvent,
};
pub const PLANET_RADIUS: f32 = 75.0; pub const PLANET_RADIUS: f32 = 75.0;
const PLAYER_DIST: f32 = PLANET_RADIUS + 300.0; const PLAYER_DIST: f32 = PLANET_RADIUS + 100.0;
const CAM_DIST: f32 = 25.0; const CAM_DIST: f32 = 30.0;
#[derive(Component, Default)]
struct PlayerState { struct PlayerState {
velocity: Vec3, velocity: Vec3,
} }
@ -23,14 +28,6 @@ struct UpState {
cam_up: Vec3, cam_up: Vec3,
} }
impl Default for PlayerState {
fn default() -> Self {
PlayerState {
velocity: Vec3::ZERO,
}
}
}
#[derive(Default)] #[derive(Default)]
struct InputState { struct InputState {
event_reader: ManualEventReader<GamepadEvent>, event_reader: ManualEventReader<GamepadEvent>,
@ -90,7 +87,13 @@ fn setup_player(mut commands: Commands, asset_server: Res<AssetServer>) {
.with_children(|rider| { .with_children(|rider| {
rider.spawn_scene(asset_server.load("cyber-bike_no_y_up.glb#Scene0")); rider.spawn_scene(asset_server.load("cyber-bike_no_y_up.glb#Scene0"));
}) })
.insert(CyberBike); .insert(CyberBike)
.insert(RigidBody::Dynamic)
.insert(CollisionShape::Cone {
half_height: 2.0,
radius: 0.8,
})
.insert(PlayerState::default());
} }
fn setup_dbg_ui(mut commands: Commands, asset_server: Res<AssetServer>) { fn setup_dbg_ui(mut commands: Commands, asset_server: Res<AssetServer>) {
@ -136,13 +139,12 @@ fn player_move(
time: Res<Time>, time: Res<Time>,
_settings: Res<MovementSettings>, _settings: Res<MovementSettings>,
mut up_state: ResMut<UpState>, mut up_state: ResMut<UpState>,
mut state: ResMut<PlayerState>, mut bike_query: Query<(&mut Transform, &mut PlayerState), Without<FlyCam>>,
mut bike_query: Query<&mut Transform, (With<CyberBike>, Without<FlyCam>)>, mut cam_query: Query<(&mut Transform, &FlyCam), Without<PlayerState>>,
mut cam_query: Query<&mut Transform, (With<FlyCam>, Without<CyberBike>)>,
) { ) {
let dt = time.delta_seconds(); let dt = time.delta_seconds();
let mut bike_xform = bike_query.single_mut(); let (mut bike_xform, mut player_state) = bike_query.single_mut();
let up = bike_xform.translation.normalize(); let up = bike_xform.translation.normalize();
let cam_up = bike_xform.up(); let cam_up = bike_xform.up();
let cos = up.dot(cam_up); let cos = up.dot(cam_up);
@ -167,9 +169,9 @@ fn player_move(
bike_xform.rotate(rot); bike_xform.rotate(rot);
} }
if state.velocity.is_finite() { if player_state.velocity.is_finite() {
//state.velocity += -up * settings.gravity * dt; //state.velocity += -up * settings.gravity * dt;
bike_xform.translation += state.velocity * dt; bike_xform.translation += player_state.velocity * dt;
} else { } else {
//state.velocity = -up * settings.gravity * dt; //state.velocity = -up * settings.gravity * dt;
} }
@ -177,7 +179,7 @@ fn player_move(
up_state.cam_up = cam_up; up_state.cam_up = cam_up;
up_state.player_up = up; up_state.player_up = up;
let mut cam_xform = cam_query.single_mut(); let (mut cam_xform, _) = cam_query.single_mut();
cam_xform.translation = bike_xform.translation + (up * CAM_DIST); cam_xform.translation = bike_xform.translation + (up * CAM_DIST);
cam_xform.look_at(bike_xform.translation, Vec3::Y); cam_xform.look_at(bike_xform.translation, Vec3::Y);
} }
@ -188,13 +190,12 @@ fn player_look(
events: Res<Events<GamepadEvent>>, events: Res<Events<GamepadEvent>>,
time: Res<Time>, time: Res<Time>,
mut istate: ResMut<InputState>, mut istate: ResMut<InputState>,
mut pstate: ResMut<PlayerState>, mut query: Query<(&mut Transform, &mut PlayerState)>,
mut query: Query<&mut Transform, With<CyberBike>>,
) { ) {
let window = windows.get_primary().unwrap(); let window = windows.get_primary().unwrap();
let window_scale = window.height().min(window.width()); let window_scale = window.height().min(window.width());
let dt = time.delta_seconds(); let dt = time.delta_seconds();
let mut transform = query.single_mut(); let (mut transform, mut pstate) = query.single_mut();
let mut vel = pstate.velocity; let mut vel = pstate.velocity;
if pstate.velocity.is_finite() { if pstate.velocity.is_finite() {