2022-01-12 08:18:13 +00:00
|
|
|
use bevy::prelude::*;
|
|
|
|
|
2022-01-14 00:14:08 +00:00
|
|
|
use crate::{
|
|
|
|
action::MovementSettings,
|
|
|
|
geometry::{CyberBike, SPAWN_ALTITUDE},
|
|
|
|
input::InputState,
|
|
|
|
};
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-01-14 00:14:08 +00:00
|
|
|
pub(crate) const CAM_DIST: f32 = 15.0;
|
2022-01-12 08:18:13 +00:00
|
|
|
|
|
|
|
#[derive(Component, Debug)]
|
|
|
|
pub struct CyberCam;
|
|
|
|
|
|
|
|
fn setup_cybercam(mut commands: Commands) {
|
|
|
|
commands
|
|
|
|
.spawn_bundle(PerspectiveCameraBundle {
|
2022-01-14 00:14:08 +00:00
|
|
|
transform: Transform::from_xyz(SPAWN_ALTITUDE + CAM_DIST, 0.0, 0.0)
|
2022-01-12 08:18:13 +00:00
|
|
|
.looking_at(Vec3::ZERO, Vec3::Y),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.insert(CyberCam);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn follow_player(
|
2022-01-14 00:14:08 +00:00
|
|
|
bike_query: Query<&Transform, (Without<CyberCam>, With<CyberBike>)>,
|
|
|
|
mut cam_query: Query<&mut Transform, (With<CyberCam>, Without<CyberBike>)>,
|
2022-01-12 08:18:13 +00:00
|
|
|
) {
|
2022-01-14 00:14:08 +00:00
|
|
|
let bike_xform = bike_query.single();
|
2022-01-12 08:18:13 +00:00
|
|
|
let up = bike_xform.translation.normalize();
|
|
|
|
|
2022-01-14 00:14:08 +00:00
|
|
|
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);
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-01-14 00:14:08 +00:00
|
|
|
let mut cam_xform = cam_query.single_mut();
|
2022-01-12 08:18:13 +00:00
|
|
|
cam_xform.translation = cam_pos;
|
|
|
|
cam_xform.look_at(look_at, up);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn player_look(
|
|
|
|
settings: Res<MovementSettings>,
|
|
|
|
windows: Res<Windows>,
|
|
|
|
time: Res<Time>,
|
|
|
|
istate: Res<InputState>,
|
2022-01-14 00:14:08 +00:00
|
|
|
mut query: Query<&mut Transform, With<CyberBike>>,
|
2022-01-12 08:18:13 +00:00
|
|
|
) {
|
|
|
|
let window = windows.get_primary().unwrap();
|
|
|
|
let window_scale = window.height().min(window.width());
|
|
|
|
let dt = time.delta_seconds();
|
2022-01-14 00:14:08 +00:00
|
|
|
let mut transform = query.single_mut();
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-01-14 00:14:08 +00:00
|
|
|
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);
|
2022-01-12 08:18:13 +00:00
|
|
|
|
|
|
|
transform.rotate(rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CyberCamPlugin;
|
|
|
|
impl Plugin for CyberCamPlugin {
|
|
|
|
fn build(&self, app: &mut bevy::prelude::App) {
|
|
|
|
app.add_startup_system(setup_cybercam)
|
|
|
|
.add_system(follow_player)
|
|
|
|
.add_system(player_look);
|
|
|
|
}
|
|
|
|
}
|