2022-01-12 08:18:13 +00:00
|
|
|
use bevy::prelude::*;
|
|
|
|
|
2022-01-27 00:53:37 +00:00
|
|
|
use crate::{
|
2022-02-27 07:31:01 +00:00
|
|
|
geometry::{CyberBikeModel, SPAWN_ALTITUDE},
|
2022-01-27 00:53:37 +00:00
|
|
|
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
|
|
|
|
2022-01-27 00:53:37 +00:00
|
|
|
// 85 degrees in radians
|
|
|
|
const MAX_PITCH: f32 = 1.48353;
|
|
|
|
|
2022-01-12 08:18:13 +00:00
|
|
|
#[derive(Component, Debug)]
|
|
|
|
pub struct CyberCam;
|
|
|
|
|
|
|
|
fn setup_cybercam(mut commands: Commands) {
|
2022-02-24 01:38:24 +00:00
|
|
|
let projection = PerspectiveProjection {
|
|
|
|
fov: std::f32::consts::FRAC_PI_3,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-01-12 08:18:13 +00:00
|
|
|
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),
|
2022-02-24 01:38:24 +00:00
|
|
|
perspective_projection: projection,
|
2022-01-12 08:18:13 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.insert(CyberCam);
|
|
|
|
}
|
|
|
|
|
2022-01-14 01:32:39 +00:00
|
|
|
fn follow_cyberbike(
|
2022-02-27 07:31:01 +00:00
|
|
|
bike_query: Query<&Transform, (Without<CyberCam>, With<CyberBikeModel>)>,
|
|
|
|
mut cam_query: Query<&mut Transform, (With<CyberCam>, Without<CyberBikeModel>)>,
|
2022-01-27 00:53:37 +00:00
|
|
|
input: Res<InputState>,
|
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 06:05:51 +00:00
|
|
|
let look_at = bike_xform.translation + (bike_xform.forward() * 200.0);
|
|
|
|
let cam_pos = bike_xform.translation + (bike_xform.back() * 2.7) + (up * 2.4);
|
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);
|
2022-01-27 00:53:37 +00:00
|
|
|
|
|
|
|
// handle input pitch
|
|
|
|
let angle = input.pitch.powi(3) * MAX_PITCH;
|
|
|
|
let axis = cam_xform.right();
|
|
|
|
cam_xform.rotate(Quat::from_axis_angle(axis, angle));
|
2022-01-12 08:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CyberCamPlugin;
|
|
|
|
impl Plugin for CyberCamPlugin {
|
|
|
|
fn build(&self, app: &mut bevy::prelude::App) {
|
|
|
|
app.add_startup_system(setup_cybercam)
|
2022-01-14 01:32:39 +00:00
|
|
|
.add_system(follow_cyberbike);
|
2022-01-12 08:18:13 +00:00
|
|
|
}
|
|
|
|
}
|